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      @Override
73      public void write(final byte[] b, final int off, final int len) throws IOException {
74          this.out.write(b,  off,  len);
75          if (this.wire.enabled()) {
76              this.wire.output(b, off, len);
77          }
78      }
79  
80      @Override
81      public void write(final int b) throws IOException {
82          this.out.write(b);
83          if (this.wire.enabled()) {
84              this.wire.output(b);
85          }
86      }
87  
88      @Override
89      public void write(final byte[] b) throws IOException {
90          this.out.write(b);
91          if (this.wire.enabled()) {
92              this.wire.output(b);
93          }
94      }
95  
96      @Override
97      public void flush() throws IOException {
98          this.out.flush();
99      }
100 
101     @Override
102     public void writeLine(final CharArrayBuffer buffer) throws IOException {
103         this.out.writeLine(buffer);
104         if (this.wire.enabled()) {
105             final String s = new String(buffer.buffer(), 0, buffer.length());
106             final String tmp = s + "\r\n";
107             this.wire.output(tmp.getBytes(this.charset));
108         }
109     }
110 
111     @Override
112     public void writeLine(final String s) throws IOException {
113         this.out.writeLine(s);
114         if (this.wire.enabled()) {
115             final String tmp = s + "\r\n";
116             this.wire.output(tmp.getBytes(this.charset));
117         }
118     }
119 
120     @Override
121     public HttpTransportMetrics getMetrics() {
122         return this.out.getMetrics();
123     }
124 
125 }