/** * Returns an {@code OperationPreprocessor} that will pretty print the content of the * request or response. * @return the preprocessor */ public static OperationPreprocessor prettyPrint() { return new ContentModifyingOperationPreprocessor( new PrettyPrintingContentModifier()); }
@Test public void modifyRequestContent() { OperationRequest request = this.requestFactory.create( URI.create("http://localhost"), HttpMethod.GET, "content".getBytes(), new HttpHeaders(), new Parameters(), Collections.<OperationRequestPart>emptyList()); OperationRequest preprocessed = this.preprocessor.preprocess(request); assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes()); }
@Test public void modifyResponseContent() { OperationResponse response = this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), "content".getBytes()); OperationResponse preprocessed = this.preprocessor.preprocess(response); assertThat(preprocessed.getContent()).isEqualTo("modified".getBytes()); }
/** * Returns an {@code OperationPreprocessor} that will mask the href of hypermedia * links in the request or response. * @return the preprocessor */ public static OperationPreprocessor maskLinks() { return new ContentModifyingOperationPreprocessor( new LinkMaskingContentModifier()); }
@Test public void contentLengthIsUpdated() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentLength(7); OperationRequest request = this.requestFactory.create( URI.create("http://localhost"), HttpMethod.GET, "content".getBytes(), httpHeaders, new Parameters(), Collections.<OperationRequestPart>emptyList()); OperationRequest preprocessed = this.preprocessor.preprocess(request); assertThat(preprocessed.getHeaders().getContentLength()).isEqualTo(8L); }
/** * Returns an {@code OperationPreprocessor} that will mask the href of hypermedia * links in the request or response. * @param mask the link mask * @return the preprocessor */ public static OperationPreprocessor maskLinks(String mask) { return new ContentModifyingOperationPreprocessor( new LinkMaskingContentModifier(mask)); }
/** * Returns an {@code OperationPreprocessor} that will modify the content of the * request or response by replacing occurrences of the given {@code pattern} with the * given {@code replacement}. * @param pattern the pattern * @param replacement the replacement * @return the preprocessor */ public static OperationPreprocessor replacePattern(Pattern pattern, String replacement) { return new ContentModifyingOperationPreprocessor( new PatternReplacingContentModifier(pattern, replacement)); }
private OperationPreprocessor removeCompany() { return new ContentModifyingOperationPreprocessor(new ContentModifier() { @Override public byte[] modifyContent(byte[] originalContent, MediaType contentType) { ObjectMapper objectMapper = new ObjectMapper(); try { Map<?, ?> map = objectMapper.readValue(originalContent, Map.class); map.remove("companyName"); return objectMapper.writeValueAsBytes(map); } catch (IOException ex) { return originalContent; } } }); }