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  
28  package org.apache.hc.core5.testing.classic;
29  
30  import java.io.IOException;
31  import java.net.Socket;
32  
33  import javax.net.ssl.SSLSocket;
34  
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.ContentLengthStrategy;
38  import org.apache.hc.core5.http.config.CharCodingConfig;
39  import org.apache.hc.core5.http.config.Http1Config;
40  import org.apache.hc.core5.http.impl.CharCodingSupport;
41  import org.apache.hc.core5.http.io.HttpConnectionFactory;
42  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
43  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
44  
45  public class LoggingBHttpClientConnectionFactory implements HttpConnectionFactory<LoggingBHttpClientConnection> {
46  
47      public static final LoggingBHttpClientConnectionFactory INSTANCE = new LoggingBHttpClientConnectionFactory();
48  
49      private final Http1Config http1Config;
50      private final CharCodingConfig charCodingConfig;
51      private final ContentLengthStrategy incomingContentStrategy;
52      private final ContentLengthStrategy outgoingContentStrategy;
53      private final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
54      private final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
55  
56      public LoggingBHttpClientConnectionFactory(
57              final Http1Config http1Config,
58              final CharCodingConfig charCodingConfig,
59              final ContentLengthStrategy incomingContentStrategy,
60              final ContentLengthStrategy outgoingContentStrategy,
61              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
62              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
63          super();
64          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
65          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
66          this.incomingContentStrategy = incomingContentStrategy;
67          this.outgoingContentStrategy = outgoingContentStrategy;
68          this.requestWriterFactory = requestWriterFactory;
69          this.responseParserFactory = responseParserFactory;
70      }
71  
72      public LoggingBHttpClientConnectionFactory(
73              final Http1Config http1Config,
74              final CharCodingConfig charCodingConfig,
75              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
76              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
77          this(http1Config, charCodingConfig, null, null, requestWriterFactory, responseParserFactory);
78      }
79  
80      public LoggingBHttpClientConnectionFactory(
81              final Http1Config http1Config,
82              final CharCodingConfig charCodingConfig) {
83          this(http1Config, charCodingConfig, null, null, null, null);
84      }
85  
86      public LoggingBHttpClientConnectionFactory() {
87          this(null, null, null, null, null, null);
88      }
89  
90      LoggingBHttpClientConnection createDetached() {
91          return new LoggingBHttpClientConnection(
92                  http1Config,
93                  CharCodingSupport.createDecoder(charCodingConfig),
94                  CharCodingSupport.createEncoder(charCodingConfig),
95                  incomingContentStrategy,
96                  outgoingContentStrategy,
97                  requestWriterFactory,
98                  responseParserFactory);
99      }
100 
101     @Override
102     public LoggingBHttpClientConnection createConnection(final Socket socket) throws IOException {
103         final LoggingBHttpClientConnection conn = createDetached();
104         conn.bind(socket);
105         return conn;
106     }
107 
108     /**
109      * @since 5.3
110      */
111     @Override
112     public LoggingBHttpClientConnection createConnection(final SSLSocket sslSocket, final Socket socket) throws IOException {
113         final LoggingBHttpClientConnection conn = createDetached();
114         conn.bind(sslSocket, socket);
115         return conn;
116     }
117 
118 }