@Override public boolean containsHeader(String key) { return header(key).isPresent(); }
private void assertDoesNotContainHeaders(HttpHeaders actual) { doesNotContain.stream().forEach(headerName -> { assertFalse(String.format("Header '%s' was expected to be absent", headerName), actual.getHeader(headerName).isPresent()); }); } }
private void assertDoesNotContainHeaders(HttpHeaders actual) { doesNotContain.stream().forEach(headerName -> { assertFalse(String.format("Header '%s' was expected to be absent", headerName), actual.getHeader(headerName).isPresent()); }); } }
@Override @JsonIgnore public String getHeader(String key) { HttpHeader header = header(key); if (header.isPresent()) { return header.firstValue(); } return null; }
@Override public boolean authenticate(Request request) { HttpHeader requestHeader = request.header(key); if (requestHeader == null || !requestHeader.isPresent()) { return false; } List<String> headerValues = requestHeader.values(); return request.containsHeader(AUTHORIZATION) && headerValues.contains(value); } }
public ContentTypeHeader getContentTypeHeader() { HttpHeader header = getHeader(ContentTypeHeader.KEY); if (header.isPresent()) { return new ContentTypeHeader(header.firstValue()); } return ContentTypeHeader.absent(); }
@Override public Body getBody() { try { byte[] bytes = ByteStreams.toByteArray(mPart.getInputStream()); HttpHeader header = getHeader(ContentTypeHeader.KEY); ContentTypeHeader contentTypeHeader = header.isPresent() ? new ContentTypeHeader(header.firstValue()) : ContentTypeHeader.absent(); return Body.ofBinaryOrText(bytes, contentTypeHeader); } catch (IOException e) { return throwUnchecked(e, Body.class); } }
private MultipartValuePattern valuePatternForPart(Request.Part part) { MultipartValuePatternBuilder builder = new MultipartValuePatternBuilder().withName(part.getName()).matchingType(MultipartValuePattern.MatchingType.ALL); if (!headersToMatch.isEmpty()) { Collection<HttpHeader> all = part.getHeaders().all(); for (HttpHeader httpHeader : all) { if (headersToMatch.contains(httpHeader.caseInsensitiveKey())) { builder.withHeader(httpHeader.key(), equalTo(httpHeader.firstValue())); } } } HttpHeader contentType = part.getHeader("Content-Type"); if (!contentType.isPresent() || contentType.firstValue().contains("text")) { builder.withBody(equalTo(part.getBody().asString())); } else if (contentType.firstValue().contains("json")) { builder.withBody(equalToJson(part.getBody().asString(), true, true)); } else if (contentType.firstValue().contains("xml")) { builder.withBody(equalToXml(part.getBody().asString())); } else { builder.withBody(binaryEqualTo(part.getBody().asBytes())); } return builder.build(); }