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 org.apache.hc.core5.http.ClassicHttpRequest;
34  import org.apache.hc.core5.http.ClassicHttpResponse;
35  import org.apache.hc.core5.http.ContentLengthStrategy;
36  import org.apache.hc.core5.http.config.CharCodingConfig;
37  import org.apache.hc.core5.http.config.Http1Config;
38  import org.apache.hc.core5.http.impl.CharCodingSupport;
39  import org.apache.hc.core5.http.io.HttpConnectionFactory;
40  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
41  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
42  
43  public class LoggingBHttpClientConnectionFactory implements HttpConnectionFactory<LoggingBHttpClientConnection> {
44  
45      public static final LoggingBHttpClientConnectionFactorytConnectionFactory.html#LoggingBHttpClientConnectionFactory">LoggingBHttpClientConnectionFactory INSTANCE = new LoggingBHttpClientConnectionFactory();
46  
47      private final Http1Config http1Config;
48      private final CharCodingConfig charCodingConfig;
49      private final ContentLengthStrategy incomingContentStrategy;
50      private final ContentLengthStrategy outgoingContentStrategy;
51      private final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory;
52      private final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory;
53  
54      public LoggingBHttpClientConnectionFactory(
55              final Http1Config http1Config,
56              final CharCodingConfig charCodingConfig,
57              final ContentLengthStrategy incomingContentStrategy,
58              final ContentLengthStrategy outgoingContentStrategy,
59              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
60              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
61          super();
62          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
63          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
64          this.incomingContentStrategy = incomingContentStrategy;
65          this.outgoingContentStrategy = outgoingContentStrategy;
66          this.requestWriterFactory = requestWriterFactory;
67          this.responseParserFactory = responseParserFactory;
68      }
69  
70      public LoggingBHttpClientConnectionFactory(
71              final Http1Config http1Config,
72              final CharCodingConfig charCodingConfig,
73              final HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
74              final HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory) {
75          this(http1Config, charCodingConfig, null, null, requestWriterFactory, responseParserFactory);
76      }
77  
78      public LoggingBHttpClientConnectionFactory(
79              final Http1Config http1Config,
80              final CharCodingConfig charCodingConfig) {
81          this(http1Config, charCodingConfig, null, null, null, null);
82      }
83  
84      public LoggingBHttpClientConnectionFactory() {
85          this(null, null, null, null, null, null);
86      }
87  
88      @Override
89      public LoggingBHttpClientConnection createConnection(final Socket socket) throws IOException {
90          final LoggingBHttpClientConnectiongBHttpClientConnection.html#LoggingBHttpClientConnection">LoggingBHttpClientConnection conn = new LoggingBHttpClientConnection(
91                  http1Config,
92                  CharCodingSupport.createDecoder(charCodingConfig),
93                  CharCodingSupport.createEncoder(charCodingConfig),
94                  incomingContentStrategy,
95                  outgoingContentStrategy,
96                  requestWriterFactory,
97                  responseParserFactory);
98          conn.bind(socket);
99          return conn;
100     }
101 
102 }