@Override public String[] getParameterAll(String name) { return URIUtils.getMultiParameters(request.getParameterValues(name)); }
@Override public String getParameter(String name) { return URIUtils.getSingleParameter(request.getParameterValues(name)); }
/** * 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()); }
@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); }
/** * Gets the resource URL for the requested uri * * @param uri The uri of a resource * @return The corresponding URL, or null if there is none */ private URL doGetResource(String uri) { if (uri.endsWith("/")) uri += "index.html"; List<String> segments = URIUtils.getSegments(uri); if (contributions == null) { synchronized (this) { if (contributions == null) { contributions = Register.getComponents(WebUIContribution.class); for (WebUIContribution contribution : contributions) register(contribution); } } } return doGetResource(root, segments, 0, uri); }
@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); }
/** * 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); } }
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);
/** * 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); }
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) { 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"); }
if (request.getUri().equals(apiUri + "/descriptors")) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); return onMessageListDescriptors(); } else if (request.getUri().equals(apiUri + "/connectors")) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); return onMessageListConnectors(); } else if (request.getUri().startsWith(apiUri + "/connectors")) { String rest = request.getUri().substring(apiUri.length() + "/connectors".length() + 1); if (rest.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); int index = rest.indexOf("/"); String connectorId = URIUtils.decodeComponent(index > 0 ? rest.substring(0, index) : rest); if (index < 0) { switch (request.getMethod()) { return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected methods: GET, PUT, DELETE"); case "/pull": { if (!HttpConstants.METHOD_POST.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); return onMessagePullFromConnector(connectorId); return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected POST method"); return onMessagePushToConnector(connectorId, request); return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND);
/** * 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()); }
/** * Responds to a request for the policy resource * * @param request The web API request to handle * @return The HTTP response */ private HttpResponse handleRequestPolicy(HttpApiRequest request) { if (request.getUri().equals(apiUri + "/policy")) { if (!HttpConstants.METHOD_GET.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected GET method"); return XSPReplyUtils.toHttpResponse(getPolicy().getConfiguration(), null); } if (request.getUri().startsWith(apiUri + "/policy/actions/")) { String rest = request.getUri().substring(apiUri.length() + "/policy/actions/".length()); String actionId = URIUtils.decodeComponent(rest); if (actionId.isEmpty()) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); if (!HttpConstants.METHOD_PUT.equals(request.getMethod())) return new HttpResponse(HttpURLConnection.HTTP_BAD_METHOD, HttpConstants.MIME_TEXT_PLAIN, "Expected PUT method"); String definition = new String(request.getContent(), IOUtils.CHARSET); return XSPReplyUtils.toHttpResponse(getPolicy().setPolicy(actionId, definition), null); } return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); }
/** * 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 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 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()); }
/** * 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()); }
/** * 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()); }
@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); }