/** * @param headerName case-insensitive header name * @return the first value of the header, if any. */ public Optional<String> getFirst(String headerName) { List<String> values = get(headerName); if (values.isEmpty()) { return Optional.empty(); } else { return Optional.of(values.get(0)); } }
public RawHttpResponse<CloseableHttpResponse> send(RawHttpRequest request) throws IOException { RequestBuilder builder = RequestBuilder.create(request.getMethod()); builder.setUri(request.getUri()); builder.setVersion(toProtocolVersion(request.getStartLine().getHttpVersion())); request.getHeaders().getHeaderNames().forEach((name) -> request.getHeaders().get(name).forEach(value -> builder.addHeader(new BasicHeader(name, value)))); request.getBody().ifPresent(b -> builder.setEntity(new InputStreamEntity(b.asRawStream()))); CloseableHttpResponse response = httpClient.execute(builder.build()); RawHttpHeaders headers = readHeaders(response); StatusLine statusLine = adaptStatus(response.getStatusLine()); @Nullable LazyBodyReader body; if (response.getEntity() != null) { FramedBody framedBody = http.getFramedBody(statusLine, headers); body = new LazyBodyReader(framedBody, response.getEntity().getContent()); } else { body = null; } return new RawHttpResponse<>(response, request, statusLine, headers, body); }
response.getHeaders().get("Content-Type"));
/** * @param headerName case-insensitive header name * @return the first value of the header, if any. */ public Optional<String> getFirst(String headerName) { List<String> values = get(headerName); if (values.isEmpty()) { return Optional.empty(); } else { return Optional.of(values.get(0)); } }
List<String> encodings = headers.get("Transfer-Encoding"); BodyDecoder bodyDecoder = new BodyDecoder(options.getEncodingRegistry(), encodings); return new FramedBody.Chunked(bodyDecoder, metadataParser); List<String> lengthValues = headers.get("Content-Length"); if (lengthValues.isEmpty()) { if (startLine instanceof StatusLine) {
List<String> encodings = headers.get("Transfer-Encoding"); BodyDecoder bodyDecoder = new BodyDecoder(options.getEncodingRegistry(), encodings); return new FramedBody.Chunked(bodyDecoder, metadataParser); List<String> lengthValues = headers.get("Content-Length"); if (lengthValues.isEmpty()) { if (startLine instanceof StatusLine) {
@Test public void canFindFileExactMatchWithExtensionInSubDirectory() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello.json", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.json").toFile(), result.get().file); assertEquals(singletonList("application/json"), result.get().contentTypeHeader.get("Content-Type")); result = fileLocator.find("p1/hello.xml", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.xml").toFile(), result.get().file); assertEquals(singletonList("text/xml"), result.get().contentTypeHeader.get("Content-Type")); }
@Test public void resourceIsReturnedIfAvailableEvenIfNotMatchingAcceptHeader() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", singletonList("image/gif")); assertTrue(result.isPresent()); // either JSON or XML file could be returned File jsonFile = rootDir.resolve("p1/hello.json").toFile(); File xmlFile = rootDir.resolve("p1/hello.xml").toFile(); File actualFile = result.get().file; if (actualFile.equals(jsonFile)) { assertEquals(singletonList("application/json"), result.get().contentTypeHeader.get("Content-Type")); } else if (actualFile.equals(xmlFile)) { assertEquals(singletonList("text/xml"), result.get().contentTypeHeader.get("Content-Type")); } else { fail("Found file is not as expected: " + actualFile); } }
@Test public void canFindFileWithNonExactMatch() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", emptyList()); assertTrue(result.isPresent()); // either JSON or XML file could be returned File jsonFile = rootDir.resolve("p1/hello.json").toFile(); File xmlFile = rootDir.resolve("p1/hello.xml").toFile(); File actualFile = result.get().file; if (actualFile.equals(jsonFile)) { assertEquals(singletonList("application/json"), result.get().contentTypeHeader.get("Content-Type")); } else if (actualFile.equals(xmlFile)) { assertEquals(singletonList("text/xml"), result.get().contentTypeHeader.get("Content-Type")); } else { fail("Found file is not as expected: " + actualFile); } }
@Test public void canFindFileUsingAcceptHeader() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", singletonList("application/json")); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.json").toFile(), result.get().file); assertEquals(singletonList("application/json"), result.get().contentTypeHeader.get("Content-Type")); result = fileLocator.find("p1/hello", singletonList("text/xml")); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.xml").toFile(), result.get().file); assertEquals(singletonList("text/xml"), result.get().contentTypeHeader.get("Content-Type")); }
@Test public void canFindFileExactMatch() { Optional<FileLocator.FileResult> result = fileLocator.find("hello", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("hello").toFile(), result.get().file); assertEquals(singletonList("application/octet-stream"), result.get().contentTypeHeader.get("Content-Type")); }
@Test public void canFindFileExactMatchInSubDirectory() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/p2/hello", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/p2/hello").toFile(), result.get().file); assertEquals(singletonList("application/octet-stream"), result.get().contentTypeHeader.get("Content-Type")); }
@Override public Optional<RawHttpResponse<?>> route(RawHttpRequest request) { final Optional<RawHttpResponse<?>> response; if (request.getMethod().equals("GET")) { String path = request.getStartLine().getUri() .normalize().getPath() .replaceAll(DIR_BACK_PATTERN, ""); // provide the index.html file at the root path if (path.isEmpty() || path.equals("/")) { path = "index.html"; } Optional<FileResult> resource = fileLocator.find(path, request.getHeaders().get("Accept")); response = resource.map(fileResult -> HttpResponses.getOkResponse(request.getStartLine().getHttpVersion()) .withHeaders(fileResult.contentTypeHeader) .withBody(new FileBody(fileResult.file))); } else { response = Optional.of(HttpResponses.getMethodNotAllowedResponse(request.getStartLine().getHttpVersion())); } return response; }