/** * 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 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; }
/** * Outputs an API response to the servlet response * * @param servletResponse The servlet response to fill * @param apiResponse The API response */ private void doResponse(HttpServletResponse servletResponse, HttpResponse apiResponse) { servletResponse.setStatus(apiResponse.getCode()); for (Couple<String, String> header : apiResponse.getHeaders()) { servletResponse.addHeader(header.x, header.y); } if (apiResponse.getContentType() != null) servletResponse.setContentType(apiResponse.getContentType()); if (apiResponse.getBodyAsBytes() != null) { servletResponse.setContentLength(apiResponse.getBodyAsBytes().length); try (OutputStream os = servletResponse.getOutputStream()) { os.write(apiResponse.getBodyAsBytes()); os.flush(); } catch (IOException exception) { Logging.get().error(exception); } } }
/** * 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 the login resource * * @param request The web API request to handle * @return The HTTP response */ private HttpResponse handleRequestLogin(HttpApiRequest request) { if (!HttpConstants.METHOD_POST.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); String login = request.getParameter("login"); if (login == null) return XSPReplyUtils.toHttpResponse(new XSPReplyApiError(ERROR_EXPECTED_QUERY_PARAMETERS, "'login'"), null); String password = new String(request.getContent(), IOUtils.CHARSET); XSPReply reply = login(request.getClient(), login, password); if (!reply.isSuccess()) return XSPReplyUtils.toHttpResponse(reply, null); String token = ((XSPReplyResult<String>) reply).getData(); HttpResponse response = new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, getCurrentUser().serializedJSON()); response.addHeader(HttpConstants.HEADER_SET_COOKIE, AUTH_TOKEN + "=" + token + "; Max-Age=" + Long.toString(securityTokenTTL) + "; Path=" + PlatformHttp.getUriPrefixApi() + "; Secure" + "; HttpOnly"); return response; }
/** * 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 + "/services")) { StringBuilder builder = new StringBuilder("["); boolean first = true; for (HttpApiService service : getServices()) { if (!first) builder.append(", "); first = false; builder.append(service.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); } else if (request.getUri().equals(apiUri + "/resources")) { StringBuilder builder = new StringBuilder("["); boolean first = true; for (HttpApiResource resource : getResources()) { if (!first) builder.append(", "); first = false; builder.append(resource.serializedJSON()); } builder.append("]"); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
@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()); }
/** * Handles a request for the manifest resource * * @param request The request * @return The response */ private HttpResponse handleManifest(HttpApiRequest request) { if (request.getUri().equals(apiUri + "/manifest")) { 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, manifest.serializedJSON()); } String rest = request.getUri().substring(apiUri.length() + "/manifest".length()); if (rest.startsWith("/inputs")) return handleManifestInputs(request); else if (rest.startsWith("/outputs")) return handleManifestOutputs(request); else if (rest.startsWith("/roles")) return handleManifestRoles(request); else if (rest.equals("/pattern")) { 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, getCollaborationPattern().serializedJSON()); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
if (request.getUri().equals(apiUri)) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); return onRequestJobs(); String rest = request.getUri().substring(apiUri.length() + 1); if (rest.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); int index = rest.indexOf("/"); String jobId = URIUtils.decodeComponent(index > 0 ? rest.substring(0, index) : rest); if (index < 0) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); Job job = getJob(jobId, JobStatus.Completed); if (job == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); } else if (rest.substring(index).equals("/cancel")) { if (!HttpConstants.METHOD_POST.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); Job job = getJob(jobId, JobStatus.Completed); if (job == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return XSPReplyUtils.toHttpResponse(cancel(job), null); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND);
if (rest.equals("artifacts")) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); boolean first = true; StringBuilder builder = new StringBuilder("["); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, builder.toString()); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); String artifactId = URIUtils.decodeComponent(rest.substring(1)); if (artifactId.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return XSPReplyUtils.toHttpResponse(unregisterInput(specId, artifactId), null); return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected methods: PUT, DELETE");
@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 + "/archetypes")) { return onGetArchetypes(); } else if (request.getUri().equals(apiUri + "/schemas")) { return onGetSchemas(); } else if (request.getUri().startsWith(apiUri + "/archetypes")) { String rest = URIUtils.decodeComponent(request.getUri().substring(apiUri.length() + "/archetypes".length() + 1)); if (rest.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return onGetArchetype(rest); } else if (request.getUri().startsWith(apiUri + "/schemas")) { String rest = URIUtils.decodeComponent(request.getUri().substring(apiUri.length() + "/schemas".length() + 1)); if (rest.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return onGetSchema(rest); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }