private void acceptClient(@NotNull Socket client) throws IOException { final SessionInputBuffer inputBuffer = wrapInputStream(client.getInputStream()); final HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inputBuffer, new BasicLineParser(), new DefaultHttpRequestFactory(), MessageConstraints.DEFAULT ); final SessionOutputBuffer outputBuffer = wrapOutputStream(client.getOutputStream()); final HttpMessageWriter<HttpResponse> writer = new DefaultHttpResponseWriter(outputBuffer); while (!socket.isClosed()) { try { service(inputBuffer, outputBuffer, parser, writer); } catch (ConnectionClosedException ignored) { break; } catch (HttpException e) { log.error(e.getMessage(), e); break; } } }
@SuppressWarnings("deprecation") protected void service(@NotNull SessionInputBuffer inputBuffer, @NotNull SessionOutputBuffer outputBuffer, @NotNull HttpMessageParser<HttpRequest> parser, @NotNull HttpMessageWriter<HttpResponse> writer) throws IOException, HttpException { try { final HttpRequest request = parser.parse(); final HttpEntity entity; if (request instanceof HttpEntityEnclosingRequest) { final EntityDeserializer deserializer = new EntityDeserializer(new LaxContentLengthStrategy()); entity = deserializer.deserialize(inputBuffer, request); ((HttpEntityEnclosingRequest) request).setEntity(entity); } else { entity = null; } final HttpResponse response = service(request); if (entity != null) { entity.getContent().close(); } if (response.getEntity() != null) { response.addHeader(HttpHeaders.CONTENT_LENGTH, Long.toString(response.getEntity().getContentLength())); response.addHeader(response.getEntity().getContentType()); response.addHeader(response.getEntity().getContentEncoding()); } response.setHeader(HttpHeaders.SERVER, "Protobuf RPC"); writer.write(response); if (response.getEntity() != null) { final EntitySerializer serializer = new EntitySerializer(new LaxContentLengthStrategy()); serializer.serialize(outputBuffer, response, response.getEntity()); } } finally { outputBuffer.flush(); } }
public HttpMessageWriter create(final SessionOutputBuffer buffer) { return new DefaultHttpRequestWriter(buffer, lineFormatter); }
public HttpMessageWriter create(final SessionOutputBuffer buffer) { return new DefaultHttpResponseWriter(buffer, lineFormatter); }
/** * Creates an instance of {@link HttpMessageWriter} to be used for * writing out HTTP requests sent over this connection. * <p> * This method can be overridden in a super class in order to provide * a different implementation of the {@link HttpMessageWriter} interface or * to pass a different implementation of {@link LineFormatter} to the * the default implementation {@link HttpRequestWriter}. * * @param buffer the session output buffer * @param params HTTP parameters * @return HTTP message writer */ protected HttpMessageWriter<HttpRequest> createRequestWriter( final SessionOutputBuffer buffer, final HttpParams params) { return new HttpRequestWriter(buffer, null, params); }
/** * Creates an instance of {@link HttpMessageWriter} to be used for * writing out HTTP responses sent over this connection. * <p> * This method can be overridden in a super class in order to provide * a different implementation of the {@link HttpMessageWriter} interface or * to pass a different implementation of {@link LineFormatter} to the * the default implementation {@link HttpResponseWriter}. * * @param buffer the session output buffer * @param params HTTP parameters * @return HTTP message writer */ protected HttpMessageWriter<HttpResponse> createResponseWriter( final SessionOutputBuffer buffer, final HttpParams params) { return new HttpResponseWriter(buffer, null, params); }