/** * Create new outbound message context. */ public OutboundMessageContext() { this.headers = HeaderUtils.createOutbound(); this.committingOutputStream = new CommittingOutputStream(); this.entityStream = committingOutputStream; }
/** * Closes the context. Flushes and closes the entity stream. */ public void close() { if (hasEntity()) { try { final OutputStream es = getEntityStream(); es.flush(); es.close(); } catch (IOException e) { // Happens when the client closed connection before receiving the full response. // This is OK and not interesting in vast majority of the cases // hence the log level set to FINE to make sure it does not flood the log unnecessarily // (especially for clients disconnecting from SSE listening, which is very common). Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } finally { // In case some of the output stream wrapper does not delegate close() call we // close the root stream manually to make sure it commits the data. if (!committingOutputStream.isClosed()) { try { committingOutputStream.close(); } catch (IOException e) { // Just log the exception Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } } } } } }
/** * Commits the {@link #getEntityStream() entity stream} if it wasn't already committed. * * @throws IOException in case of the IO error. */ public void commitStream() throws IOException { if (!committingOutputStream.isCommitted()) { entityStream.flush(); if (!committingOutputStream.isCommitted()) { committingOutputStream.commit(); committingOutputStream.flush(); } } }
/** * Commit the output stream. * * @throws IOException when underlying stream returned from the callback method throws the io exception. */ public void commit() throws IOException { flushBuffer(true); commitStream(); }
/** * Enable buffering of the serialized entity with the {@link #DEFAULT_BUFFER_SIZE default buffer size }. */ void enableBuffering() { enableBuffering(DEFAULT_BUFFER_SIZE); }
/** * Returns {@code true} if the entity stream has been committed. * * @return {@code true} if the entity stream has been committed. Otherwise returns {@code false}. */ public boolean isCommitted() { return committingOutputStream.isCommitted(); }
@Override public void close() throws IOException { if (isClosed) { return; } isClosed = true; if (streamProvider == null) { streamProvider = NULL_STREAM_PROVIDER; } commit(); adaptedOutput.close(); }
/** * Set a stream provider callback. * <p/> * This method must be called before first bytes are written to the {@link #getEntityStream() entity stream}. * * @param streamProvider non-{@code null} output stream provider. */ public void setStreamProvider(StreamProvider streamProvider) { committingOutputStream.setStreamProvider(streamProvider); }
private void commitStream() throws IOException { commitStream(-1); }
/** * Commit the output stream. * * @throws IOException when underlying stream returned from the callback method throws the io exception. */ public void commit() throws IOException { flushBuffer(true); commitStream(); }
/** * Enable buffering of the serialized entity with the {@link #DEFAULT_BUFFER_SIZE default buffer size }. */ void enableBuffering() { enableBuffering(DEFAULT_BUFFER_SIZE); }
/** * Returns {@code true} if the entity stream has been committed. * * @return {@code true} if the entity stream has been committed. Otherwise returns {@code false}. */ public boolean isCommitted() { return committingOutputStream.isCommitted(); }
@Override public void close() throws IOException { if (isClosed) { return; } isClosed = true; if (streamProvider == null) { streamProvider = NULL_STREAM_PROVIDER; } commit(); adaptedOutput.close(); }
/** * Set a stream provider callback. * <p/> * This method must be called before first bytes are written to the {@link #getEntityStream() entity stream}. * * @param streamProvider non-{@code null} output stream provider. */ public void setStreamProvider(StreamProvider streamProvider) { committingOutputStream.setStreamProvider(streamProvider); }
private void commitStream() throws IOException { commitStream(-1); }
@Override public void write(byte b[]) throws IOException { if (directWrite) { commitStream(); adaptedOutput.write(b); } else { if (b.length + buffer.size() > bufferSize) { flushBuffer(false); adaptedOutput.write(b); } else { buffer.write(b); } } }
/** * Commits the {@link #getEntityStream() entity stream} if it wasn't already committed. * * @throws IOException in case of the IO error. */ public void commitStream() throws IOException { if (!committingOutputStream.isCommitted()) { entityStream.flush(); if (!committingOutputStream.isCommitted()) { committingOutputStream.commit(); committingOutputStream.flush(); } } }
/** * Closes the context. Flushes and closes the entity stream. */ public void close() { if (hasEntity()) { try { final OutputStream es = getEntityStream(); es.flush(); es.close(); } catch (IOException e) { // Happens when the client closed connection before receiving the full response. // This is OK and not interesting in vast majority of the cases // hence the log level set to FINE to make sure it does not flood the log unnecessarily // (especially for clients disconnecting from SSE listening, which is very common). Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } finally { // In case some of the output stream wrapper does not delegate close() call we // close the root stream manually to make sure it commits the data. if (!committingOutputStream.isClosed()) { try { committingOutputStream.close(); } catch (IOException e) { // Just log the exception Logger.getLogger(OutboundMessageContext.class.getName()).log(Level.FINE, e.getMessage(), e); } } } } } }
/** * Enable a buffering of serialized entity. The buffering will be configured from configuration. The property * determining the size of the buffer is {@link CommonProperties#OUTBOUND_CONTENT_LENGTH_BUFFER}. * </p> * The buffering functionality is by default disabled and could be enabled by calling this method. In this case * this method must be called before first bytes are written to the {@link #getEntityStream() entity stream}. * * @param configuration runtime configuration. */ public void enableBuffering(Configuration configuration) { final Integer bufferSize = CommonProperties.getValue(configuration.getProperties(), configuration.getRuntimeType(), CommonProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, Integer.class); if (bufferSize != null) { committingOutputStream.enableBuffering(bufferSize); } else { committingOutputStream.enableBuffering(); } }
/** * Create new outbound message context. */ public OutboundMessageContext() { this.headers = HeaderUtils.createOutbound(); this.committingOutputStream = new CommittingOutputStream(); this.entityStream = committingOutputStream; }