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.IOException;
30  
31  import org.apache.http.Consts;
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.io.HttpTransportMetrics;
34  import org.apache.http.io.SessionOutputBuffer;
35  import org.apache.http.util.CharArrayBuffer;
36  
37  /**
38   * Logs all data written to the wire LOG.
39   * @since 4.0
40   * @deprecated (4.3) no longer used.
41   */
42  @Immutable
43  @Deprecated
44  public class LoggingSessionOutputBuffer implements SessionOutputBuffer {
45  
46      /** Original data transmitter. */
47      private final SessionOutputBuffer out;
48  
49      /** The wire log to use. */
50      private final Wire wire;
51  
52      private final String charset;
53  
54      /**
55       * Create an instance that wraps the specified session output buffer.
56       * @param out The session output buffer.
57       * @param wire The Wire log to use.
58       * @param charset protocol charset, {@code ASCII} if {@code null}
59       */
60      public LoggingSessionOutputBuffer(
61              final SessionOutputBuffer out, final Wire wire, final String charset) {
62          super();
63          this.out = out;
64          this.wire = wire;
65          this.charset = charset != null ? charset : Consts.ASCII.name();
66      }
67  
68      public LoggingSessionOutputBuffer(final SessionOutputBuffer out, final Wire wire) {
69          this(out, wire, null);
70      }
71  
72      public void write(final byte[] b, final int off, final int len) throws IOException {
73          this.out.write(b,  off,  len);
74          if (this.wire.enabled()) {
75              this.wire.output(b, off, len);
76          }
77      }
78  
79      public void write(final int b) throws IOException {
80          this.out.write(b);
81          if (this.wire.enabled()) {
82              this.wire.output(b);
83          }
84      }
85  
86      public void write(final byte[] b) throws IOException {
87          this.out.write(b);
88          if (this.wire.enabled()) {
89              this.wire.output(b);
90          }
91      }
92  
93      public void flush() throws IOException {
94          this.out.flush();
95      }
96  
97      public void writeLine(final CharArrayBuffer buffer) throws IOException {
98          this.out.writeLine(buffer);
99          if (this.wire.enabled()) {
100             final String s = new String(buffer.buffer(), 0, buffer.length());
101             final String tmp = s + "\r\n";
102             this.wire.output(tmp.getBytes(this.charset));
103         }
104     }
105 
106     public void writeLine(final String s) throws IOException {
107         this.out.writeLine(s);
108         if (this.wire.enabled()) {
109             final String tmp = s + "\r\n";
110             this.wire.output(tmp.getBytes(this.charset));
111         }
112     }
113 
114     public HttpTransportMetrics getMetrics() {
115         return this.out.getMetrics();
116     }
117 
118 }