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.URIScheme;
39  import org.apache.hc.core5.http.config.CharCodingConfig;
40  import org.apache.hc.core5.http.config.Http1Config;
41  import org.apache.hc.core5.http.impl.CharCodingSupport;
42  import org.apache.hc.core5.http.io.HttpConnectionFactory;
43  import org.apache.hc.core5.http.io.HttpMessageParserFactory;
44  import org.apache.hc.core5.http.io.HttpMessageWriterFactory;
45  
46  public class LoggingBHttpServerConnectionFactory implements HttpConnectionFactory<LoggingBHttpServerConnection> {
47  
48      public static final LoggingBHttpServerConnectionFactoryrConnectionFactory.html#LoggingBHttpServerConnectionFactory">LoggingBHttpServerConnectionFactory INSTANCE = new LoggingBHttpServerConnectionFactory();
49  
50      private final String scheme;
51      private final Http1Config http1Config;
52      private final CharCodingConfig charCodingConfig;
53      private final ContentLengthStrategy incomingContentStrategy;
54      private final ContentLengthStrategy outgoingContentStrategy;
55      private final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory;
56      private final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory;
57  
58      public LoggingBHttpServerConnectionFactory(
59              final String scheme,
60              final Http1Config http1Config,
61              final CharCodingConfig charCodingConfig,
62              final ContentLengthStrategy incomingContentStrategy,
63              final ContentLengthStrategy outgoingContentStrategy,
64              final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
65              final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
66          super();
67          this.scheme = scheme;
68          this.http1Config = http1Config != null ? http1Config : Http1Config.DEFAULT;
69          this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
70          this.incomingContentStrategy = incomingContentStrategy;
71          this.outgoingContentStrategy = outgoingContentStrategy;
72          this.requestParserFactory = requestParserFactory;
73          this.responseWriterFactory = responseWriterFactory;
74      }
75  
76      public LoggingBHttpServerConnectionFactory(
77              final String scheme,
78              final Http1Config http1Config,
79              final CharCodingConfig charCodingConfig,
80              final HttpMessageParserFactory<ClassicHttpRequest> requestParserFactory,
81              final HttpMessageWriterFactory<ClassicHttpResponse> responseWriterFactory) {
82          this(scheme, http1Config, charCodingConfig, null, null, requestParserFactory, responseWriterFactory);
83      }
84  
85      public LoggingBHttpServerConnectionFactory(
86              final String scheme,
87              final Http1Config http1Config,
88              final CharCodingConfig charCodingConfig) {
89          this(scheme, http1Config, charCodingConfig, null, null, null, null);
90      }
91  
92      public LoggingBHttpServerConnectionFactory() {
93          this(null, null, null, null, null, null, null);
94      }
95  
96      @Override
97      public LoggingBHttpServerConnection createConnection(final Socket socket) throws IOException {
98          final LoggingBHttpServerConnectiongBHttpServerConnection.html#LoggingBHttpServerConnection">LoggingBHttpServerConnection conn = new LoggingBHttpServerConnection(
99                  scheme != null ? scheme : (socket instanceof SSLSocket ? URIScheme.HTTPS.id : URIScheme.HTTP.id),
100                 http1Config,
101                 CharCodingSupport.createDecoder(charCodingConfig),
102                 CharCodingSupport.createEncoder(charCodingConfig),
103                 incomingContentStrategy,
104                 outgoingContentStrategy,
105                 requestParserFactory,
106                 responseWriterFactory);
107         conn.bind(socket);
108         return conn;
109     }
110 
111 }