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