1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.amsterdam.plugins.http;
21
22 import java.util.*;
23 import java.io.*;
24 import java.net.URL;
25 import HTTPClient.*;
26
27 import org.w3c.dom.Element;
28
29 import org.backsource.amsterdam.service.protocol.ProtocolHandler;
30 import org.backsource.amsterdam.service.protocol.GETProtocolHandler;
31 import org.backsource.amsterdam.service.protocol.GETProtocolHandlerSupport;
32 import org.backsource.amsterdam.service.protocol.ListEntry;
33 import org.backsource.amsterdam.service.protocol.ProtocolConfigure;
34 import org.backsource.amsterdam.service.ServiceMessage;
35 import org.backsource.amsterdam.service.ServiceInvoker;
36 import org.backsource.amsterdam.service.ServiceException;
37
38 import HTTPClient.*;
39
40 /*** HTTPProtocolHandler - retrieve GET/HTTP resources.
41 *
42 * @author Mats Nyberg <mn@tim.se>
43 * @version $Revision: 1.1.1.1 $
44 */
45
46 public class HTTPProtocolHandler extends GETProtocolHandlerSupport {
47
48 public static final int DEFAULT_TIMEOUT = 3000;
49
50 private String host = null;
51 private int port = 80;
52 private String baseURL = "/";
53 private int retryTimes = 0;
54 private int retryDelay = 0;
55 private int timeOut = 0;
56 private String user = null;
57 private String pass = null;
58 private String scheeme = null;
59 private String realm = null;
60
61
62
63 public HTTPProtocolHandler(ProtocolConfigure conf) throws ServiceException{
64 setConfiguration(conf);
65 }
66
67
68 private int getIntConfigParam(String entry, int defaultValue){
69 try{
70
71 return new Integer(entry).intValue();
72
73 }catch(Exception e){}
74
75 return defaultValue;
76 }
77
78
79 private void setConfiguration(ProtocolConfigure conf) throws ServiceException {
80 String host = conf.getEntry("Host");
81 String port = conf.getEntry("Port");
82 String baseURL = conf.getEntry("BaseURL");
83 String timeOut = conf.getEntry("TimeOut");
84 String retryTimes = conf.getEntry("RetryTimes");
85 String retryDelay = conf.getEntry("RetryDelay");
86
87 user = conf.getEntry("User");
88 pass = conf.getEntry("Pass");
89 scheeme = conf.getEntry("Scheeme");
90 realm = conf.getEntry("Realm");
91
92
93 if(host != null)
94 this.host = host;
95 else
96 throw new ServiceException("No host specified");
97
98 if(port != null)
99 this.port = getIntConfigParam(port, 8080);
100
101 if(baseURL != null)
102 this.baseURL = baseURL;
103 else
104 throw new ServiceException("No URL specified");
105
106 if(timeOut != null)
107 this.timeOut = getIntConfigParam(timeOut, DEFAULT_TIMEOUT);
108
109 if(retryTimes != null)
110 this.retryTimes = getIntConfigParam(retryTimes, 0);
111
112 if(retryDelay != null)
113 this.retryDelay = getIntConfigParam(retryDelay, 0);
114
115 }
116
117
118 public void close() {}
119
120 public Iterator list(String url) throws ServiceException{
121 invoker.getService().getLogger().debug("Doing list");
122 if(url == null) {
123 invoker.getService().getLogger().warn("No url, returning empty iterator");
124 return new Vector().iterator();
125 }else if ( url.endsWith("/") || url.equals(".") ){
126 return new Vector().iterator();
127 }
128 Vector vector = new Vector();
129 try{
130 ListEntry entry = new ListEntry();
131 HTTPConnection connection = new HTTPConnection(host, port);
132 connection.setTimeout(timeOut);
133 URI uri = new URI( HTTPProtocolHandlerFactory.PROTOCOL, host, port, baseURL);
134 uri = new URI(uri, url);
135
136
137 HTTPResponse response = connection.Head(uri.getPath());
138 Date lastModified = response.getHeaderAsDate("Last-Modified");
139
140 entry.setUrl(url);
141 entry.setTimestamp(lastModified);
142 vector.add(entry);
143 }catch(Exception ex){
144 throw new ServiceException("Could not list url: " + url + " " + ex);
145 }
146 return vector.iterator();
147 }
148
149
150
151
152 ServiceMessage get(URL url)throws ServiceException {
153
154 HTTPConnection connection = null;
155
156 try{
157
158 connection = new HTTPConnection(url);
159 connection.setTimeout(timeOut);
160
161 } catch (ProtocolNotSuppException notSuported){
162 System.out.println(notSuported);
163 throw new ServiceException("Could not fetch url: " + url.toString(),notSuported);
164 } catch (Exception something){
165
166 throw new ServiceException("Could not fetch url: " + url.toString(),something);
167 }
168
169
170 Exception lastEx = null;
171 for(int i=0; i <= retryTimes; i++){
172
173 try{
174 HTTPResponse response = connection.Get(url.getFile());
175 if (response.getStatusCode() == 200) {
176
177 String contentType = response.getHeader("Content-Type");
178 String data = new String( response.getData() );
179 return new ServiceMessage(url.toString(), contentType, response.getInputStream());
180 } else {
181 throw new ServiceException("Error in getting " + url.toString() + response.getReasonLine() + " code: " + response.getStatusCode());
182 }
183 } catch(Exception exception){
184 lastEx = exception;
185 invoker.getService().getLogger().warn(exception.toString());
186
187 connection.stop();
188 try{
189 if(retryDelay > 0){
190 synchronized(this){
191 wait(retryDelay);
192 }
193 }
194 } catch(InterruptedException interrupted){
195
196 }
197 }
198 }
199
200
201 throw new ServiceException(lastEx);
202 }
203
204
205
206
207 public ServiceMessage get(ListEntry entry) throws ServiceException { return get(entry.getUrl()); }
208
209 public ServiceMessage get(String end) throws ServiceException{
210 invoker.getService().getLogger().debug("Getting " + baseURL + "/" +end);
211 try{
212 java.net.URL url = new java.net.URL("http", host, port, baseURL + "/" + end);
213 return get(url);
214 }catch(java.net.MalformedURLException e){
215
216 throw new ServiceException(e);
217 }
218 }
219
220
221
222
223
224
225
226
227
228
229 /***
230 public void importXml(Element element) throws DeploymentException {
231 globalConf.importXml(element);
232 getConfiguration();
233 }
234 */
235 }