001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.nhttp;
020:
021: import java.io.IOException;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.http.Header;
026: import org.apache.http.HttpException;
027: import org.apache.http.HttpResponse;
028: import org.apache.http.nio.ContentDecoder;
029: import org.apache.http.nio.ContentEncoder;
030: import org.apache.http.nio.NHttpClientConnection;
031: import org.apache.http.nio.NHttpClientHandler;
032:
033: /**
034: * Decorator class intended to transparently extend an {@link NHttpClientHandler}
035: * with basic event logging capabilities using Commons Logging.
036: */
037: public class LoggingNHttpClientHandler implements NHttpClientHandler {
038:
039: private final Log log;
040: private final Log headerlog;
041: private final NHttpClientHandler handler;
042:
043: public LoggingNHttpClientHandler(final NHttpClientHandler handler) {
044: super ();
045: if (handler == null) {
046: throw new IllegalArgumentException(
047: "HTTP client handler may not be null");
048: }
049: this .handler = handler;
050: this .log = LogFactory.getLog(handler.getClass());
051: this .headerlog = LogFactory
052: .getLog("org.apache.axis2.transport.nhttp.headers");
053: }
054:
055: public void connected(final NHttpClientConnection conn,
056: final Object attachment) {
057: if (this .log.isDebugEnabled()) {
058: this .log.debug("HTTP connection " + conn + ": Connected ("
059: + attachment + ")");
060: }
061: this .handler.connected(conn, attachment);
062: }
063:
064: public void closed(final NHttpClientConnection conn) {
065: if (this .log.isDebugEnabled()) {
066: this .log.debug("HTTP connection " + conn + ": Closed");
067: }
068: this .handler.closed(conn);
069: }
070:
071: public void exception(final NHttpClientConnection conn,
072: final IOException ex) {
073: this .log.error("HTTP connection " + conn + ": "
074: + ex.getMessage(), ex);
075: this .handler.exception(conn, ex);
076: }
077:
078: public void exception(final NHttpClientConnection conn,
079: final HttpException ex) {
080: this .log.error("HTTP connection " + conn + ": "
081: + ex.getMessage(), ex);
082: this .handler.exception(conn, ex);
083: }
084:
085: public void requestReady(final NHttpClientConnection conn) {
086: if (this .log.isDebugEnabled()) {
087: this .log.debug("HTTP connection " + conn
088: + ": Request ready");
089: }
090: this .handler.requestReady(conn);
091: }
092:
093: public void outputReady(final NHttpClientConnection conn,
094: final ContentEncoder encoder) {
095: if (this .log.isDebugEnabled()) {
096: this .log
097: .debug("HTTP connection " + conn + ": Output ready");
098: }
099: this .handler.outputReady(conn, encoder);
100: if (this .log.isDebugEnabled()) {
101: this .log.debug("HTTP connection " + conn
102: + ": Content encoder " + encoder);
103: }
104: }
105:
106: public void responseReceived(final NHttpClientConnection conn) {
107: HttpResponse response = conn.getHttpResponse();
108: if (this .log.isDebugEnabled()) {
109: this .log.debug("HTTP connection " + conn + ": "
110: + response.getStatusLine());
111: }
112: this .handler.responseReceived(conn);
113: if (this .headerlog.isDebugEnabled()) {
114: this .headerlog.debug("<< "
115: + response.getStatusLine().toString());
116: Header[] headers = response.getAllHeaders();
117: for (int i = 0; i < headers.length; i++) {
118: this .headerlog.debug("<< " + headers[i].toString());
119: }
120: }
121: }
122:
123: public void inputReady(final NHttpClientConnection conn,
124: final ContentDecoder decoder) {
125: if (this .log.isDebugEnabled()) {
126: this .log.debug("HTTP connection " + conn + ": Input ready");
127: }
128: this .handler.inputReady(conn, decoder);
129: if (this .log.isDebugEnabled()) {
130: this .log.debug("HTTP connection " + conn
131: + ": Content decoder " + decoder);
132: }
133: }
134:
135: public void timeout(final NHttpClientConnection conn) {
136: if (this .log.isDebugEnabled()) {
137: this .log.debug("HTTP connection " + conn + ": Timeout");
138: }
139: this.handler.timeout(conn);
140: }
141:
142: }
|