@Override public LazyBodyReader toBodyReader() { try { return new LazyBodyReader( new FramedBody.ContentLength(getBodyDecoder(), file.length()), new BufferedInputStream(Files.newInputStream(file.toPath()))); } catch (IOException e) { throw new RuntimeException(e); } }
@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; }
File file = ((FileBody) requestBody).getFile(); if (file.isDirectory()) { return new CliError(ErrorCode.UNEXPECTED_ERROR, "Request body file cannot be a directory: " + file);
@Test(expected = FileNotFoundException.class) public void replacingBodyWithFile() throws Throwable { RawHttp http = new RawHttp(); RawHttpRequest request = http.parseRequest("POST http://example.com/hello"); try { RawHttpRequest requestWithBody = request.withBody( new FileBody(new File("hello.request"), "text/plain")); System.out.println(requestWithBody.eagerly()); } catch (RuntimeException e) { throw e.getCause(); } }
@Override public LazyBodyReader toBodyReader() { try { return new LazyBodyReader( new FramedBody.ContentLength(getBodyDecoder(), file.length()), new BufferedInputStream(Files.newInputStream(file.toPath()))); } catch (IOException e) { throw new RuntimeException(e); } }