View Javadoc

1   package at.meikel.dmrl.server.httpclient;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.util.Date;
6   
7   import org.apache.http.HttpEntity;
8   import org.apache.http.HttpHost;
9   import org.apache.http.StatusLine;
10  import org.apache.http.client.ClientProtocolException;
11  import org.apache.http.client.config.RequestConfig;
12  import org.apache.http.client.methods.CloseableHttpResponse;
13  import org.apache.http.client.methods.HttpGet;
14  import org.apache.http.impl.client.CloseableHttpClient;
15  import org.apache.http.impl.client.HttpClients;
16  import org.apache.log4j.Logger;
17  
18  import at.meikel.dmrl.server.server.HttpDataRetriever;
19  
20  public class HttpDataRetrieverImpl implements HttpDataRetriever {
21  
22      private String proxyHost;
23      private int proxyPort;
24      private String proxyScheme;
25      private String currentName = null;
26      private Date currentDate = null;
27      private CloseableHttpClient httpClient;
28      private CloseableHttpResponse response;
29  
30  
31      private static final String URL = "http://www.minigolfsport.de/download/";
32      private final static Logger LOGGER = Logger.getLogger(HttpDataRetrieverImpl.class);
33  
34      public HttpDataRetrieverImpl() {
35          this(null, 0);
36      }
37  
38      public HttpDataRetrieverImpl(String proxyHost, int proxyPort) {
39          // See: http://hc.apache.org/httpcomponents-client/tutorial/html/
40          this.proxyHost = proxyHost;
41          this.proxyPort = proxyPort;
42          this.proxyScheme = "http";
43      }
44  
45      @Override
46      public String getProxyHost() {
47          return proxyHost;
48      }
49  
50      @Override
51      public void setProxyHost(String proxyHost) {
52          this.proxyHost = proxyHost;
53      }
54  
55      @Override
56      public int getProxyPort() {
57          return proxyPort;
58      }
59  
60      @Override
61      public void setProxyPort(int proxyPort) {
62          this.proxyPort = proxyPort;
63      }
64  
65      @Override
66      public String getUrl() {
67          return URL;
68      }
69  
70      @Override
71      public InputStream retrieveInputStream() {
72          // See: http://hc.apache.org/httpcomponents-client/tutorial/html/
73  
74          assert currentName != null;
75  
76          InputStream result = null;
77  
78          HttpGet httpGet = new HttpGet(URL + currentName);
79          if (proxyHost != null) {
80              HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyScheme);
81              RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
82              httpGet.setConfig(config);
83              LOGGER.debug("Using proxy (" + proxyHost + "," + proxyPort + "," + proxyScheme + ").");
84          }
85          httpClient = HttpClients.createDefault();
86          try {
87          	response = httpClient.execute(httpGet);
88          	HttpEntity entity = response.getEntity();
89          	if (entity != null) {
90          		result = entity.getContent();
91          	}
92          } catch (ClientProtocolException e) {
93          	LOGGER.warn("Failed to retrieve data from '" + URL + currentName + "'.", e);
94          } catch (IOException e) {
95          	LOGGER.warn("Failed to retrieve data from '" + URL + currentName + "'.", e);
96          }
97  
98          return result;
99      }
100 
101     @Override
102     public void freeAllResources() {
103     	if (response != null) {
104     		try {
105 				response.close();
106 			} catch (IOException e) {
107 				LOGGER.warn("Failed to close http reponse.", e);
108 			}
109     	}
110     	
111     	if (httpClient != null) {
112     		try {
113 				httpClient.close();
114 			} catch (IOException e) {
115 				LOGGER.warn("Failed to close http reponse.", e);
116 			}
117     	}
118     }
119     
120     @Override
121     public boolean findCurrentFile() {
122         // See: http://code.google.com/p/selenium/wiki/GettingStarted
123 
124         currentName = null;
125         currentDate = null;
126 
127         CloseableHttpClient httpClient = HttpClients.createDefault();
128         try {
129         	for (int i=99; i>=45; i--) {
130         		String currentNameInLoop = "rangliste" + Integer.toString(i) + ".xls";
131         		String currentUrl = URL + currentNameInLoop;
132         		HttpGet httpGet = new HttpGet(currentUrl);
133         		if (proxyHost != null) {
134         			HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
135         			RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
136         			httpGet.setConfig(config);
137                     LOGGER.debug("Using proxy (" + proxyHost + "," + proxyPort + "," + proxyScheme + ").");
138         		}
139         		try {
140         			CloseableHttpResponse response = httpClient.execute(httpGet);
141         			try {
142         				StatusLine statusLine = response.getStatusLine();
143         				int statusCode = statusLine.getStatusCode();
144         				LOGGER.debug("Status code for URL '" + currentUrl + "' is " + Integer.toString(statusCode));
145         				if (statusCode == 200) {
146         					currentName = currentNameInLoop;
147         					currentDate = new Date(0);
148         					break;
149         				}
150         			} finally {
151         				response.close();
152         			}
153         		} catch (ClientProtocolException e) {
154         			LOGGER.warn("Failed to check URL '" + currentUrl + "'.", e);
155         		} catch (IOException e) {
156         			LOGGER.warn("Failed to check URL '" + currentUrl + "'.", e);
157         		}
158         	}
159         } finally {
160         	try {
161 				httpClient.close();
162 			} catch (IOException e) {
163 				LOGGER.warn("Error while closing http client.", e);
164 			}
165         }
166 
167         if (LOGGER.isDebugEnabled()) {
168             LOGGER.debug("Found file '" + currentName + "' (" + currentDate
169                     + ")");
170         }
171         
172         return currentName != null;
173     }
174 
175     @Override
176     public String getCurrentFileName() {
177         return currentName;
178     }
179 
180     @Override
181     public Date getCurrentFileDate() {
182         return currentDate;
183     }
184 }