/** * Start accepting a potentially lazy, infinite stream of chunks (representing messages) * to establish duplex communications as a server. * * @param incomingMessageStream stream of messages being received from a client. * @param createHandler callback that takes a message sender that can be used to send out messages, and returns a * message handler receives messages from the remote. * @return response to be sent to the client to initiate duplex communication. */ public RawHttpResponse<Void> accept(Iterator<Chunk> incomingMessageStream, Function<MessageSender, MessageHandler> createHandler) { MessageSender sender = new MessageSender(options.createMessageQueue()); MessageHandler handler = createHandler.apply(sender); startMessageLoop(incomingMessageStream, sender, handler); return okResponse.withBody(new StreamedChunkedBody(sender.getChunkStream())); }
@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; }