def __call__(self, environ, start_response): status = "200 OK" # TODO: put headers into response response_headers = [("Content-type", "text/html")] form = cgi.FieldStorage(fp=environ["wsgi.input"], environ=environ) request = http_io.Request() for key in form.keys(): request.SetParameter(key, form.getvalue(key, "")) origin_request_host = utils.GetSchemeHostPort(environ) request.SetParameter(constants.ORIGIN_REQUEST_HOST, origin_request_host) logger.debug("Origin request host: %s", origin_request_host) response = http_io.Response() if request.parameters: self._publish_servlet.DoGet(request, response) else: logger.error("Internal Error - Request has no parameters") http_io.ResponseWriter.AddBodyElement( response, constants.HDR_STATUS_MESSAGE, "Internal Error - Request has no parameters") http_io.AddBodyElement(response, constants.HDR_STATUS_CODE, constants.STATUS_FAILURE) start_response(status, response_headers) return response.body
def __call__(self, environ, start_response): """Executes an application. Parses HTTP requests into internal request object and delegates processing to SnippetsEditorHandler. Args: environ: WSGI environment. start_response: callable that starts response. Returns: response body. """ request_method = "GET" if "REQUEST_METHOD" in environ: request_method = environ["REQUEST_METHOD"] # Get parameters from HTTP request. request = http_io.Request() if request_method == "GET": form = cgi.FieldStorage(fp=environ["wsgi.input"], environ=environ) for key in form.keys(): request.SetParameter(key, form.getvalue(key, "")) else: try: request_body_size = int(environ.get("CONTENT_LENGTH", 0)) except ValueError: request_body_size = 0 post_input = environ["wsgi.input"].read(request_body_size) logger.debug("POST request body: %s", post_input) self.__ParsePostInput(post_input, request) response = http_io.Response() if request.parameters: self._handler.DoRequest(request, response) else: logger.error("Internal Error - Request has no parameters.") http_io.ResponseWriter.AddJsonFailureBody( response, "Internal Error - Request has no parameters") try: start_response(SnippetsApp.STATUS_OK, SnippetsApp.RESPONSE_HEADERS) return response.body except Exception: exc_info = sys.exc_info() start_response(SnippetsApp.STATUS_ERROR, SnippetsApp.RESPONSE_HEADERS, exc_info) return exceptions.FormatException(exc_info)
def __call__(self, environ, start_response): """Executes an application. Parses HTTP requests into internal request object and delegates processing to StreamPushServlet. Args: environ: WSGI environment. start_response: callable that starts response. Returns: response body. """ form = cgi.FieldStorage(fp=environ["wsgi.input"], environ=environ) # Get parameters from HTTP request. request = http_io.Request() for key in form.keys(): request.SetParameter(key, form.getvalue(key, "")) response = http_io.Response() if request.parameters: self._stream_pusher.DoGet(request, response) else: logger.error("Internal Error - Request has no parameters.") http_io.ResponseWriter.AddBodyElement( response, constants.HDR_STATUS_MESSAGE, "Internal Error - Request has no parameters") http_io.AddBodyElement(response, constants.HDR_STATUS_CODE, constants.STATUS_FAILURE) try: start_response(StreamPushApp.STATUS_OK, StreamPushApp.RESPONSE_HEADERS) return response.body except Exception: exc_info = sys.exc_info() start_response(StreamPushApp.STATUS_ERROR, StreamPushApp.RESPONSE_HEADERS, exc_info) return self._FormatException(exc_info)
def HandleSwapTargetsRequest(self, request, response): """Handles swap targets request. Sample request: '/geserve/Publish?Cmd=SwapTargets&Host=fusion.host.name& TargetPathA=target_path_a&TargetPathB=target_path_b Args: request: request object. response: response object. Raises: PublishServeException. """ logger.debug("HandleSwapTargetsRequest...") # Extract parameters. target_path_in_a = request.GetParameter(constants.TARGET_PATH_A) target_path_in_b = request.GetParameter(constants.TARGET_PATH_B) origin_request_host = request.GetParameter( constants.ORIGIN_REQUEST_HOST) target_path_a = serve_utils.NormalizeTargetPath(target_path_in_a) if not target_path_a: raise exceptions.PublishServeException( "HandleSwapTargetsRequest: Not a valid target path %s " "(path format is /sub_path1[/sub_path2]." % target_path_in_a) target_path_b = serve_utils.NormalizeTargetPath(target_path_in_b) if not target_path_b: raise exceptions.PublishServeException( "HandleSwapTargetsRequest: Not a valid target path %s " "(path format is /sub_path1[/sub_path2]." % target_path_in_b) (target_details_a, target_details_b) = self._publish_helper.SwapTargets( target_path_a, target_path_b) # Unpublish targets. # There is no need to handle any errors/exceptions here, as they # are handled by the 'UnPublish' service. request_u = self._CreateUnpublishRequest(origin_request_host, target_path_a) response_u = http_io.Response() self.HandleUnpublishRequest(request_u, response_u) request_u = self._CreateUnpublishRequest(origin_request_host, target_path_b) response_u = http_io.Response() self.HandleUnpublishRequest(request_u, response_u) # Publish targets with new db_name. request_a = self._CreatePublishRequest(origin_request_host, target_details_a) response_a = http_io.Response() self.HandlePublishRequest(request_a, response_a) request_b = self._CreatePublishRequest(origin_request_host, target_details_b) response_b = http_io.Response() self.HandlePublishRequest(request_b, response_b) logger.debug("Targets %s and %s have been successfully swapped.", target_path_a, target_path_b) http_io.ResponseWriter.AddBodyElement(response, constants.HDR_STATUS_CODE, constants.STATUS_SUCCESS)
def HandleRepublishRequest(self, request, response): """Handles republish database request. Sample request: '/geserve/Publish?Cmd=RepublishDb&Host=fusion.host.name& TargetPath=test&DbName=/gevol/assets/Databases/terrain_alpha_pack_test.kdata base/gedb.kda/ver00X/gedb' Args: request: request object. response: response object. Raises: PublishServeException. """ logger.debug("HandleRepublishRequest...") # Extract parameters db_name = request.GetParameter(constants.DB_NAME) target_path_in = request.GetParameter(constants.TARGET_PATH) origin_request_host = request.GetParameter( constants.ORIGIN_REQUEST_HOST) client_host_name = request.GetParameter(constants.HOST_NAME) if not db_name or not target_path_in or not client_host_name: raise exceptions.PublishServeException( "HandleRepublishRequest:db_name, host_name or target_path " "parameters not available in the request: %s, %s, %s.", db_name, client_host_name, target_path_in) target_path = serve_utils.NormalizeTargetPath(target_path_in) if not target_path: raise exceptions.PublishServeException( "HandleRepublishRequest: Not a valid target path %s " "(path format is /sub_path1[/sub_path2]." % target_path_in) if self._publish_helper.IsDatabasePushed(client_host_name, db_name): target_details = self._publish_helper.GetTargetDetails(target_path) if not target_details: raise exceptions.PublishServeException( "HandleRepublishRequest: Make sure the target path %s " "exists and is currently published." % target_path) if "publishcontext" not in target_details.keys(): raise exceptions.PublishServeException( "Republish is not supported for targets " "published using GEE version 5.1.2 or earlier.") # Check if the databases are comparable, versions of the same database. if not self._publish_helper.AreDatabasesComparable( db_name, client_host_name, target_details["dbname"], target_details["fusion_host"]): raise exceptions.PublishServeException( "HandleRepublishRequest: Database names do not match for target " "and given database. Target database: %s, %s, Input database: %s, " "%s should only be versions of the same database." % (target_details["fusion_host"], target_details["dbname"], client_host_name, db_name)) # Check if db_name has POI data. # Get database ID from gesearch database. value = target_details.get("fusion_host", "") search_db_id = self._publish_helper.GetSearchDbId(value, db_name) if search_db_id == 0: # db_name has no POI data. Do not republish if DB published # on target has POISearch enabled. if "POISearch" in target_details["publishcontext"].get( "searchdefs"): raise exceptions.PublishServeException( "HandleRepublishRequest: target_path %s has POISearch service " "enabled while the new version of database %s has no POISearch " "data. Republish is disabled." % (target_path, db_name)) # Replace db name with the new one. target_details["dbname"] = db_name # Unpublish target. request_u = self._CreateUnpublishRequest(origin_request_host, target_path) response_u = http_io.Response() self.HandleUnpublishRequest(request_u, response_u) # Publish target with new db_name. request_p = self._CreatePublishRequest(origin_request_host, target_details) response_p = http_io.Response() self.HandlePublishRequest(request_p, response_p) logger.debug( "Database %s has been successfully republished to " "target %s.", db_name, target_path) http_io.ResponseWriter.AddBodyElement(response, constants.HDR_STATUS_CODE, constants.STATUS_SUCCESS)