responseWriterFactory) {
super(session, buffersize, fragmentSizeHint, allocator, chardecoder, charencoder,
incomingContentStrategy != null ? incomingContentStrategy :
DisallowIdentityContentLengthStrategy.INSTANCE,
outgoingContentStrategy != null ? outgoingContentStrategy :
StrictContentLengthStrategy.INSTANCE);
this.requestParser = (requestParserFactory != null ? requestParserFactory :
DefaultHttpRequestParserFactory.INSTANCE).create(this.inbuf, constraints);
this.responseWriter = (responseWriterFactory != null ? responseWriterFactory :
DefaultHttpResponseWriterFactory.INSTANCE).create(this.outbuf);
}
/**
* @since 4.3
*/
public DefaultNHttpServerConnection(
final IOSession session,
final int buffersize,
final CharsetDecoder chardecoder,
final CharsetEncoder charencoder,
final MessageConstraints constraints) {
this(session, buffersize, buffersize, null, chardecoder, charencoder, constraints,
null, null, null, null);
}
/**
* @since 4.3
*/
public DefaultNHttpServerConnection(final IOSession session, final int buffersize) {
this(session, buffersize, buffersize, null, null, null, null, null, null, null, null);
}
/**
* @deprecated (4.3) use constructor.
*/
@Override
@Deprecated
protected ContentLengthStrategy createIncomingContentStrategy() {
return new DisallowIdentityContentLengthStrategy(new LaxContentLengthStrategy(0));
}
/**
* Creates an instance of {@link NHttpMessageParser} to be used
* by this connection for parsing incoming {@link HttpRequest} messages.
*
* This method can be overridden in a super class in order to provide
* a different implementation of the {@link NHttpMessageParser} interface.
*
* @return HTTP response parser.
*
* @deprecated (4.3) use constructor.
*/
@Deprecated
protected NHttpMessageParser createRequestParser(
final SessionInputBuffer buffer,
final HttpRequestFactory requestFactory,
final HttpParams params) {
final MessageConstraints constraints = HttpParamConfig.getMessageConstraints(params);
return new DefaultHttpRequestParser(buffer, null, requestFactory, constraints);
}
/**
* Creates an instance of {@link NHttpMessageWriter} to be used
* by this connection for writing out outgoing {@link HttpResponse}
* messages.
*
* This method can be overridden by a super class in order to provide
* a different implementation of the {@link NHttpMessageWriter} interface.
*
* @return HTTP response parser.
*
* @deprecated (4.3) use constructor.
*/
@Deprecated
protected NHttpMessageWriter createResponseWriter(
final SessionOutputBuffer buffer,
final HttpParams params) {
// override in derived class to specify a line formatter
return new DefaultHttpResponseWriter(buffer, null);
}
/**
* @since 4.2
*/
protected void onRequestReceived(final HttpRequest request) {
}
/**
* @since 4.2
*/
protected void onResponseSubmitted(final HttpResponse response) {
}
public void resetInput() {
this.request = null;
this.contentDecoder = null;
this.requestParser.reset();
}
public void resetOutput() {
this.response = null;
this.contentEncoder = null;
this.responseWriter.reset();
}
public void consumeInput(final NHttpServerEventHandler handler) {
if (this.status != ACTIVE) {
this.session.clearEvent(EventMask.READ);
return;
}
try {
if (this.request == null) {
int bytesRead;
do {
bytesRead = this.requestParser.fillBuffer(this.session.channel());
if (bytesRead > 0) {
this.inTransportMetrics.incrementBytesTransferred(bytesRead);
}
this.request = this.requestParser.parse();
} while (bytesRead > 0 && this.request == null);
if (this.request != null) {
if (this.request instanceof HttpEntityEnclosingRequest) {
// Receive incoming entity
final HttpEntity entity = prepareDecoder(this.request);
((HttpEntityEnclosingRequest)this.request).setEntity(entity);
}
this.connMetrics.incrementRequestCount();
onRequestReceived(this.request);
handler.requestReceived(this);
if (this.contentDecoder == null) {
// No request entity is expected
// Ready to receive a new request
resetInput();
}
}
if (bytesRead == -1) {
handler.endOfInput(this);
}
}
if (this.contentDecoder != null) {
// Loop until there is interest in input,
// decoder is not done and there is buffered session data
while ((this.session.getEventMask() & SelectionKey.OP_READ) > 0) {
handler.inputReady(this, this.contentDecoder);
if (this.contentDecoder.isCompleted()) {
// Response entity received
// Ready to receive a new response
resetInput();
break;
}
if (!this.inbuf.hasData()) {
break;
}
}
}
} catch (final HttpException ex) {
resetInput();
handler.exception(this, ex);
} catch (final Exception ex) {
handler.exception(this, ex);
} finally {
// Finally set buffered input flag
this.hasBufferedInput = this.inbuf.hasData();
}
}
public void produceOutput(final NHttpServerEventHandler handler) {
try {
if (this.status == ACTIVE) {
if (this.contentEncoder == null) {
handler.responseReady(this);
}
if (this.contentEncoder != null) {
handler.outputReady(this, this.contentEncoder);
if (this.contentEncoder.isCompleted()) {
resetOutput();
}
}
}
if (this.outbuf.hasData()) {
final int bytesWritten = this.outbuf.flush(this.session.channel());
if (bytesWritten > 0) {
this.outTransportMetrics.incrementBytesTransferred(bytesWritten);
}
}
if (!this.outbuf.hasData()) {
if (this.status == CLOSING) {
this.session.close();
this.status = CLOSED;
resetOutput();
}
if (this.contentEncoder == null && this.status != CLOSED) {
this.session.clearEvent(EventMask.WRITE);
}
}
} catch (final Exception ex) {
handler.exception(this, ex);
} finally {
// Finally set the buffered output flag
this.hasBufferedOutput = this.outbuf.hasData();
}
}
public void submitResponse(final HttpResponse response) throws IOException, HttpException {
Args.notNull(response, "HTTP response");
assertNotClosed();
if (this.response != null) {
throw new HttpException("Response already submitted");
}
onResponseSubmitted(response);
this.responseWriter.write(response);
this.hasBufferedOutput = this.outbuf.hasData();
if (response.getStatusLine().getStatusCode() >= 200) {
this.connMetrics.incrementResponseCount();
if (response.getEntity() != null) {
this.response = response;
prepareEncoder(response);
}
}
this.session.setEvent(EventMask.WRITE);
}
public boolean isResponseSubmitted() {
return this.response != null;
}
public void consumeInput(final NHttpServiceHandler handler) {
consumeInput(new NHttpServerEventHandlerAdaptor(handler));
}
public void produceOutput(final NHttpServiceHandler handler) {
produceOutput(new NHttpServerEventHandlerAdaptor(handler));
}
}