View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.conn;
28  
29  import java.io.ByteArrayInputStream;
30  import java.io.IOException;
31  import java.io.InputStream;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.util.Args;
37  
38  /**
39   * Logs data to the wire LOG.
40   * TODO: make package private. Should not be part of the public API.
41   *
42   * @since 4.0
43   */
44  @Contract(threading = ThreadingBehavior.IMMUTABLE)
45  public class Wire {
46  
47      private final Log log;
48      private final String id;
49  
50      /**
51       * @since 4.3
52       */
53      public Wire(final Log log, final String id) {
54          this.log = log;
55          this.id = id;
56      }
57  
58      public Wire(final Log log) {
59          this(log, "");
60      }
61  
62      private void wire(final String header, final InputStream instream)
63        throws IOException {
64          final StringBuilder buffer = new StringBuilder();
65          int ch;
66          while ((ch = instream.read()) != -1) {
67              if (ch == 13) {
68                  buffer.append("[\\r]");
69              } else if (ch == 10) {
70                      buffer.append("[\\n]\"");
71                      buffer.insert(0, "\"");
72                      buffer.insert(0, header);
73                      log.debug(id + " " + buffer.toString());
74                      buffer.setLength(0);
75              } else if ((ch < 32) || (ch > 127)) {
76                  buffer.append("[0x");
77                  buffer.append(Integer.toHexString(ch));
78                  buffer.append("]");
79              } else {
80                  buffer.append((char) ch);
81              }
82          }
83          if (buffer.length() > 0) {
84              buffer.append('\"');
85              buffer.insert(0, '\"');
86              buffer.insert(0, header);
87              log.debug(id + " " + buffer.toString());
88          }
89      }
90  
91  
92      public boolean enabled() {
93          return log.isDebugEnabled();
94      }
95  
96      public void output(final InputStream outstream)
97        throws IOException {
98          Args.notNull(outstream, "Output");
99          wire(">> ", outstream);
100     }
101 
102     public void input(final InputStream instream)
103       throws IOException {
104         Args.notNull(instream, "Input");
105         wire("<< ", instream);
106     }
107 
108     public void output(final byte[] b, final int off, final int len)
109       throws IOException {
110         Args.notNull(b, "Output");
111         wire(">> ", new ByteArrayInputStream(b, off, len));
112     }
113 
114     public void input(final byte[] b, final int off, final int len)
115       throws IOException {
116         Args.notNull(b, "Input");
117         wire("<< ", new ByteArrayInputStream(b, off, len));
118     }
119 
120     public void output(final byte[] b)
121       throws IOException {
122         Args.notNull(b, "Output");
123         wire(">> ", new ByteArrayInputStream(b));
124     }
125 
126     public void input(final byte[] b)
127       throws IOException {
128         Args.notNull(b, "Input");
129         wire("<< ", new ByteArrayInputStream(b));
130     }
131 
132     public void output(final int b)
133       throws IOException {
134         output(new byte[] {(byte) b});
135     }
136 
137     public void input(final int b)
138       throws IOException {
139         input(new byte[] {(byte) b});
140     }
141 
142     public void output(final String s)
143       throws IOException {
144         Args.notNull(s, "Output");
145         output(s.getBytes());
146     }
147 
148     public void input(final String s)
149       throws IOException {
150         Args.notNull(s, "Input");
151         input(s.getBytes());
152     }
153 }