View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.any23.http;
19  
20  import org.apache.any23.configuration.DefaultConfiguration;
21  
22  /**
23   * Default implementation of {@link HTTPClientConfiguration}.
24   *
25   * @author Michele Mostarda (mostarda@fbk.eu)
26   */
27  public class DefaultHTTPClientConfiguration implements HTTPClientConfiguration {
28  
29      private static DefaultHTTPClientConfiguration instance;
30  
31      public static DefaultHTTPClientConfiguration singleton() {
32          if(instance == null) {
33              instance = new DefaultHTTPClientConfiguration();
34          }
35          return instance;
36      }
37  
38      private String userAgent;
39      private int    defaultTimeout;
40      private int    maxConnections;
41      private String acceptHeader;
42  
43      /**
44       * Constructor.
45       *
46       * @param userAgent the user agent descriptor string.
47       * @param defaultTimeout the default timeout, cannot be <code>&lt;&#61; to 0</code>
48       * @param maxConnections the default max connections, cannot be <code>&lt;&#61; to 0</code>
49       * @param acceptHeader the accept header string, can be <code>null</code>.
50       */
51      public DefaultHTTPClientConfiguration(
52              String userAgent, int defaultTimeout, int maxConnections, String acceptHeader
53      ) {
54          if(userAgent == null)   throw new IllegalArgumentException("userAgent cannot be null.");
55          if(defaultTimeout <= 0) throw new IllegalArgumentException("defaultTimeout cannot be <= 0 .");
56          if(maxConnections <= 0) throw new IllegalArgumentException("maxConnections cannot be <= 0 .");
57          this.userAgent      = userAgent;
58          this.defaultTimeout = defaultTimeout;
59          this.maxConnections = maxConnections;
60          this.acceptHeader   = acceptHeader;
61      }
62  
63      /**
64       * Constructor.
65       * initialized with default {@link DefaultConfiguration} parameters
66       *
67       * @param acceptHeader the value to initialize <code>acceptHeader</code>.
68       */
69      public DefaultHTTPClientConfiguration(String acceptHeader) {
70          this(
71                  DefaultConfiguration.singleton().getPropertyOrFail   ("any23.http.user.agent.default"),
72                  DefaultConfiguration.singleton().getPropertyIntOrFail("any23.http.client.timeout"),
73                  DefaultConfiguration.singleton().getPropertyIntOrFail("any23.http.client.max.connections"),
74                  acceptHeader
75          );
76      }
77  
78      /**
79       * Constructor.
80       * initialized with default {@link DefaultConfiguration} parameters and <code>acceptHeader=null</code>.
81       */
82      public DefaultHTTPClientConfiguration() {
83          this(null);
84      }
85  
86      public String getUserAgent() {
87          return userAgent;
88      }
89  
90      public int getDefaultTimeout() {
91          return defaultTimeout;
92      }
93  
94      public int getMaxConnections() {
95          return maxConnections;
96      }
97  
98      public String getAcceptHeader() {
99          return acceptHeader;
100     }
101 
102 }