@Override public RawHttpResponse<Response> withHeaders(RawHttpHeaders headers) { return withHeaders(headers, true); }
@Override public RawHttpResponse<Response> withHeaders(RawHttpHeaders headers) { return withHeaders(headers, true); }
/** * Callback that will be called every time the server receives a HTTP request, but before it sends out a * HTTP response. * <p> * The actual response the client will see is the one returned by this method. * <p> * By default, this method adds "Date" and "Server" headers (the latter with the value of "RawHTTP"). * * @param request received by the server * @param response the server routed to. Normally, this callback should return this response with possibly * minor alterations. * @return the response the client should receive. Must not be null. * @throws IOException if an error occurs reading/writing HTTP messages */ default RawHttpResponse<Void> onResponse(RawHttpRequest request, RawHttpResponse<Void> response) throws IOException { return response.withHeaders(getCurrentDateHeader().and(SERVER_HEADER)); }
/** * Callback that will be called every time the server receives a HTTP request, but before it sends out a * HTTP response. * <p> * The actual response the client will see is the one returned by this method. * <p> * By default, this method adds "Date" and "Server" headers (the latter with the value of "RawHTTP"). * * @param request received by the server * @param response the server routed to. Normally, this callback should return this response with possibly * minor alterations. * @return the response the client should receive. Must not be null. * @throws IOException if an error occurs reading/writing HTTP messages */ default RawHttpResponse<Void> onResponse(RawHttpRequest request, RawHttpResponse<Void> response) throws IOException { return response.withHeaders(getCurrentDateHeader().and(SERVER_HEADER)); }
@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; }