~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to contrib/src/main/java/org/apache/http/contrib/logging/LoggingNHttpServiceHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 744512 $
 
4
 * $Date: 2009-02-14 17:34:15 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.contrib.logging;
 
33
 
 
34
import java.io.IOException;
 
35
 
 
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;
 
45
 
 
46
/**
 
47
 * Decorator class intended to transparently extend an {@link NHttpServiceHandler} 
 
48
 * with basic event logging capabilities using Commons Logging. 
 
49
 * 
 
50
 */
 
51
public class LoggingNHttpServiceHandler implements NHttpServiceHandler {
 
52
 
 
53
    private final Log log;
 
54
    private final Log headerlog;
 
55
    private final NHttpServiceHandler handler;
 
56
    
 
57
    public LoggingNHttpServiceHandler(final NHttpServiceHandler handler) {
 
58
        super();
 
59
        if (handler == null) {
 
60
            throw new IllegalArgumentException("HTTP service handler may not be null");
 
61
        }
 
62
        this.handler = handler;
 
63
        this.log = LogFactory.getLog(handler.getClass());
 
64
        this.headerlog = LogFactory.getLog("org.apache.http.headers");
 
65
    }
 
66
    
 
67
    public void connected(final NHttpServerConnection conn) {
 
68
        if (this.log.isDebugEnabled()) {
 
69
            this.log.debug("HTTP connection " + conn + ": Connected");
 
70
        }
 
71
        this.handler.connected(conn);
 
72
    }
 
73
 
 
74
    public void closed(final NHttpServerConnection conn) {
 
75
        if (this.log.isDebugEnabled()) {
 
76
            this.log.debug("HTTP connection " + conn + ": Closed");
 
77
        }
 
78
        this.handler.closed(conn);
 
79
    }
 
80
 
 
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);
 
84
    }
 
85
 
 
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);
 
89
    }
 
90
 
 
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());
 
95
        }
 
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());
 
102
            }
 
103
        }
 
104
    }
 
105
 
 
106
    public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) {
 
107
        if (this.log.isDebugEnabled()) {
 
108
            this.log.debug("HTTP connection " + conn + ": Output ready");
 
109
        }
 
110
        this.handler.outputReady(conn, encoder);
 
111
        if (this.log.isDebugEnabled()) {
 
112
            this.log.debug("HTTP connection " + conn + ": Content encoder " + encoder);
 
113
        }
 
114
    }
 
115
 
 
116
    public void responseReady(final NHttpServerConnection conn) {
 
117
        if (this.log.isDebugEnabled()) {
 
118
            this.log.debug("HTTP connection " + conn + ": Response ready");
 
119
        }
 
120
        this.handler.responseReady(conn);
 
121
    }
 
122
 
 
123
    public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) {
 
124
        if (this.log.isDebugEnabled()) {
 
125
            this.log.debug("HTTP connection " + conn + ": Input ready");
 
126
        }
 
127
        this.handler.inputReady(conn, decoder);
 
128
        if (this.log.isDebugEnabled()) {
 
129
            this.log.debug("HTTP connection " + conn + ": Content decoder " + decoder);
 
130
        }
 
131
    }
 
132
 
 
133
    public void timeout(final NHttpServerConnection conn) {
 
134
        if (this.log.isDebugEnabled()) {
 
135
            this.log.debug("HTTP connection " + conn + ": Timeout");
 
136
        }
 
137
        this.handler.timeout(conn);
 
138
    }
 
139
 
 
140
}
 
 
b'\\ No newline at end of file'