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.HttpRequest;
028: import org.apache.http.nio.ContentDecoder;
029: import org.apache.http.nio.ContentEncoder;
030: import org.apache.http.nio.NHttpServerConnection;
031: import org.apache.http.nio.NHttpServiceHandler;
032:
033: /**
034: * Decorator class intended to transparently extend an {@link NHttpServiceHandler}
035: * with basic event logging capabilities using Commons Logging.
036: */
037: public class LoggingNHttpServiceHandler implements NHttpServiceHandler {
038:
039: private final Log log;
040: private final Log headerlog;
041: private final NHttpServiceHandler handler;
042:
043: public LoggingNHttpServiceHandler(final NHttpServiceHandler handler) {
044: super ();
045: if (handler == null) {
046: throw new IllegalArgumentException(
047: "HTTP service 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 NHttpServerConnection conn) {
056: if (this .log.isDebugEnabled()) {
057: this .log.debug("HTTP connection " + conn + ": Connected");
058: }
059: this .handler.connected(conn);
060: }
061:
062: public void closed(final NHttpServerConnection conn) {
063: if (this .log.isDebugEnabled()) {
064: this .log.debug("HTTP connection " + conn + ": Closed");
065: }
066: this .handler.closed(conn);
067: }
068:
069: public void exception(final NHttpServerConnection conn,
070: final IOException ex) {
071: if (ex.getMessage().indexOf("Connection reset") != -1
072: || ex.getMessage().indexOf("forcibly closed") != -1) {
073: this .log.warn("HTTP connection " + conn + ": "
074: + ex.getMessage());
075: } else {
076: this .log.error("HTTP connection " + conn + ": "
077: + ex.getMessage(), ex);
078: }
079: this .handler.exception(conn, ex);
080: }
081:
082: public void exception(final NHttpServerConnection conn,
083: final HttpException ex) {
084: if (ex.getMessage().indexOf("Connection reset") > 0
085: || ex.getMessage().indexOf("forcibly closed") > 0) {
086: this .log.warn("HTTP connection " + conn + ": "
087: + ex.getMessage());
088: } else {
089: this .log.error("HTTP connection " + conn + ": "
090: + ex.getMessage(), ex);
091: }
092: this .handler.exception(conn, ex);
093: }
094:
095: public void requestReceived(final NHttpServerConnection conn) {
096: HttpRequest request = conn.getHttpRequest();
097: if (this .log.isDebugEnabled()) {
098: this .log.debug("HTTP connection " + conn + ": "
099: + request.getRequestLine());
100: }
101: this .handler.requestReceived(conn);
102: if (this .headerlog.isDebugEnabled()) {
103: this .headerlog.debug(">> "
104: + request.getRequestLine().toString());
105: Header[] headers = request.getAllHeaders();
106: for (int i = 0; i < headers.length; i++) {
107: this .headerlog.debug(">> " + headers[i].toString());
108: }
109: }
110: }
111:
112: public void outputReady(final NHttpServerConnection conn,
113: final ContentEncoder encoder) {
114: if (this .log.isDebugEnabled()) {
115: this .log
116: .debug("HTTP connection " + conn + ": Output ready");
117: }
118: this .handler.outputReady(conn, encoder);
119: if (this .log.isDebugEnabled()) {
120: this .log.debug("HTTP connection " + conn
121: + ": Content encoder " + encoder);
122: }
123: }
124:
125: public void responseReady(final NHttpServerConnection conn) {
126: if (this .log.isDebugEnabled()) {
127: this .log.debug("HTTP connection " + conn
128: + ": Response ready");
129: }
130: this .handler.responseReady(conn);
131: }
132:
133: public void inputReady(final NHttpServerConnection conn,
134: final ContentDecoder decoder) {
135: if (this .log.isDebugEnabled()) {
136: this .log.debug("HTTP connection " + conn + ": Input ready");
137: }
138: this .handler.inputReady(conn, decoder);
139: if (this .log.isDebugEnabled()) {
140: this .log.debug("HTTP connection " + conn
141: + ": Content decoder " + decoder);
142: }
143: }
144:
145: public void timeout(final NHttpServerConnection conn) {
146: if (this .log.isDebugEnabled()) {
147: this .log.debug("HTTP connection " + conn + ": Timeout");
148: }
149: this.handler.timeout(conn);
150: }
151:
152: }
|