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