2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/contrib/src/main/java/org/apache/http/contrib/logging/LoggingNHttpServiceHandler.java $
4
* $Date: 2009-02-14 17:34:15 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.contrib.logging;
34
import java.io.IOException;
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38
import org.apache.http.Header;
39
import org.apache.http.HttpException;
40
import org.apache.http.HttpRequest;
41
import org.apache.http.nio.ContentDecoder;
42
import org.apache.http.nio.ContentEncoder;
43
import org.apache.http.nio.NHttpServerConnection;
44
import org.apache.http.nio.NHttpServiceHandler;
47
* Decorator class intended to transparently extend an {@link NHttpServiceHandler}
48
* with basic event logging capabilities using Commons Logging.
51
public class LoggingNHttpServiceHandler implements NHttpServiceHandler {
53
private final Log log;
54
private final Log headerlog;
55
private final NHttpServiceHandler handler;
57
public LoggingNHttpServiceHandler(final NHttpServiceHandler handler) {
59
if (handler == null) {
60
throw new IllegalArgumentException("HTTP service handler may not be null");
62
this.handler = handler;
63
this.log = LogFactory.getLog(handler.getClass());
64
this.headerlog = LogFactory.getLog("org.apache.http.headers");
67
public void connected(final NHttpServerConnection conn) {
68
if (this.log.isDebugEnabled()) {
69
this.log.debug("HTTP connection " + conn + ": Connected");
71
this.handler.connected(conn);
74
public void closed(final NHttpServerConnection conn) {
75
if (this.log.isDebugEnabled()) {
76
this.log.debug("HTTP connection " + conn + ": Closed");
78
this.handler.closed(conn);
81
public void exception(final NHttpServerConnection conn, final IOException ex) {
82
this.log.error("HTTP connection " + conn + ": " + ex.getMessage(), ex);
83
this.handler.exception(conn, ex);
86
public void exception(final NHttpServerConnection conn, final HttpException ex) {
87
this.log.error("HTTP connection " + conn + ": " + ex.getMessage(), ex);
88
this.handler.exception(conn, ex);
91
public void requestReceived(final NHttpServerConnection conn) {
92
HttpRequest request = conn.getHttpRequest();
93
if (this.log.isDebugEnabled()) {
94
this.log.debug("HTTP connection " + conn + ": " + request.getRequestLine());
96
this.handler.requestReceived(conn);
97
if (this.headerlog.isDebugEnabled()) {
98
this.headerlog.debug(">> " + request.getRequestLine().toString());
99
Header[] headers = request.getAllHeaders();
100
for (int i = 0; i < headers.length; i++) {
101
this.headerlog.debug(">> " + headers[i].toString());
106
public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
107
if (this.log.isDebugEnabled()) {
108
this.log.debug("HTTP connection " + conn + ": Output ready");
110
this.handler.outputReady(conn, encoder);
111
if (this.log.isDebugEnabled()) {
112
this.log.debug("HTTP connection " + conn + ": Content encoder " + encoder);
116
public void responseReady(final NHttpServerConnection conn) {
117
if (this.log.isDebugEnabled()) {
118
this.log.debug("HTTP connection " + conn + ": Response ready");
120
this.handler.responseReady(conn);
123
public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
124
if (this.log.isDebugEnabled()) {
125
this.log.debug("HTTP connection " + conn + ": Input ready");
127
this.handler.inputReady(conn, decoder);
128
if (this.log.isDebugEnabled()) {
129
this.log.debug("HTTP connection " + conn + ": Content decoder " + decoder);
133
public void timeout(final NHttpServerConnection conn) {
134
if (this.log.isDebugEnabled()) {
135
this.log.debug("HTTP connection " + conn + ": Timeout");
137
this.handler.timeout(conn);
b'\\ No newline at end of file'