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.hc.core5.http.impl;
28  
29  import org.apache.hc.core5.http.protocol.HttpProcessor;
30  import org.apache.hc.core5.http.protocol.HttpProcessorBuilder;
31  import org.apache.hc.core5.http.protocol.RequestConnControl;
32  import org.apache.hc.core5.http.protocol.RequestContent;
33  import org.apache.hc.core5.http.protocol.RequestExpectContinue;
34  import org.apache.hc.core5.http.protocol.RequestTargetHost;
35  import org.apache.hc.core5.http.protocol.RequestUserAgent;
36  import org.apache.hc.core5.http.protocol.RequestValidateHost;
37  import org.apache.hc.core5.http.protocol.ResponseConnControl;
38  import org.apache.hc.core5.http.protocol.ResponseContent;
39  import org.apache.hc.core5.http.protocol.ResponseDate;
40  import org.apache.hc.core5.http.protocol.ResponseServer;
41  import org.apache.hc.core5.util.TextUtils;
42  import org.apache.hc.core5.util.VersionInfo;
43  
44  /**
45   * @since 5.0
46   */
47  public final class HttpProcessors {
48  
49      private final static String SOFTWARE = "Apache-HttpCore";
50  
51      public static HttpProcessorBuilder customServer(final String serverInfo) {
52          return HttpProcessorBuilder.create()
53                  .addAll(
54                          new ResponseDate(),
55                          new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo :
56                                  VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)),
57                          new ResponseContent(),
58                          new ResponseConnControl())
59                  .addAll(
60                          new RequestValidateHost());
61      }
62  
63      public static HttpProcessor server(final String serverInfo) {
64          return customServer(serverInfo).build();
65      }
66  
67      public static HttpProcessor server() {
68          return customServer(null).build();
69      }
70  
71      public static HttpProcessorBuilder customClient(final String agentInfo) {
72          return HttpProcessorBuilder.create()
73                  .addAll(
74                          new RequestContent(),
75                          new RequestTargetHost(),
76                          new RequestConnControl(),
77                          new RequestUserAgent(!TextUtils.isBlank(agentInfo) ? agentInfo :
78                                  VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)),
79                          new RequestExpectContinue());
80      };
81  
82      public static HttpProcessor client(final String agentInfo) {
83          return customClient(agentInfo).build();
84      }
85  
86      public static HttpProcessor client() {
87          return customClient(null).build();
88      }
89  
90  }