def post(self): session_id = get_session_id(session, request) json_dict = request.get_json(silent=True) if json_dict is False or json_dict is None: raise MalformedJSONHTTPError(data=request.get_data()) cimport_params = json_dict.get('object', None) check_required_keys(cimport_params or {}, CImportParams.required) file_contents = cimport_params['urlenc_file_contents'] file_contents = unquote(file_contents) file_contents = file_contents.replace("\u2018", "'").replace("\u2019", "'") overwrite = cimport_params['overwrite'] type = cimport_params['type'] if file_contents.startswith('<?xml'): fd, abs_path = mkstemp(suffix='.xml') fs_temp = codecs.open(abs_path, 'w','utf-8') fs_temp.write(file_contents) fs_temp.close() fd_close(fd) try: dao = ImportDAO(session_id) result = dao.file_import(abs_path, type, overwrite) dao.close() except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError(status_code=500,message=str(ex.message),status='Unknown error') remove_file(abs_path) resp_dict = {'message': str(result)} resp = make_response(json_serialize(resp_dict, session_id=session_id), OK) resp.headers['Content-Type'] = 'application/json' return resp elif type == 'Attack Tree (Dot)': try: environment_name = cimport_params['environment'] contributor_name = cimport_params['contributor'] dao = ImportDAO(session_id) result = dao.import_attack_tree(file_contents,environment_name,contributor_name) dao.close() except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError(status_code=500,message=str(ex.message),status='Unknown error') else: raise CairisHTTPError(status_code=BAD_REQUEST,message='The provided file is not a valid XML file',status='Invalid XML input')
def post(self, type): session_id = get_session_id(session, request) try: if not request.files: raise LookupError() file = request.files['file'] except LookupError: raise MissingParameterHTTPError(param_names=['file']) try: fd, abs_path = mkstemp(suffix='.xml') fs_temp = open(abs_path, 'wb') xml_text = file.stream.read() fs_temp.write(xml_text) fs_temp.close() fd_close(fd) except IOError: raise CairisHTTPError( status_code=CONFLICT, status='Unable to load XML file', message='The XML file could not be loaded on the server.' + 'Please check if the application has permission to write temporary files.' ) try: dao = ImportDAO(session_id) result = dao.file_import(abs_path, type, 1) dao.close() except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError(status_code=500, message=str(ex), status='Unknown error') remove_file(abs_path) message = str(result) if (result == 0): message = file.filename + ' imported' resp_dict = {'message': message} resp = make_response(json_serialize(resp_dict, session_id=session_id), OK) resp.headers['Content-Type'] = 'application/json' return resp
def post(self): session_id = get_session_id(session, request) json_dict = request.get_json(silent=True) if json_dict is False or json_dict is None: raise MalformedJSONHTTPError(data=request.get_data()) cimport_params = json_dict.get('object', None) check_required_keys(cimport_params or {}, CImportParams.required) file_contents = cimport_params['urlenc_file_contents'] file_contents = unquote(file_contents) type = cimport_params['type'] overwrite = cimport_params.get('overwrite', None) if file_contents.startswith('<?xml'): fd, abs_path = mkstemp(suffix='.xml') fs_temp = open(abs_path, 'w') fs_temp.write(file_contents) fs_temp.close() fd_close(fd) try: result = cimport.file_import(abs_path, type, overwrite, session_id=session_id) except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError( status_code=500, message=str(ex.message), status='Unknown error' ) remove_file(abs_path) resp_dict = {'message': result} resp = make_response(json_serialize(resp_dict, session_id=session_id), httplib.OK) resp.headers['Content-Type'] = 'application/json' return resp else: raise CairisHTTPError( status_code=httplib.BAD_REQUEST, message='The provided file is not a valid XML file', status='Invalid XML input' )
def post(self, type): session_id = get_session_id(session, request) overwrite = request.form.get('overwrite', None) overwrite = request.args.get('overwrite', overwrite) try: if not request.files: raise LookupError() file = request.files['file'] except LookupError: raise MissingParameterHTTPError(param_names=['file']) try: fd, abs_path = mkstemp(suffix='.xml') fs_temp = open(abs_path, 'w') xml_text = file.stream.read() fs_temp.write(xml_text) fs_temp.close() fd_close(fd) except IOError: raise CairisHTTPError( status_code=httplib.CONFLICT, status='Unable to load XML file', message='The XML file could not be loaded on the server.' + 'Please check if the application has permission to write temporary files.' ) try: result = cimport.file_import(abs_path, type, overwrite, session_id=session_id) except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError( status_code=500, message=str(ex.message), status='Unknown error' ) remove_file(abs_path) resp_dict = { 'message': result } resp = make_response(json_serialize(resp_dict, session_id=session_id), httplib.OK) resp.headers['Content-Type'] = 'application/json' return resp
def post(self): session_id = get_session_id(session, request) json_dict = request.get_json(silent=True) if json_dict is False or json_dict is None: raise MalformedJSONHTTPError(data=request.get_data()) cimport_params = json_dict.get('object', None) check_required_keys(cimport_params or {}, CImportParams.required) file_contents = cimport_params['urlenc_file_contents'] file_contents = unquote(file_contents) type = cimport_params['type'] if file_contents.startswith('<?xml'): fd, abs_path = mkstemp(suffix='.xml') fs_temp = open(abs_path, 'w') fs_temp.write(file_contents) fs_temp.close() fd_close(fd) try: dao = ImportDAO(session_id) result = dao.file_import(abs_path, type, 1) dao.close() except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError(status_code=500,message=str(ex.message),status='Unknown error') remove_file(abs_path) resp_dict = {'message': str(result)} resp = make_response(json_serialize(resp_dict, session_id=session_id), httplib.OK) resp.headers['Content-Type'] = 'application/json' return resp else: raise CairisHTTPError(status_code=httplib.BAD_REQUEST,message='The provided file is not a valid XML file',status='Invalid XML input')
def post(self): session_id = get_session_id(session, request) json_dict = request.get_json(silent=True) if json_dict is False or json_dict is None: raise MalformedJSONHTTPError(data=request.get_data()) cimport_params = json_dict.get('object', None) check_required_keys(cimport_params or {}, CImportParams.required) file_contents = cimport_params['urlenc_file_contents'] file_contents = unquote(file_contents) file_contents = file_contents.replace("\u2018", "'").replace("\u2019", "'") overwrite = cimport_params['overwrite'] type = cimport_params['type'] if file_contents.startswith('<?xml'): fd, abs_path = mkstemp(suffix='.xml') fs_temp = codecs.open(abs_path, 'w', 'utf-8') fs_temp.write(file_contents) fs_temp.close() fd_close(fd) try: dao = ImportDAO(session_id) if (type in [ 'diagrams.net (Data Flow Diagram)', 'diagrams.net (Asset Model)' ]): environment_name = cimport_params['environment'] result = dao.diagramsnet_import(abs_path, type, environment_name) else: result = dao.file_import(abs_path, type, overwrite) dao.close() except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) remove_file(abs_path) message = str(result) if (result == 0): message = 'Model imported' resp_dict = {'message': message} resp = make_response( json_serialize(resp_dict, session_id=session_id), OK) resp.headers['Content-Type'] = 'application/json' return resp elif type == 'Attack Tree (Dot)': try: environment_name = cimport_params['environment'] contributor_name = cimport_params['contributor'] dao = ImportDAO(session_id) result = dao.import_attack_tree(file_contents, environment_name, contributor_name) dao.close() message = str(result) if (result == 0): message = 'Model imported' resp_dict = {'message': message} resp_dict = {'message': str(result)} resp = make_response( json_serialize(resp_dict, session_id=session_id), OK) resp.headers['Content-Type'] = 'application/json' return resp except DatabaseProxyException as ex: raise ARMHTTPError(ex) except ARMException as ex: raise ARMHTTPError(ex) except Exception as ex: raise CairisHTTPError(status_code=500, message=str(ex), status='Unknown error') else: raise CairisHTTPError( status_code=BAD_REQUEST, message='The provided file is not a valid XML file', status='Invalid XML input')