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
47       *            the user agent descriptor string.
48       * @param defaultTimeout
49       *            the default timeout, cannot be <code>&lt;&#61; to 0</code>
50       * @param maxConnections
51       *            the default max connections, cannot be <code>&lt;&#61; to 0</code>
52       * @param acceptHeader
53       *            the accept header string, can be <code>null</code>.
54       */
55      public DefaultHTTPClientConfiguration(String userAgent, int defaultTimeout, int maxConnections,
56              String acceptHeader) {
57          if (userAgent == null)
58              throw new IllegalArgumentException("userAgent cannot be null.");
59          if (defaultTimeout <= 0)
60              throw new IllegalArgumentException("defaultTimeout cannot be <= 0 .");
61          if (maxConnections <= 0)
62              throw new IllegalArgumentException("maxConnections cannot be <= 0 .");
63          this.userAgent = userAgent;
64          this.defaultTimeout = defaultTimeout;
65          this.maxConnections = maxConnections;
66          this.acceptHeader = acceptHeader;
67      }
68  
69      /**
70       * Constructor. initialized with default {@link DefaultConfiguration} parameters
71       *
72       * @param acceptHeader
73       *            the value to initialize <code>acceptHeader</code>.
74       */
75      public DefaultHTTPClientConfiguration(String acceptHeader) {
76          this(DefaultConfiguration.singleton().getPropertyOrFail("any23.http.user.agent.default"),
77                  DefaultConfiguration.singleton().getPropertyIntOrFail("any23.http.client.timeout"),
78                  DefaultConfiguration.singleton().getPropertyIntOrFail("any23.http.client.max.connections"),
79                  acceptHeader);
80      }
81  
82      /**
83       * Constructor. initialized with default {@link DefaultConfiguration} parameters and <code>acceptHeader=null</code>.
84       */
85      public DefaultHTTPClientConfiguration() {
86          this(null);
87      }
88  
89      public String getUserAgent() {
90          return userAgent;
91      }
92  
93      public int getDefaultTimeout() {
94          return defaultTimeout;
95      }
96  
97      public int getMaxConnections() {
98          return maxConnections;
99      }
100 
101     public String getAcceptHeader() {
102         return acceptHeader;
103     }
104 
105 }