@Override protected SessionOutputBuffer createSessionOutputBuffer( final Socket socket, int buffersize, final HttpParams params) throws IOException { if (buffersize == -1) { buffersize = 8192; } SessionOutputBuffer outbuffer = super.createSessionOutputBuffer( socket, buffersize, params); if (wireLog.isDebugEnabled()) { outbuffer = new LoggingSessionOutputBuffer( outbuffer, new Wire(wireLog), HttpProtocolParams.getHttpElementCharset(params)); } return outbuffer; }
/** * Initializes this connection object with {@link SessionInputBuffer} and * {@link SessionOutputBuffer} instances to be used for sending and * receiving data. These session buffers can be bound to any arbitrary * physical output medium. * <p> * This method will invoke {@link #createHttpResponseFactory()}, * {@link #createRequestWriter(SessionOutputBuffer, HttpParams)} * and {@link #createResponseParser(SessionInputBuffer, HttpResponseFactory, HttpParams)} * methods to initialize HTTP request writer and response parser for this * connection. * * @param inbuffer the session input buffer. * @param outbuffer the session output buffer. * @param params HTTP parameters. */ protected void init( final SessionInputBuffer inbuffer, final SessionOutputBuffer outbuffer, final HttpParams params) { if (inbuffer == null) { throw new IllegalArgumentException("Input session buffer may not be null"); } if (outbuffer == null) { throw new IllegalArgumentException("Output session buffer may not be null"); } this.inbuffer = inbuffer; this.outbuffer = outbuffer; if (inbuffer instanceof EofSensor) { this.eofSensor = (EofSensor) inbuffer; } this.responseParser = createResponseParser( inbuffer, createHttpResponseFactory(), params); this.requestWriter = createRequestWriter( outbuffer, params); this.metrics = createConnectionMetrics( inbuffer.getMetrics(), outbuffer.getMetrics()); }
/** * Writes out the content of the given HTTP entity to the session output * buffer based on properties of the given HTTP message. * * @param outbuffer the output session buffer. * @param message the HTTP message. * @param entity the HTTP entity to be written out. * @throws HttpException in case of HTTP protocol violation. * @throws IOException in case of an I/O error. */ public void serialize( final SessionOutputBuffer outbuffer, final HttpMessage message, final HttpEntity entity) throws HttpException, IOException { if (outbuffer == null) { throw new IllegalArgumentException("Session output buffer may not be null"); } if (message == null) { throw new IllegalArgumentException("HTTP message may not be null"); } if (entity == null) { throw new IllegalArgumentException("HTTP entity may not be null"); } OutputStream outstream = doSerialize(outbuffer, message); entity.writeTo(outstream); outstream.close(); }
/** * Initializes this connection object with {@link SessionInputBuffer} and * {@link SessionOutputBuffer} instances to be used for sending and * receiving data. These session buffers can be bound to any arbitrary * physical output medium. * <p> * This method will invoke {@link #createHttpRequestFactory}, * {@link #createRequestParser(SessionInputBuffer, HttpRequestFactory, HttpParams)} * and {@link #createResponseWriter(SessionOutputBuffer, HttpParams)} * methods to initialize HTTP request parser and response writer for this * connection. * * @param inbuffer the session input buffer. * @param outbuffer the session output buffer. * @param params HTTP parameters. */ protected void init( final SessionInputBuffer inbuffer, final SessionOutputBuffer outbuffer, final HttpParams params) { if (inbuffer == null) { throw new IllegalArgumentException("Input session buffer may not be null"); } if (outbuffer == null) { throw new IllegalArgumentException("Output session buffer may not be null"); } this.inbuffer = inbuffer; this.outbuffer = outbuffer; if (inbuffer instanceof EofSensor) { this.eofSensor = (EofSensor) inbuffer; } this.requestParser = createRequestParser( inbuffer, createHttpRequestFactory(), params); this.responseWriter = createResponseWriter( outbuffer, params); this.metrics = createConnectionMetrics( inbuffer.getMetrics(), outbuffer.getMetrics()); }
@Override protected SessionOutputBuffer createSessionOutputBuffer(final Socket socket, int buffersize, final HttpParams params) throws IOException { if (buffersize == -1) { buffersize = 8192; } SessionOutputBuffer outbuffer = super.createSessionOutputBuffer(socket, buffersize, params); if (wireLog.isDebugEnabled()) { outbuffer = new LoggingSessionOutputBuffer(outbuffer, new Wire(wireLog), HttpProtocolParams.getHttpElementCharset(params)); } return outbuffer; }
@Override protected SessionOutputBuffer createSessionOutputBuffer( final Socket socket, final int buffersize, final HttpParams params) throws IOException { SessionOutputBuffer outbuffer = super.createSessionOutputBuffer( socket, buffersize > 0 ? buffersize : 8192, params); if (wireLog.isDebugEnabled()) { outbuffer = new LoggingSessionOutputBuffer( outbuffer, new Wire(wireLog), HttpProtocolParams.getHttpElementCharset(params)); } return outbuffer; }
@Override protected SessionOutputBuffer createSessionOutputBuffer( final Socket socket, int buffersize, final HttpParams params) throws IOException { if (buffersize == -1) { buffersize = 8192; } SessionOutputBuffer outbuffer = super.createSessionOutputBuffer( socket, buffersize, params); if (wireLog.isDebugEnabled()) { outbuffer = new LoggingSessionOutputBuffer(outbuffer, new Wire(wireLog)); } return outbuffer; }
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; } } }
/** * Create an instance that wraps the specified session output buffer. * @param out The session output buffer. * @param wire The Wire log to use. * @param charset protocol charset, <code>ASCII</code> if <code>null</code> */ public LoggingSessionOutputBuffer( final SessionOutputBuffer out, final Wire wire, final String charset) { super(); this.out = out; this.wire = wire; this.charset = charset != null ? charset : Consts.ASCII.name(); }
/** * Wraps a session output buffer and cuts off output after a defined number * of bytes. * * @param out The session output buffer * @param contentLength The maximum number of bytes that can be written to * the stream. Subsequent write operations will be ignored. * * @since 4.0 */ public ContentLengthOutputStream(final SessionOutputBuffer out, long contentLength) { super(); if (out == null) { throw new IllegalArgumentException("Session output buffer may not be null"); } if (contentLength < 0) { throw new IllegalArgumentException("Content length may not be negative"); } this.out = out; this.contentLength = contentLength; }
/** * Creates an instance of AbstractMessageWriter. * * @param buffer the session output buffer. * @param formatter the line formatter. * @param params HTTP parameters. */ public AbstractMessageWriter(final SessionOutputBuffer buffer, final LineFormatter formatter, final HttpParams params) { super(); if (buffer == null) { throw new IllegalArgumentException("Session input buffer may not be null"); } this.sessionBuffer = buffer; this.lineBuf = new CharArrayBuffer(128); this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.DEFAULT; }
public IdentityOutputStream(final SessionOutputBuffer out) { super(); if (out == null) { throw new IllegalArgumentException("Session output buffer may not be null"); } this.out = out; }
protected OutputStream createOutputStream( final long len, final SessionOutputBuffer outbuffer) { if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedOutputStreamHC4(2048, outbuffer); } else if (len == ContentLengthStrategy.IDENTITY) { return new IdentityOutputStreamHC4(outbuffer); } else { return new ContentLengthOutputStreamHC4(outbuffer, len); } }
/** * Creates an instance of AbstractMessageWriterHC4. * * @param buffer the session output buffer. * @param formatter the line formatter. * @param params HTTP parameters. * * @deprecated (4.3) use * {@link AbstractMessageWriterHC4#AbstractMessageWriterHC4(SessionOutputBuffer, LineFormatter)} */ @Deprecated public AbstractMessageWriterHC4(final SessionOutputBuffer buffer, final LineFormatter formatter, final HttpParams params) { super(); Args.notNull(buffer, "Session input buffer"); this.sessionBuffer = buffer; this.lineBuf = new CharArrayBuffer(128); this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatterHC4.INSTANCE; }
@Override protected OutputStream createOutputStream(final long len, final SessionOutputBuffer outbuffer) { if (len == ContentLengthStrategy.CHUNKED) { return new ChunkedOutputStream(chunkSize, outbuffer); } return super.createOutputStream(len, outbuffer); }
/** * Create an instance that wraps the specified session output buffer. * @param out The session output buffer. * @param wire The Wire log to use. * @param charset protocol charset, {@code ASCII} if {@code null} */ public LoggingSessionOutputBuffer( final SessionOutputBuffer out, final Wire wire, final String charset) { super(); this.out = out; this.wire = wire; this.charset = charset != null ? charset : Consts.ASCII.name(); }
@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 LoggingSessionOutputBuffer(final SessionOutputBuffer out, final Wire wire) { this(out, wire, null); }
public HttpResponseWriter(final SessionOutputBuffer buffer, final LineFormatter formatter, final HttpParams params) { super(buffer, formatter, params); }
public HttpRequestWriter(final SessionOutputBuffer buffer, final LineFormatter formatter, final HttpParams params) { super(buffer, formatter, params); }
protected SessionOutputBuffer getSessionOutputBuffer() { return this.outbuffer; }
public IdentityOutputStreamHC4(final SessionOutputBuffer out) { super(); this.out = Args.notNull(out, "Session output buffer"); }
public HttpMessageWriter create(final SessionOutputBuffer buffer) { return new DefaultHttpRequestWriter(buffer, lineFormatter); }
public DefaultHttpResponseWriter(final SessionOutputBuffer buffer) { super(buffer, null); }
public DefaultHttpRequestWriter(final SessionOutputBuffer buffer) { this(buffer, null); }
public HttpMessageWriter create(final SessionOutputBuffer buffer) { return new DefaultHttpResponseWriter(buffer, lineFormatter); }
/** * Create an instance that wraps the specified session output buffer. * @param out The session output buffer. * @param wire The Wire log to use. */ public LoggingSessionOutputBuffer(final SessionOutputBuffer out, final Wire wire) { super(); this.out = out; this.wire = wire; }