@Override protected Map<String, Object> createModel(Operation operation) { try { Object object = this.objectMapper.readValue( operation.getResponse().getContentAsString(), Object.class); Object field = this.fieldProcessor.extract(JsonFieldPath.compile(this.path), object); if (field instanceof List && this.index != null) { field = ((List<?>) field).get(this.index); } return Collections.singletonMap("value", this.objectMapper.writeValueAsString(field)); } catch (Exception ex) { throw new IllegalStateException(ex); } }
private HttpHeaders augmentHeaders(HttpHeaders originalHeaders, URI uri, byte[] content) { return new HttpHeadersHelper(originalHeaders) .addIfAbsent(HttpHeaders.HOST, createHostHeader(uri)) .setContentLengthHeader(content).getHeaders(); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newContent}. If the original request had a {@code Content-Length} * header it will be modified to match the length of the new content. * @param original the original request * @param newContent the new content * @return the new request with the new content */ public OperationRequest createFrom(OperationRequest original, byte[] newContent) { return new StandardOperationRequest(original.getUri(), original.getMethod(), newContent, getUpdatedHeaders(original.getHeaders(), newContent), original.getParameters(), original.getParts(), original.getCookies()); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newHeaders}. * @param original the original request * @param newHeaders the new headers * @return the new request with the new headers */ public OperationRequest createFrom(OperationRequest original, HttpHeaders newHeaders) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), newHeaders, original.getParameters(), original.getParts(), original.getCookies()); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newParameters}. * @param original the original request * @param newParameters the new parameters * @return the new request with the new parameters */ public OperationRequest createFrom(OperationRequest original, Parameters newParameters) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), original.getHeaders(), newParameters, original.getParts(), original.getCookies()); }
@Override public OperationRequest preprocess(OperationRequest request) { Parameters parameters = new Parameters(); parameters.putAll(request.getParameters()); for (Modification modification : this.modifications) { modification.apply(parameters); } return this.requestFactory.createFrom(request, parameters); }
/** * Returns a new {@code Parameters} containing only the parameters that do no appear * in the query string of the given {@code uri}. * @param uri the uri * @return the unique parameters */ public Parameters getUniqueParameters(URI uri) { Parameters queryStringParameters = new QueryStringParser().parse(uri); Parameters uniqueParameters = new Parameters(); for (Map.Entry<String, List<String>> parameter : entrySet()) { addIfUnique(parameter, queryStringParameters, uniqueParameters); } return uniqueParameters; }
private OperationRequestPart findPart(Operation operation) { for (OperationRequestPart candidate : operation.getRequest().getParts()) { if (candidate.getName().equals(this.partName)) { return candidate; } } throw new SnippetException("A request part named '" + this.partName + "' was not found in the request"); }
/** * Creates a new {@code OperationResponse} based on the given {@code original} but * with the given {@code newContent}. If the original response had a * {@code Content-Length} header it will be modified to match the length of the new * content. * @param original the original response * @param newContent the new content * @return the new response with the new content */ public OperationResponse createFrom(OperationResponse original, byte[] newContent) { return new StandardOperationResponse(original.getStatus(), getUpdatedHeaders(original.getHeaders(), newContent), newContent); }
@Override protected Set<String> extractActualParameters(Operation operation) { return operation.getRequest().getParameters().keySet(); }
@Override public OperationRequest preprocess(OperationRequest request) { return this.requestFactory.createFrom(request, removeHeaders(request.getHeaders())); }
/** * Creates a new {@code OperationResponse} based on the given {@code original} but * with the given {@code newHeaders}. * @param original the original response * @param newHeaders the new headers * @return the new response with the new headers */ public OperationResponse createFrom(OperationResponse original, HttpHeaders newHeaders) { return new StandardOperationResponse(original.getStatus(), newHeaders, original.getContent()); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getRequest().getContent(); }
@Override public void document(Operation operation) throws IOException { RestDocumentationContext context = (RestDocumentationContext) operation .getAttributes().get(RestDocumentationContext.class.getName()); WriterResolver writerResolver = (WriterResolver) operation.getAttributes() .get(WriterResolver.class.getName()); try (Writer writer = writerResolver.resolve( operation.getName() + "/" + getSnippetName(), this.file, context)) { Map<String, Object> model = createModel(operation); model.putAll(getAttributes()); TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes() .get(TemplateEngine.class.getName()); writer.append(templateEngine.compileTemplate(getSnippetName()).render(model)); } }
private HttpHeaders getUpdatedHeaders(HttpHeaders originalHeaders, byte[] updatedContent) { return new HttpHeadersHelper(originalHeaders) .updateContentLengthHeaderIfPresent(updatedContent).getHeaders(); }
/** * Creates a new {@link OperationResponse}. If the response has any content, the given * {@code headers} will be augmented to ensure that they include a * {@code Content-Length} header. * @param status the status of the response * @param headers the request's headers * @param content the content of the request * @return the {@code OperationResponse} */ public OperationResponse create(HttpStatus status, HttpHeaders headers, byte[] content) { return new StandardOperationResponse(status, augmentHeaders(headers, content), content); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getRequest().getContent(); }
private HttpHeaders getUpdatedHeaders(HttpHeaders originalHeaders, byte[] updatedContent) { return new HttpHeadersHelper(originalHeaders) .updateContentLengthHeaderIfPresent(updatedContent).getHeaders(); }