Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpClientConnectionFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpClientConnectionFactory.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpClientConnectionFactory.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpClientConnectionFactory.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,152 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.config.ConnectionConfig; +import org.apache.http.config.MessageConstraints; +import org.apache.http.impl.ConnSupport; +import org.apache.http.impl.entity.StrictContentLengthStrategy; +import org.apache.http.impl.nio.DefaultNHttpClientConnection; +import org.apache.http.impl.nio.codecs.DefaultHttpRequestWriter; +import org.apache.http.impl.nio.codecs.DefaultHttpResponseParser; +import org.apache.http.nio.*; +import org.apache.http.nio.reactor.IOSession; +import org.apache.http.nio.reactor.SessionInputBuffer; +import org.apache.http.nio.reactor.SessionOutputBuffer; +import org.apache.http.nio.util.HeapByteBufferAllocator; + +import java.io.IOException; + +public class LoggingNHttpClientConnectionFactory implements NHttpConnectionFactory<DefaultNHttpClientConnection> { + + private static final Log targetConnLog = LogFactory.getLog( + LoggingConstants.TARGET_CONNECTION_LOG_ID); + private static final Log targetHeaderLog = LogFactory.getLog( + LoggingConstants.TARGET_HEADER_LOG_ID); + private static final Log targetSessionLog = LogFactory.getLog( + LoggingConstants.TARGET_SESSION_LOG_ID); + private static final Log targetWireLog = LogFactory.getLog( + LoggingConstants.TARGET_WIRE_LOG_ID); + + private static final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory = + new LoggingNHttpRequestWriterFactory(); + private static final NHttpMessageParserFactory<HttpResponse> responseParserFactory = + new LoggingNHttpResponseParserFactory(); + + private final ConnectionConfig config; + + public LoggingNHttpClientConnectionFactory(ConnectionConfig config) { + this.config = config; + } + + public DefaultNHttpClientConnection createConnection(IOSession session) { + if (targetSessionLog.isDebugEnabled() || targetWireLog.isDebugEnabled()) { + session = new LoggingIOSession(targetSessionLog, targetWireLog, + session, "http-sender"); + } + + if (targetConnLog.isDebugEnabled() || targetHeaderLog.isDebugEnabled()) { + return new LoggingNHttpClientConnection( + session, + config.getBufferSize(), + config.getFragmentSizeHint(), + HeapByteBufferAllocator.INSTANCE, + ConnSupport.createDecoder(config), + ConnSupport.createEncoder(config), + config.getMessageConstraints(), + StrictContentLengthStrategy.INSTANCE, + StrictContentLengthStrategy.INSTANCE, + requestWriterFactory, + responseParserFactory, + targetConnLog); + } else { + return new DefaultNHttpClientConnection( + session, + config.getBufferSize(), + config.getFragmentSizeHint(), + HeapByteBufferAllocator.INSTANCE, + ConnSupport.createDecoder(config), + ConnSupport.createEncoder(config), + config.getMessageConstraints(), + StrictContentLengthStrategy.INSTANCE, + StrictContentLengthStrategy.INSTANCE, + requestWriterFactory, + responseParserFactory); + } + } + + static class LoggingNHttpRequestWriterFactory implements NHttpMessageWriterFactory<HttpRequest> { + public NHttpMessageWriter<HttpRequest> create(SessionOutputBuffer sessionBuffer) { + return new LoggingNHttpRequestWriter(sessionBuffer); + } + } + + static class LoggingNHttpResponseParserFactory implements NHttpMessageParserFactory<HttpResponse> { + public NHttpMessageParser<HttpResponse> create(SessionInputBuffer sessionBuffer, + MessageConstraints messageConstraints) { + return new LoggingNHttpResponseParser(sessionBuffer, messageConstraints); + } + } + + static class LoggingNHttpRequestWriter extends DefaultHttpRequestWriter { + + public LoggingNHttpRequestWriter(SessionOutputBuffer buffer) { + super(buffer); + } + + public void write(final HttpRequest request) throws IOException, HttpException { + if (request != null && targetHeaderLog.isDebugEnabled()) { + targetHeaderLog.debug(">> " + request.getRequestLine().toString()); + Header[] headers = request.getAllHeaders(); + for (Header header : headers) { + targetHeaderLog.debug(">> " + header.toString()); + } + } + super.write(request); + } + + } + + static class LoggingNHttpResponseParser extends DefaultHttpResponseParser { + + public LoggingNHttpResponseParser(SessionInputBuffer buffer, MessageConstraints constraints) { + super(buffer, constraints); + } + + public HttpResponse parse() throws IOException, HttpException { + HttpResponse response = super.parse(); + if (response != null && targetHeaderLog.isDebugEnabled()) { + targetHeaderLog.debug("<< " + response.getStatusLine().toString()); + Header[] headers = response.getAllHeaders(); + for (Header header : headers) { + targetHeaderLog.debug("<< " + header.toString()); + } + } + return response; + } + + } +}
Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLClientConnectionFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLClientConnectionFactory.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLClientConnectionFactory.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLClientConnectionFactory.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.http.config.ConnectionConfig; +import org.apache.http.impl.nio.DefaultNHttpClientConnection; +import org.apache.http.nio.reactor.IOSession; +import org.apache.http.nio.reactor.ssl.SSLIOSession; +import org.apache.http.nio.reactor.ssl.SSLMode; +import org.apache.http.nio.reactor.ssl.SSLSetupHandler; + +import javax.net.ssl.SSLContext; +import java.net.InetSocketAddress; +import java.util.Map; + +public class LoggingNHttpSSLClientConnectionFactory extends LoggingNHttpClientConnectionFactory { + + private SSLContext sslContext; + private SSLSetupHandler sslSetupHandler; + private Map<String,SSLContext> customContexts; + + public LoggingNHttpSSLClientConnectionFactory(ConnectionConfig config, + SSLContext sslContext, + SSLSetupHandler sslSetupHandler, + Map<String, SSLContext> customContexts) { + super(config); + this.sslContext = sslContext; + this.sslSetupHandler = sslSetupHandler; + this.customContexts = customContexts; + } + + @Override + public DefaultNHttpClientConnection createConnection(IOSession session) { + final SSLIOSession ssliosession = new SSLIOSession( + session, + SSLMode.CLIENT, + getSSLContext(session), + sslSetupHandler); + session.setAttribute(SSLIOSession.SESSION_KEY, ssliosession); + return super.createConnection(ssliosession); + } + + private SSLContext getSSLContext(IOSession session) { + InetSocketAddress address = (InetSocketAddress) session.getRemoteAddress(); + String host = address.getHostName() + ":" + address.getPort(); + SSLContext customContext = null; + if (customContexts != null) { + // See if there's a custom SSL profile configured for this server + customContext = customContexts.get(host); + } + + if (customContext == null) { + customContext = sslContext; + } + return customContext; + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLServerConnectionFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLServerConnectionFactory.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLServerConnectionFactory.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpSSLServerConnectionFactory.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.http.config.ConnectionConfig; +import org.apache.http.impl.nio.DefaultNHttpServerConnection; +import org.apache.http.nio.reactor.IOSession; +import org.apache.http.nio.reactor.ssl.SSLIOSession; +import org.apache.http.nio.reactor.ssl.SSLMode; +import org.apache.http.nio.reactor.ssl.SSLSetupHandler; + +import javax.net.ssl.SSLContext; + +public class LoggingNHttpSSLServerConnectionFactory extends LoggingNHttpServerConnectionFactory { + + private SSLContext sslContext; + private SSLSetupHandler sslSetupHandler; + + public LoggingNHttpSSLServerConnectionFactory(ConnectionConfig config, SSLContext sslContext, + SSLSetupHandler sslSetupHandler) { + super(config); + this.sslContext = sslContext; + this.sslSetupHandler = sslSetupHandler; + } + + @Override + public DefaultNHttpServerConnection createConnection(IOSession session) { + final SSLIOSession ssliosession = new SSLIOSession( + session, + SSLMode.SERVER, + sslContext, + sslSetupHandler); + session.setAttribute(SSLIOSession.SESSION_KEY, ssliosession); + return super.createConnection(ssliosession); + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnection.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnection.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnection.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnection.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.config.MessageConstraints; +import org.apache.http.entity.ContentLengthStrategy; +import org.apache.http.impl.nio.DefaultNHttpServerConnection; +import org.apache.http.nio.NHttpMessageParserFactory; +import org.apache.http.nio.NHttpMessageWriterFactory; +import org.apache.http.nio.NHttpServerEventHandler; +import org.apache.http.nio.reactor.IOSession; +import org.apache.http.nio.util.ByteBufferAllocator; + +import java.io.IOException; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CharsetEncoder; + +public class LoggingNHttpServerConnection extends DefaultNHttpServerConnection { + + private final Log log; + + public LoggingNHttpServerConnection(IOSession session, + int bufferSize, + int fragmentSizeHint, + ByteBufferAllocator allocator, + CharsetDecoder charDecoder, + CharsetEncoder charEncoder, + MessageConstraints constraints, + ContentLengthStrategy incomingContentStrategy, + ContentLengthStrategy outgoingContentStrategy, + NHttpMessageParserFactory<HttpRequest> requestParserFactory, + NHttpMessageWriterFactory<HttpResponse> responseWriterFactory, + Log log) { + super(session, bufferSize, fragmentSizeHint, allocator, + charDecoder, charEncoder, constraints, incomingContentStrategy, + outgoingContentStrategy, requestParserFactory, responseWriterFactory); + this.log = log; + } + + @Override + public void close() throws IOException { + this.log.debug("Close connection"); + super.close(); + } + + @Override + public void shutdown() throws IOException { + this.log.debug("Shutdown connection"); + super.shutdown(); + } + + @Override + public void submitResponse(final HttpResponse response) throws IOException, HttpException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + this + ": " + response.getStatusLine().toString()); + } + super.submitResponse(response); + } + + @Override + public void consumeInput(final NHttpServerEventHandler handler) { + this.log.debug("Consume input"); + super.consumeInput(handler); + } + + @Override + public void produceOutput(final NHttpServerEventHandler handler) { + this.log.debug("Produce output"); + super.produceOutput(handler); + } + +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnectionFactory.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnectionFactory.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnectionFactory.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingNHttpServerConnectionFactory.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,150 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.config.ConnectionConfig; +import org.apache.http.config.MessageConstraints; +import org.apache.http.impl.ConnSupport; +import org.apache.http.impl.entity.StrictContentLengthStrategy; +import org.apache.http.impl.nio.DefaultNHttpServerConnection; +import org.apache.http.impl.nio.codecs.DefaultHttpRequestParser; +import org.apache.http.impl.nio.codecs.DefaultHttpResponseWriter; +import org.apache.http.nio.*; +import org.apache.http.nio.reactor.IOSession; +import org.apache.http.nio.reactor.SessionInputBuffer; +import org.apache.http.nio.reactor.SessionOutputBuffer; +import org.apache.http.nio.util.HeapByteBufferAllocator; + +import java.io.IOException; + +public class LoggingNHttpServerConnectionFactory implements NHttpConnectionFactory<DefaultNHttpServerConnection> { + + private static final Log sourceConnLog = LogFactory.getLog( + LoggingConstants.SOURCE_CONNECTION_LOG_ID); + private static final Log sourceHeaderLog = LogFactory.getLog( + LoggingConstants.SOURCE_HEADER_LOG_ID); + private static final Log sourceSessionLog = LogFactory.getLog( + LoggingConstants.SOURCE_SESSION_LOG_ID); + private static final Log sourceWireLog = LogFactory.getLog( + LoggingConstants.SOURCE_WIRE_LOG_ID); + + private static final NHttpMessageParserFactory<HttpRequest> requestParserFactory = + new LoggingNHttpRequestParserFactory(); + private static final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory = + new LoggingNHttpResponseWriterFactory(); + + private final ConnectionConfig config; + + public LoggingNHttpServerConnectionFactory(ConnectionConfig config) { + this.config = config; + } + + public DefaultNHttpServerConnection createConnection(IOSession session) { + if (sourceSessionLog.isDebugEnabled() || sourceWireLog.isDebugEnabled()) { + session = new LoggingIOSession(sourceSessionLog, sourceWireLog, + session, "http-listener"); + } + + if (sourceConnLog.isDebugEnabled()) { + return new LoggingNHttpServerConnection( + session, + config.getBufferSize(), + config.getFragmentSizeHint(), + HeapByteBufferAllocator.INSTANCE, + ConnSupport.createDecoder(config), + ConnSupport.createEncoder(config), + config.getMessageConstraints(), + StrictContentLengthStrategy.INSTANCE, + StrictContentLengthStrategy.INSTANCE, + requestParserFactory, + responseWriterFactory, + sourceConnLog); + } else { + return new DefaultNHttpServerConnection( + session, + config.getBufferSize(), + config.getFragmentSizeHint(), + HeapByteBufferAllocator.INSTANCE, + ConnSupport.createDecoder(config), + ConnSupport.createEncoder(config), + config.getMessageConstraints(), + StrictContentLengthStrategy.INSTANCE, + StrictContentLengthStrategy.INSTANCE, + requestParserFactory, + responseWriterFactory); + } + } + + static class LoggingNHttpRequestParserFactory implements NHttpMessageParserFactory<HttpRequest> { + public NHttpMessageParser<HttpRequest> create(SessionInputBuffer sessionBuffer, + MessageConstraints messageConstraints) { + return new LoggingNHttpRequestParser(sessionBuffer, messageConstraints); + } + } + + static class LoggingNHttpResponseWriterFactory implements NHttpMessageWriterFactory<HttpResponse> { + public NHttpMessageWriter<HttpResponse> create(SessionOutputBuffer sessionBuffer) { + return new LoggingNHttpResponseWriter(sessionBuffer); + } + } + + static class LoggingNHttpRequestParser extends DefaultHttpRequestParser { + + public LoggingNHttpRequestParser(SessionInputBuffer buffer, MessageConstraints constraints) { + super(buffer, constraints); + } + + public HttpRequest parse() throws IOException, HttpException { + HttpRequest request = super.parse(); + if (request != null && sourceHeaderLog.isDebugEnabled()) { + sourceHeaderLog.debug(">> " + request.getRequestLine().toString()); + Header[] headers = request.getAllHeaders(); + for (Header header : headers) { + sourceHeaderLog.debug(">> " + header.toString()); + } + } + return request; + } + } + + static class LoggingNHttpResponseWriter extends DefaultHttpResponseWriter { + + public LoggingNHttpResponseWriter(SessionOutputBuffer buffer) { + super(buffer); + } + + public void write(final HttpResponse response) throws IOException, HttpException { + if (response != null && sourceHeaderLog.isDebugEnabled()) { + sourceHeaderLog.debug("<< " + response.getStatusLine().toString()); + Header[] headers = response.getAllHeaders(); + for (Header header : headers) { + sourceHeaderLog.debug("<< " + header.toString()); + } + } + super.write(response); + } + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingServerEventHandler.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingServerEventHandler.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingServerEventHandler.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingServerEventHandler.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.nio.ContentDecoder; +import org.apache.http.nio.ContentEncoder; +import org.apache.http.nio.NHttpServerConnection; +import org.apache.http.nio.NHttpServerEventHandler; + +import java.io.IOException; + +public class LoggingServerEventHandler implements NHttpServerEventHandler { + + private final Log log; + + private final NHttpServerEventHandler handler; + + public LoggingServerEventHandler(final NHttpServerEventHandler handler) { + super(); + if (handler == null) { + throw new IllegalArgumentException("HTTP service handler may not be null"); + } + this.handler = handler; + this.log = LogFactory.getLog(handler.getClass()); + } + + public void connected(final NHttpServerConnection conn) throws IOException, HttpException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Connected"); + } + this.handler.connected(conn); + } + + public void closed(final NHttpServerConnection conn) { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Closed"); + } + this.handler.closed(conn); + } + + public void endOfInput(NHttpServerConnection conn) throws IOException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Closed at the remote end"); + } + this.handler.endOfInput(conn); + } + + public void exception(NHttpServerConnection conn, Exception ex) { + // No need to log errors at this level - Actual handler implementation + // should take care of that + this.handler.exception(conn, ex); + } + + public void requestReceived(final NHttpServerConnection conn) throws IOException, HttpException { + HttpRequest request = conn.getHttpRequest(); + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP InRequest Received on connection " + conn + ": " + + request.getRequestLine()); + } + this.handler.requestReceived(conn); + } + + public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) throws IOException, HttpException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Output ready"); + } + this.handler.outputReady(conn, encoder); + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Content encoder " + encoder); + } + } + + public void responseReady(final NHttpServerConnection conn) throws IOException, HttpException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Response ready"); + } + this.handler.responseReady(conn); + } + + public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) throws IOException, HttpException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Input ready"); + } + this.handler.inputReady(conn, decoder); + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Content decoder " + decoder); + } + } + + public void timeout(final NHttpServerConnection conn) throws IOException { + if (this.log.isDebugEnabled()) { + this.log.debug("HTTP connection " + conn + ": Timeout"); + } + this.handler.timeout(conn); + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingUtils.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingUtils.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingUtils.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/LoggingUtils.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.config.ConnectionConfig; +import org.apache.http.impl.nio.DefaultHttpClientIODispatch; +import org.apache.http.impl.nio.DefaultHttpServerIODispatch; +import org.apache.http.nio.NHttpClientEventHandler; +import org.apache.http.nio.NHttpServerEventHandler; +import org.apache.http.nio.reactor.ssl.SSLSetupHandler; + +import javax.net.ssl.SSLContext; +import java.util.Map; + +public class LoggingUtils { + + private static NHttpClientEventHandler decorate(NHttpClientEventHandler handler) { + Log log = LogFactory.getLog(handler.getClass()); + if (log.isDebugEnabled()) { + handler = new LoggingClientEventHandler(handler); + } + return handler; + } + + private static NHttpServerEventHandler decorate(NHttpServerEventHandler handler) { + Log log = LogFactory.getLog(handler.getClass()); + if (log.isDebugEnabled()) { + handler = new LoggingServerEventHandler(handler); + } + return handler; + } + + public static DefaultHttpServerIODispatch getServerIODispatch(final NHttpServerEventHandler handler, + final ConnectionConfig config) { + return new DefaultHttpServerIODispatch(decorate(handler), + new LoggingNHttpServerConnectionFactory(config)); + } + + public static DefaultHttpServerIODispatch getServerIODispatch(final NHttpServerEventHandler handler, + final ConnectionConfig config, + final SSLContext sslContext, + final SSLSetupHandler sslSetupHandler) { + return new DefaultHttpServerIODispatch(decorate(handler), + new LoggingNHttpSSLServerConnectionFactory(config, sslContext, sslSetupHandler)); + } + + public static DefaultHttpClientIODispatch getClientIODispatch(final NHttpClientEventHandler handler, + final ConnectionConfig config) { + return new DefaultHttpClientIODispatch(decorate(handler), + new LoggingNHttpClientConnectionFactory(config)); + } + + public static DefaultHttpClientIODispatch getClientIODispatch(final NHttpClientEventHandler handler, + final ConnectionConfig config, + final SSLContext sslContext, + final SSLSetupHandler sslSetupHandler, + Map<String, SSLContext> customContexts) { + return new DefaultHttpClientIODispatch(decorate(handler), + new LoggingNHttpSSLClientConnectionFactory(config, sslContext, sslSetupHandler, customContexts)); + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/Wire.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/Wire.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/Wire.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/main/java/org/apache/synapse/transport/utils/logging/Wire.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.logging; + +import org.apache.commons.logging.Log; + +import java.nio.ByteBuffer; + +public class Wire { + + private final Log log; + + public Wire(final Log log) { + super(); + this.log = log; + } + + private void wire(final String header, final byte[] b, int pos, int off) { + StringBuilder buffer = new StringBuilder(); + for (int i = 0; i < off; i++) { + int ch = b[pos + i]; + if (ch == 13) { + buffer.append("[\\r]"); + } else if (ch == 10) { + buffer.append("[\\n]\""); + buffer.insert(0, "\""); + buffer.insert(0, header); + this.log.debug(buffer.toString()); + buffer.setLength(0); + } else if ((ch < 32) || (ch > 127)) { + buffer.append("[0x"); + buffer.append(Integer.toHexString(ch)); + buffer.append("]"); + } else { + buffer.append((char) ch); + } + } + if (buffer.length() > 0) { + buffer.append('\"'); + buffer.insert(0, '\"'); + buffer.insert(0, header); + this.log.debug(buffer.toString()); + } + } + + + public boolean isEnabled() { + return this.log.isDebugEnabled(); + } + + public void output(final byte[] b, int pos, int off) { + wire("<< ", b, pos, off); + } + + public void input(final byte[] b, int pos, int off) { + wire(">> ", b, pos, off); + } + + public void output(byte[] b) { + output(b, 0, b.length); + } + + public void input(byte[] b) { + input(b, 0, b.length); + } + + public void output(int b) { + output(new byte[] {(byte) b}); + } + + public void input(int b) { + input(new byte[] {(byte) b}); + } + + public void output(final ByteBuffer b) { + if (b.hasArray()) { + output(b.array(), b.arrayOffset() + b.position(), b.remaining()); + } else { + byte[] tmp = new byte[b.remaining()]; + b.get(tmp); + output(tmp); + } + } + + public void input(final ByteBuffer b) { + if (b.hasArray()) { + input(b.array(), b.arrayOffset() + b.position(), b.remaining()); + } else { + byte[] tmp = new byte[b.remaining()]; + b.get(tmp); + input(tmp); + } + } +} Added: synapse/trunk/java/modules/transports/core/nhttp/src/test/java/org/apache/synapse/transport/utils/config/HttpTransportConfigurationTest.java URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/test/java/org/apache/synapse/transport/utils/config/HttpTransportConfigurationTest.java?rev=1512866&view=auto ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/test/java/org/apache/synapse/transport/utils/config/HttpTransportConfigurationTest.java (added) +++ synapse/trunk/java/modules/transports/core/nhttp/src/test/java/org/apache/synapse/transport/utils/config/HttpTransportConfigurationTest.java Sun Aug 11 03:22:44 2013 @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.synapse.transport.utils.config; + +import junit.framework.TestCase; +import org.apache.http.config.ConnectionConfig; +import org.apache.http.impl.nio.reactor.IOReactorConfig; + +import java.nio.charset.CodingErrorAction; + +public class HttpTransportConfigurationTest extends TestCase { + + public void testDefaults() { + HttpTransportConfiguration config = new SimpleHttpTransportConfiguration("bogus"); + assertEquals(10, (int) config.getIntProperty("foo", 10)); + assertEquals("barValue", config.getStringProperty("bar", "barValue")); + assertEquals(true, (boolean) config.getBooleanProperty("baz", true)); + + IOReactorConfig reactorConfig = config.getReactorConfig(); + assertEquals(2, reactorConfig.getIoThreadCount()); + assertEquals(0, reactorConfig.getConnectTimeout()); + assertEquals(1024 * 8, reactorConfig.getRcvBufSize()); + assertEquals(1024 * 8, reactorConfig.getSndBufSize()); + assertEquals(60000, reactorConfig.getSoTimeout()); + assertEquals(true, reactorConfig.isTcpNoDelay()); + assertEquals(false, reactorConfig.isInterestOpQueued()); + + ConnectionConfig connConfig = config.getConnectionConfig(); + assertEquals(1024 * 8, connConfig.getBufferSize()); + assertEquals(CodingErrorAction.REPORT, connConfig.getMalformedInputAction()); + assertEquals(CodingErrorAction.REPORT, connConfig.getUnmappableInputAction()); + } + + public void testNHttp() { + HttpTransportConfiguration config = new SimpleHttpTransportConfiguration("nhttp"); + assertEquals(1000, (int) config.getIntProperty("test.foo", -1)); + assertEquals("Testing", config.getStringProperty("test.bar", "NotTesting")); + assertEquals(true, (boolean) config.getBooleanProperty("test.baz", false)); + + IOReactorConfig reactorConfig = config.getReactorConfig(); + assertEquals(true, reactorConfig.isSoReuseAddress()); + assertEquals(2, reactorConfig.getIoThreadCount()); + assertEquals(0, reactorConfig.getConnectTimeout()); + assertEquals(1024 * 8, reactorConfig.getRcvBufSize()); + assertEquals(1024 * 8, reactorConfig.getSndBufSize()); + assertEquals(60000, reactorConfig.getSoTimeout()); + assertEquals(true, reactorConfig.isTcpNoDelay()); + assertEquals(false, reactorConfig.isInterestOpQueued()); + + ConnectionConfig connConfig = config.getConnectionConfig(); + assertEquals(1024 * 8, connConfig.getBufferSize()); + assertEquals(CodingErrorAction.REPORT, connConfig.getMalformedInputAction()); + assertEquals(CodingErrorAction.REPORT, connConfig.getUnmappableInputAction()); + } + + class SimpleHttpTransportConfiguration extends HttpTransportConfiguration { + public SimpleHttpTransportConfiguration(String fileName) { + super(fileName); + } + + @Override + protected int getThreadsPerReactor() { + return 2; + } + } +} Modified: synapse/trunk/java/modules/transports/core/nhttp/src/test/resources/nhttp.properties URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/transports/core/nhttp/src/test/resources/nhttp.properties?rev=1512866&r1=1512865&r2=1512866&view=diff ============================================================================== --- synapse/trunk/java/modules/transports/core/nhttp/src/test/resources/nhttp.properties (original) +++ synapse/trunk/java/modules/transports/core/nhttp/src/test/resources/nhttp.properties Sun Aug 11 03:22:44 2013 @@ -1,8 +1,13 @@ http.socket.timeout=60000 http.connection.timeout=0 -http.socket.buffer-size=8096 -http.tcp.nodelay=1 +http.socket.buffer-size=8192 +http.tcp.nodelay=true http.nio.interest-ops-queueing=false # This property is crucial for automated tests -http.socket.reuseaddr=true \ No newline at end of file +http.socket.reuseaddr=true + +# Used by HttpTransportConfiguration tests +test.foo=1000 +test.bar=Testing +test.baz=true \ No newline at end of file Modified: synapse/trunk/java/pom.xml URL: http://svn.apache.org/viewvc/synapse/trunk/java/pom.xml?rev=1512866&r1=1512865&r2=1512866&view=diff ============================================================================== --- synapse/trunk/java/pom.xml (original) +++ synapse/trunk/java/pom.xml Sun Aug 11 03:22:44 2013 @@ -1121,7 +1121,7 @@ <truezip.version>6.6</truezip.version> <jsch.version>0.1.31</jsch.version> <jms-1.1-spec.version>1.1</jms-1.1-spec.version> - <httpcore.nio.version>4.2.4</httpcore.nio.version> + <httpcore.nio.version>4.3</httpcore.nio.version> <http.client.version>4.1</http.client.version> <aspectj.version>1.6.1</aspectj.version> <qfj.version>1.4.0</qfj.version> Modified: synapse/trunk/java/repository/conf/nhttp.properties URL: http://svn.apache.org/viewvc/synapse/trunk/java/repository/conf/nhttp.properties?rev=1512866&r1=1512865&r2=1512866&view=diff ============================================================================== --- synapse/trunk/java/repository/conf/nhttp.properties (original) +++ synapse/trunk/java/repository/conf/nhttp.properties Sun Aug 11 03:22:44 2013 @@ -21,22 +21,25 @@ #http.socket.timeout=60000 #http.socket.buffer-size=8192 -#http.tcp.nodelay=1 +#http.socket.rcv-buffer-size=8192 +#http.socket.snd-buffer-size=8192 +#http.tcp.nodelay=true #http.connection.stalecheck=0 # Uncomment the following property for an AIX based deployment #http.nio.interest-ops-queueing=true +# Number of I/O dispatcher threads that should be used by each IOReactor +#io_threads_per_reactor=2 + # HTTP Listener thread pool parameters #snd_t_core=20 #snd_t_max=100 #snd_alive_sec=5 #snd_qlen=-1 -#snd_io_threads=2 # HTTP Sender thread pool parameters #lst_t_core=20 #lst_t_max=100 #lst_alive_sec=5 #lst_qlen=-1 -#lst_io_threads=2
