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.RequestConformance;
32  import org.apache.hc.core5.http.protocol.RequestConnControl;
33  import org.apache.hc.core5.http.protocol.RequestContent;
34  import org.apache.hc.core5.http.protocol.RequestExpectContinue;
35  import org.apache.hc.core5.http.protocol.RequestTargetHost;
36  import org.apache.hc.core5.http.protocol.RequestUserAgent;
37  import org.apache.hc.core5.http.protocol.RequestValidateHost;
38  import org.apache.hc.core5.http.protocol.ResponseConformance;
39  import org.apache.hc.core5.http.protocol.ResponseConnControl;
40  import org.apache.hc.core5.http.protocol.ResponseContent;
41  import org.apache.hc.core5.http.protocol.ResponseDate;
42  import org.apache.hc.core5.http.protocol.ResponseServer;
43  import org.apache.hc.core5.util.TextUtils;
44  import org.apache.hc.core5.util.VersionInfo;
45  
46  /**
47   * Factory class for standard {@link HttpProcessor} instances.
48   *
49   * @since 5.0
50   */
51  public final class HttpProcessors {
52  
53      private final static String SOFTWARE = "Apache-HttpCore";
54  
55      /**
56       * Creates {@link HttpProcessorBuilder} initialized with default protocol interceptors
57       * for server side HTTP/1.1 processing.
58       *
59       * @param serverInfo the server info text or {@code null} for default.
60       * @return the processor builder.
61       */
62      public static HttpProcessorBuilder customServer(final String serverInfo) {
63          return HttpProcessorBuilder.create()
64                  .addAll(
65                          ResponseConformance.INSTANCE,
66                          ResponseDate.INSTANCE,
67                          new ResponseServer(!TextUtils.isBlank(serverInfo) ? serverInfo :
68                                  VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)),
69                          ResponseContent.INSTANCE,
70                          ResponseConnControl.INSTANCE)
71                  .addAll(
72                          RequestValidateHost.INSTANCE,
73                          RequestConformance.INSTANCE);
74      }
75  
76      /**
77       * Creates {@link HttpProcessor} initialized with default protocol interceptors
78       * for server side HTTP/1.1 processing.
79       *
80       * @param serverInfo the server info text or {@code null} for default.
81       * @return the processor.
82       */
83      public static HttpProcessor server(final String serverInfo) {
84          return customServer(serverInfo).build();
85      }
86  
87      /**
88       * Creates {@link HttpProcessor} initialized with default protocol interceptors
89       * for server side HTTP/1.1 processing.
90       *
91       * @return the processor.
92       */
93      public static HttpProcessor server() {
94          return customServer(null).build();
95      }
96  
97      /**
98       * Creates {@link HttpProcessorBuilder} initialized with default protocol interceptors
99       * for client side HTTP/1.1 processing.
100      *
101      * @param agentInfo the agent info text or {@code null} for default.
102      * @return the processor builder.
103      */
104     public static HttpProcessorBuilder customClient(final String agentInfo) {
105         return HttpProcessorBuilder.create()
106                 .addAll(
107                         RequestTargetHost.INSTANCE,
108                         RequestContent.INSTANCE,
109                         RequestConnControl.INSTANCE,
110                         new RequestUserAgent(!TextUtils.isBlank(agentInfo) ? agentInfo :
111                                 VersionInfo.getSoftwareInfo(SOFTWARE, "org.apache.hc.core5", HttpProcessors.class)),
112                         RequestExpectContinue.INSTANCE);
113     }
114 
115     /**
116      * Creates {@link HttpProcessor} initialized with default protocol interceptors
117      * for client side HTTP/1.1 processing.
118      *
119      * @param agentInfo the agent info text or {@code null} for default.
120      * @return the processor.
121      */
122     public static HttpProcessor client(final String agentInfo) {
123         return customClient(agentInfo).build();
124     }
125 
126     /**
127      * Creates {@link HttpProcessor} initialized with default protocol interceptors
128      * for client side HTTP/1.1 processing.
129      *
130      * @return the processor.
131      */
132     public static HttpProcessor client() {
133         return customClient(null).build();
134     }
135 
136 }