private void ensureRequestData() { if (requestData == null) { if (parent == null) { throw new IllegalStateException( "Neither another HttpRequest nor request data input stream available."); } else { requestData = parent.getData(); } } }
public static InputStream unzipStreamIfNeeded(final HttpRequest httpRequest) throws IOException { final String contentEncodingHeader = httpRequest.getHeader("content-encoding"); if ("gzip".equals(contentEncodingHeader)) { return new GZIPInputStream(httpRequest.getData()); } else { return httpRequest.getData(); } }
@Override public void close(CompletionHandler completionHandler) { if ( closed ) return; try { httpRequest.getData().close(); } catch (IOException e) {}; if (channel == null) channel = handleResponse(); try { channel.close(completionHandler); } catch (IllegalStateException e) { // Ignore: Known to be thrown when the other party closes } closed = true; }
private VespaXMLFeedReader.Operation createPutOperation(HttpRequest request, String id, String condition) { final VespaXMLFeedReader.Operation operationPut = singleDocumentParser.parsePut(request.getData(), id); if (condition != null && ! condition.isEmpty()) { operationPut.setCondition(new TestAndSetCondition(condition)); } return operationPut; }
private VespaXMLFeedReader.Operation createUpdateOperation(HttpRequest request, String id, String condition, Optional<Boolean> create) { final VespaXMLFeedReader.Operation operationUpdate = singleDocumentParser.parseUpdate(request.getData(), id); if (condition != null && ! condition.isEmpty()) { operationUpdate.getDocumentUpdate().setCondition(new TestAndSetCondition(condition)); } create.ifPresent(c -> operationUpdate.getDocumentUpdate().setCreateIfNonExistent(c)); return operationUpdate; }
private HttpResponse handlePATCH(HttpRequest request) { String path = request.getUri().getPath(); if (path.startsWith("/nodes/v2/node/")) { Node node = nodeFromRequest(request); nodeRepository.write(new NodePatcher(nodeFlavors, request.getData(), node, nodeRepository).apply()); return new MessageResponse("Updated " + node.hostname()); } else if (path.startsWith("/nodes/v2/upgrade/")) { return setTargetVersions(request); } throw new NotFoundException("Nothing at '" + path + "'"); }
byte[] byteArray = IOUtils.readBytes(request.getData(), 1 << 20); inspector = SlimeUtils.jsonToSlime(byteArray).get(); if (inspector.field("error_message").valid()){
private MessageResponse setTargetVersions(HttpRequest request) { NodeType nodeType = NodeType.valueOf(lastElement(request.getUri().getPath()).toLowerCase()); Inspector inspector = toSlime(request.getData()).get(); List<String> messageParts = new ArrayList<>(3); boolean force = inspector.field("force").asBool(); Inspector versionField = inspector.field("version"); Inspector osVersionField = inspector.field("osVersion"); if (versionField.valid()) { Version version = Version.fromString(versionField.asString()); maintenance.infrastructureVersions().setTargetVersion(nodeType, version, force); messageParts.add("version to " + version.toFullString()); } if (osVersionField.valid()) { String v = osVersionField.asString(); if (v.isEmpty()) { nodeRepository.osVersions().removeTarget(nodeType); messageParts.add("osVersion to null"); } else { Version osVersion = Version.fromString(v); nodeRepository.osVersions().setTarget(nodeType, osVersion, force); messageParts.add("osVersion to " + osVersion.toFullString()); } } if (messageParts.isEmpty()) { throw new IllegalArgumentException("At least one of 'version' and 'osVersion' must be set"); } return new MessageResponse("Set " + String.join(", ", messageParts) + " for nodes of type " + nodeType); }
private HttpResponse handlePOST(HttpRequest request) { Path path = new Path(request.getUri().getPath()); if (path.matches("/nodes/v2/command/restart")) { int restartCount = nodeRepository.restart(toNodeFilter(request)).size(); return new MessageResponse("Scheduled restart of " + restartCount + " matching nodes"); } if (path.matches("/nodes/v2/command/reboot")) { int rebootCount = nodeRepository.reboot(toNodeFilter(request)).size(); return new MessageResponse("Scheduled reboot of " + rebootCount + " matching nodes"); } if (path.matches("/nodes/v2/node")) { int addedNodes = addNodes(request.getData()); return new MessageResponse("Added " + addedNodes + " nodes to the provisioned state"); } if (path.matches("/nodes/v2/maintenance/inactive/{job}")) return setJobActive(path.get("job"), false); if (path.matches("/nodes/v2/flags/{flag}")) return setFlag(path.get("flag"), true, "", ""); if (path.matches("/nodes/v2/flags/{flag}/{dimension}/{value}")) return setFlag(path.get("flag"), true, path.get("dimension"), path.get("value")); if (path.matches("/nodes/v2/upgrade/firmware")) return requestFirmwareCheckResponse(); throw new NotFoundException("Nothing at path '" + request.getUri().getPath() + "'"); }