/** * Responds to a request for a single connector * * @param connectorId The identifier of a connector * @return The response */ private HttpResponse onMessageGetConnector(String connectorId) { ConnectorService connector = getConnector(connectorId); if (connector == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, connector.serializedJSON()); }
/** * Responds to a request for a registered domain * * @return The response */ private HttpResponse onGetSchema(String identifier) { ArtifactSchema schema = getSchema(identifier); if (schema != null) return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, schema.serializedJSON()); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); } }
/** * Responds to a request for a registered archetypes * * @return The response */ private HttpResponse onGetArchetype(String identifier) { ArtifactArchetype archetype = getArchetype(identifier); if (archetype != null) return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, archetype.serializedJSON()); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
/** * Responds to a request for the me resource * * @param request The web API request to handle * @return The HTTP response */ private HttpResponse handleRequestMe(HttpApiRequest request) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, getCurrentUser().serializedJSON()); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { switch (request.getMethod()) { case HttpConstants.METHOD_GET: return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, serializedJSON()); case HttpConstants.METHOD_POST: return onMessagePostQuads(request); } return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected methods: GET, POST"); }
/** * Responds to a request for the registered archetypes * * @return The response */ private HttpResponse onGetArchetypes() { StringBuilder builder = new StringBuilder("["); boolean first = true; for (ArtifactArchetype archetype : getArchetypes()) { if (!first) builder.append(", "); first = false; builder.append(archetype.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { if (request.getUri().equals(apiUri + "/modules")) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); StringBuilder builder = new StringBuilder("["); boolean first = true; for (WebModule module : Register.getComponents(WebModule.class)) { if (!first) builder.append(", "); first = false; builder.append(module.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
/** * Responds to a request for the list of the connectors * * @return The response */ private HttpResponse onMessageListConnectors() { Collection<ConnectorService> connectors = getConnectors(); StringBuilder builder = new StringBuilder("["); boolean first = true; for (ConnectorService connector : connectors) { if (!first) builder.append(", "); first = false; builder.append(connector.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
/** * Responds to a request for the registered schemas * * @return The response */ private HttpResponse onGetSchemas() { StringBuilder builder = new StringBuilder("["); boolean first = true; for (ArtifactSchema schema : getSchemas()) { if (!first) builder.append(", "); first = false; builder.append(schema.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
/** * Responds to a request for the list of the descriptors * * @return The response */ private HttpResponse onMessageListDescriptors() { Collection<ConnectorDescriptor> domains = getDescriptors(); StringBuilder builder = new StringBuilder("["); boolean first = true; for (ConnectorDescriptor description : domains) { if (!first) builder.append(", "); first = false; builder.append(description.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); if (request.getUri().equals(apiUri + "/metrics")) return onMessageGetMetricList(securityService); if (request.getUri().startsWith(apiUri + "/metrics")) { String rest = request.getUri().substring(apiUri.length() + "/metrics".length() + 1); if (rest.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); int index = rest.indexOf("/"); String metricId = URIUtils.decodeComponent(index > 0 ? rest.substring(0, index) : rest); if (index < 0) return onMessageGetMetric(securityService, metricId); else if (rest.substring(index).equals("/snapshot")) return onMessageGetMetricValue(securityService, metricId); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { XSPReply reply = securityService.checkAction(ACTION_GET_LOG); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); StringBuilder builder = new StringBuilder("["); boolean first = true; for (PlatformLogMessage message : buffer.getMessages()) { if (!first) builder.append(", "); first = false; builder.append(message.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
/** * Responds to a request for the list of available metrics * * @param securityService The current security service * @return The metrics */ private HttpResponse onMessageGetMetricList(SecurityService securityService) { XSPReply reply = securityService.checkAction(ACTION_GET_METRICS); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); // get all the metrics boolean first = true; StringBuilder builder = new StringBuilder("["); for (Metric metric : getMetrics()) { if (!first) builder.append(", "); first = false; builder.append(metric.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); }
/** * Responds to a request for the values of a metric * * @param securityService The current security service * @param identifier The requested metric * @return The metrics' values */ private HttpResponse onMessageGetMetricValue(SecurityService securityService, String identifier) { XSPReply reply = securityService.checkAction(ACTION_POLL); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); MetricSnapshot value = pollMetric(identifier); if (value == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, value.serializedJSON()); }
/** * Responds to a request for the logout resource * * @param request The web API request to handle * @return The HTTP response */ private HttpResponse handleRequestLogout(HttpApiRequest request) { if (!HttpConstants.METHOD_POST.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); XSPReply reply = logout(); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); HttpResponse response = new HttpResponse(HttpURLConnection.HTTP_OK); response.addHeader(HttpConstants.HEADER_SET_COOKIE, AUTH_TOKEN + "= " + "; Max-Age=0" + "; Path=" + PlatformHttp.getUriPrefixApi() + "; Secure" + "; HttpOnly"); return response; }
/** * Responds to a request for the definition of a metric * * @param securityService The current security service * @param identifier The identifier of the requested metric * @return The metrics */ private HttpResponse onMessageGetMetric(SecurityService securityService, String identifier) { XSPReply reply = securityService.checkAction(ACTION_GET_METRICS); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); for (MetricProvider provider : Register.getComponents(MeasurableService.class)) { for (Metric metric : provider.getMetrics()) { if (metric.getIdentifier().equals(identifier)) return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, metric.serializedJSON()); } } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { if (request.getUri().equals(apiUri + "/login")) return handleRequestLogin(request); if (request.getUri().equals(apiUri + "/logout")) return handleRequestLogout(request); if (request.getUri().equals(apiUri + "/me")) return handleRequestMe(request); if (request.getUri().startsWith(apiUri + "/policy")) return handleRequestPolicy(request); if (request.getUri().startsWith(apiUri + "/users")) return handleRequestUsers(request); if (request.getUri().startsWith(apiUri + "/groups")) return handleRequestGroups(request); if (request.getUri().startsWith(apiUri + "/roles")) return handleRequestRoles(request); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
/** * Responds to the request to pull an artifact from a connector * When successful, this action creates the appropriate job and returns it. * * @param connectorId The identifier of the connector to delete * @return The response */ private HttpResponse onMessagePullFromConnector(String connectorId) { JobExecutionService executor = Register.getComponent(JobExecutionService.class); if (executor == null) return XSPReplyUtils.toHttpResponse(XSPReplyServiceUnavailable.instance(), null); Job job = new PullArtifactJob(connectorId); executor.schedule(job); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); }
@Override public HttpResponse handle(SecurityService securityService, HttpApiRequest request) { if (!HttpConstants.METHOD_POST.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); byte[] content = request.getContent(); if (content == null || content.length == 0) return XSPReplyUtils.toHttpResponse(new XSPReplyApiError(ERROR_FAILED_TO_READ_CONTENT), null); BufferedLogger logger = new BufferedLogger(); ASTNode root = JSONLDLoader.parseJSON(logger, new String(content, IOUtils.CHARSET)); if (root == null) return XSPReplyUtils.toHttpResponse(new XSPReplyApiError(ERROR_CONTENT_PARSING_FAILED, logger.getErrorsAsString()), null); return XSPReplyUtils.toHttpResponse(perform(new XOWLImpactAnalysisSetup(root)), null); }
/** * Responds to the request to push an artifact to a connector * When successful, this action creates the appropriate job and returns it. * * @param connectorId The identifier of the connector to delete * @param request The request to handle * @return The response */ private HttpResponse onMessagePushToConnector(String connectorId, HttpApiRequest request) { String artifact = request.getParameter("artifact"); if (artifact == null) return XSPReplyUtils.toHttpResponse(new XSPReplyApiError(ERROR_EXPECTED_QUERY_PARAMETERS, "'artifact'"), null); JobExecutionService executor = Register.getComponent(JobExecutionService.class); if (executor == null) return XSPReplyUtils.toHttpResponse(XSPReplyServiceUnavailable.instance(), null); Job job = new PushArtifactJob(connectorId, artifact); executor.schedule(job); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); } }