View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jdigg.requests;
17  
18  import net.sf.jdigg.arguments.AbstractArguments;
19  import net.sf.jdigg.util.Assert;
20  
21  import org.apache.commons.httpclient.Credentials;
22  import org.apache.commons.httpclient.HostConfiguration;
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.UsernamePasswordCredentials;
25  import org.apache.commons.httpclient.auth.AuthScope;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * Base request class for a Digg request.
31   * <p>Please read this before you use the API:
32   * <a href="http://apidoc.digg.com/BasicConcepts#BePolitePlease">Be Polite, Please!</a></p>
33   * @author Philip May
34   * @since 1.0 M1
35   */
36  public abstract class AbstractRequest {
37  
38      protected final Log log = LogFactory.getLog(getClass());
39  
40      public static final String DIGG_SERVICE_URL = "http://services.digg.com";
41  
42      private RequestConfig requestConfig;
43  
44      public AbstractRequest(RequestConfig requestConfig) {
45          Assert.notNull(requestConfig, "The RequestConfig must not be 'null'!");
46          this.requestConfig = requestConfig;
47      }
48  
49      protected HttpClient getHttpClient() {
50          HttpClient httpClient = new HttpClient();
51          RequestConfig requestConfig = getRequestConfig();
52  
53          ProxyHost proxyHost = requestConfig.getProxyHost();
54          if (proxyHost != null) {
55              HostConfiguration hostConfiguration = httpClient.getHostConfiguration();
56              hostConfiguration.setProxy(proxyHost.getProxyHost(), proxyHost.getProxyPort());
57              httpClient.setHostConfiguration(hostConfiguration);
58  
59              ProxyUser proxyUser = proxyHost.getProxyUser();
60              if (proxyUser != null) {
61                  Credentials defaultcreds =
62                          new UsernamePasswordCredentials(proxyUser.getUserName(), proxyUser.getPassword());
63                  httpClient.getState().setProxyCredentials(AuthScope.ANY, defaultcreds);
64              }
65          }
66  
67          return httpClient;
68      }
69  
70      protected String buildRequestString(String endpoint, AbstractArguments listStoriesArguments) {
71          return endpoint + "?" + listStoriesArguments.getUrlEncodedArgumentList();
72      }
73  
74      protected RequestConfig getRequestConfig() {
75          return requestConfig;
76      }
77  
78  }