2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: HttpCommunicator2.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.4 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/12/06 16:26:51 $ |
2 |
26 Feb 07 |
jari |
* $Author: wwang67 $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.remote.protocol.communication.http; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.io.BufferedInputStream; |
2 |
26 Feb 07 |
jari |
15 |
import java.io.BufferedOutputStream; |
2 |
26 Feb 07 |
jari |
16 |
import java.io.File; |
2 |
26 Feb 07 |
jari |
17 |
import java.io.FileInputStream; |
2 |
26 Feb 07 |
jari |
18 |
import java.io.FileOutputStream; |
2 |
26 Feb 07 |
jari |
19 |
import java.io.IOException; |
2 |
26 Feb 07 |
jari |
20 |
import java.io.InputStream; |
2 |
26 Feb 07 |
jari |
21 |
import java.io.OutputStream; |
2 |
26 Feb 07 |
jari |
22 |
import java.net.MalformedURLException; |
2 |
26 Feb 07 |
jari |
23 |
import java.net.URL; |
2 |
26 Feb 07 |
jari |
24 |
import java.util.Enumeration; |
2 |
26 Feb 07 |
jari |
25 |
import java.util.Properties; |
2 |
26 Feb 07 |
jari |
26 |
|
2 |
26 Feb 07 |
jari |
27 |
import org.tigr.remote.protocol.util.TempFile; |
2 |
26 Feb 07 |
jari |
28 |
import org.tigr.util.ConfMap; |
2 |
26 Feb 07 |
jari |
29 |
|
2 |
26 Feb 07 |
jari |
30 |
import HTTPClient.CookieModule; |
2 |
26 Feb 07 |
jari |
31 |
import HTTPClient.HTTPConnection; |
2 |
26 Feb 07 |
jari |
32 |
import HTTPClient.HttpURLConnection; |
2 |
26 Feb 07 |
jari |
33 |
|
2 |
26 Feb 07 |
jari |
34 |
public class HttpCommunicator2 { |
2 |
26 Feb 07 |
jari |
35 |
|
2 |
26 Feb 07 |
jari |
36 |
private URL sendURL; |
2 |
26 Feb 07 |
jari |
37 |
private URL receiveURL; |
2 |
26 Feb 07 |
jari |
38 |
private HttpURLConnection connection; |
2 |
26 Feb 07 |
jari |
39 |
private BufferedOutputStream bufferedOutputStream; |
2 |
26 Feb 07 |
jari |
40 |
private InputStream inputStream; |
2 |
26 Feb 07 |
jari |
41 |
private int contentLength = 0; |
2 |
26 Feb 07 |
jari |
42 |
private String context; |
2 |
26 Feb 07 |
jari |
43 |
private String fileName; |
2 |
26 Feb 07 |
jari |
44 |
private boolean fileCreated; |
2 |
26 Feb 07 |
jari |
45 |
private boolean keepTempFile; |
2 |
26 Feb 07 |
jari |
46 |
|
2 |
26 Feb 07 |
jari |
47 |
private static long countInstances = 0; |
2 |
26 Feb 07 |
jari |
48 |
|
2 |
26 Feb 07 |
jari |
49 |
/** |
2 |
26 Feb 07 |
jari |
* Returns an unique name for context. |
2 |
26 Feb 07 |
jari |
51 |
*/ |
2 |
26 Feb 07 |
jari |
52 |
public synchronized static String getUniqueContext() { |
2 |
26 Feb 07 |
jari |
53 |
return("context" + (++countInstances)); |
2 |
26 Feb 07 |
jari |
54 |
} |
2 |
26 Feb 07 |
jari |
55 |
|
2 |
26 Feb 07 |
jari |
56 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>HttpCommunicator2</code> with specified url. |
2 |
26 Feb 07 |
jari |
58 |
*/ |
2 |
26 Feb 07 |
jari |
59 |
private HttpCommunicator2(String serverURL) throws MalformedURLException { |
2 |
26 Feb 07 |
jari |
60 |
sendURL = new URL(serverURL + "?post-request"); |
2 |
26 Feb 07 |
jari |
61 |
receiveURL = new URL(serverURL + "?get-response"); |
2 |
26 Feb 07 |
jari |
62 |
context = getUniqueContext(); |
2 |
26 Feb 07 |
jari |
63 |
} |
2 |
26 Feb 07 |
jari |
64 |
|
2 |
26 Feb 07 |
jari |
65 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>HttpCommunicator2</code> with specified configuration. |
2 |
26 Feb 07 |
jari |
67 |
*/ |
2 |
26 Feb 07 |
jari |
68 |
public HttpCommunicator2(ConfMap map) throws MalformedURLException, IOException { |
2 |
26 Feb 07 |
jari |
69 |
this(map.getString("remote.server")); |
2 |
26 Feb 07 |
jari |
70 |
fileName = (new TempFile(map)).getName(); |
2 |
26 Feb 07 |
jari |
71 |
keepTempFile = map.getBoolean("remote.debug.keep-request-file", false); |
2 |
26 Feb 07 |
jari |
72 |
} |
2 |
26 Feb 07 |
jari |
73 |
|
2 |
26 Feb 07 |
jari |
74 |
/** |
2 |
26 Feb 07 |
jari |
* Send request with specified properties. |
2 |
26 Feb 07 |
jari |
76 |
*/ |
2 |
26 Feb 07 |
jari |
77 |
public OutputStream send(Properties properties) throws IOException { |
2 |
26 Feb 07 |
jari |
78 |
createSendConnection(); |
2 |
26 Feb 07 |
jari |
79 |
setRequestProperties(properties); |
2 |
26 Feb 07 |
jari |
80 |
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(fileName)); |
2 |
26 Feb 07 |
jari |
81 |
fileCreated = true; |
2 |
26 Feb 07 |
jari |
82 |
return bufferedOutputStream; |
2 |
26 Feb 07 |
jari |
83 |
} |
2 |
26 Feb 07 |
jari |
84 |
|
2 |
26 Feb 07 |
jari |
85 |
/** |
2 |
26 Feb 07 |
jari |
* Returns response as an input stream. |
2 |
26 Feb 07 |
jari |
87 |
*/ |
2 |
26 Feb 07 |
jari |
88 |
public InputStream receive() throws IOException { |
2 |
26 Feb 07 |
jari |
89 |
createReceiveConnection(); |
2 |
26 Feb 07 |
jari |
90 |
inputStream = new BufferedInputStream(connection.getInputStream()); |
2 |
26 Feb 07 |
jari |
91 |
checkResponseCode(); |
2 |
26 Feb 07 |
jari |
92 |
return inputStream; |
2 |
26 Feb 07 |
jari |
93 |
} |
2 |
26 Feb 07 |
jari |
94 |
|
2 |
26 Feb 07 |
jari |
95 |
/** |
2 |
26 Feb 07 |
jari |
* Clean up resources after responce was received. |
2 |
26 Feb 07 |
jari |
97 |
*/ |
2 |
26 Feb 07 |
jari |
98 |
public void cleanupAfterReceive() throws IOException { |
2 |
26 Feb 07 |
jari |
99 |
inputStream.close(); |
2 |
26 Feb 07 |
jari |
100 |
inputStream = null; |
2 |
26 Feb 07 |
jari |
101 |
checkResponseCode(); |
2 |
26 Feb 07 |
jari |
102 |
connection.disconnect(); |
2 |
26 Feb 07 |
jari |
103 |
connection = null; |
2 |
26 Feb 07 |
jari |
104 |
} |
2 |
26 Feb 07 |
jari |
105 |
|
2 |
26 Feb 07 |
jari |
106 |
/** |
2 |
26 Feb 07 |
jari |
* Clean up resources after request was sent. |
2 |
26 Feb 07 |
jari |
108 |
*/ |
2 |
26 Feb 07 |
jari |
109 |
public void cleanupAfterSend() throws IOException { |
2 |
26 Feb 07 |
jari |
110 |
File file = null; |
2 |
26 Feb 07 |
jari |
111 |
InputStream in = null; |
2 |
26 Feb 07 |
jari |
112 |
try { |
2 |
26 Feb 07 |
jari |
113 |
bufferedOutputStream.flush(); |
2 |
26 Feb 07 |
jari |
114 |
bufferedOutputStream.close(); |
2 |
26 Feb 07 |
jari |
115 |
bufferedOutputStream = null; |
2 |
26 Feb 07 |
jari |
116 |
file = new File(fileName); |
2 |
26 Feb 07 |
jari |
117 |
setContentLength((int)file.length()); |
2 |
26 Feb 07 |
jari |
118 |
if (contentLength < 1) |
2 |
26 Feb 07 |
jari |
119 |
throw new IOException("HttpCommunicator: wrong content length"); |
2 |
26 Feb 07 |
jari |
120 |
connection.setRequestProperty("Content-Length",(new Integer(contentLength)).toString()); |
2 |
26 Feb 07 |
jari |
121 |
in = new BufferedInputStream(new FileInputStream(file)); |
2 |
26 Feb 07 |
jari |
122 |
OutputStream out = new BufferedOutputStream(connection.getOutputStream()); |
2 |
26 Feb 07 |
jari |
123 |
byte[] b = new byte[1024*100]; |
2 |
26 Feb 07 |
jari |
124 |
int cnt; |
2 |
26 Feb 07 |
jari |
125 |
while ((cnt = in.read(b)) >= 0) { |
2 |
26 Feb 07 |
jari |
126 |
out.write(b, 0, cnt); |
2 |
26 Feb 07 |
jari |
127 |
} |
2 |
26 Feb 07 |
jari |
128 |
in.close(); |
2 |
26 Feb 07 |
jari |
129 |
in = null; |
2 |
26 Feb 07 |
jari |
130 |
out.flush(); |
2 |
26 Feb 07 |
jari |
131 |
out.close(); |
2 |
26 Feb 07 |
jari |
132 |
out = null; |
2 |
26 Feb 07 |
jari |
133 |
deleteTempFile( file ); |
2 |
26 Feb 07 |
jari |
134 |
fileCreated = false; |
2 |
26 Feb 07 |
jari |
135 |
checkResponseCode(); |
2 |
26 Feb 07 |
jari |
136 |
connection.disconnect(); |
2 |
26 Feb 07 |
jari |
137 |
connection = null; |
2 |
26 Feb 07 |
jari |
138 |
} catch (IOException ex) { |
2 |
26 Feb 07 |
jari |
139 |
if (fileCreated) { |
2 |
26 Feb 07 |
jari |
140 |
if (in != null) { |
2 |
26 Feb 07 |
jari |
141 |
in.close(); |
2 |
26 Feb 07 |
jari |
142 |
in = null; |
2 |
26 Feb 07 |
jari |
143 |
} |
2 |
26 Feb 07 |
jari |
144 |
if (file == null) file = new File(fileName); |
2 |
26 Feb 07 |
jari |
145 |
deleteTempFile( file ); |
2 |
26 Feb 07 |
jari |
146 |
} |
2 |
26 Feb 07 |
jari |
147 |
throw ex; |
2 |
26 Feb 07 |
jari |
148 |
} |
2 |
26 Feb 07 |
jari |
149 |
} |
2 |
26 Feb 07 |
jari |
150 |
|
2 |
26 Feb 07 |
jari |
151 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a connection to send request. |
2 |
26 Feb 07 |
jari |
153 |
*/ |
2 |
26 Feb 07 |
jari |
154 |
private void createSendConnection() throws IOException { |
2 |
26 Feb 07 |
jari |
155 |
try { |
2 |
26 Feb 07 |
jari |
156 |
connection = new HttpURLConnection(sendURL); |
2 |
26 Feb 07 |
jari |
157 |
connection.setContext(context); |
2 |
26 Feb 07 |
jari |
158 |
HTTPConnection.addDefaultModule(Class.forName("HTTPClient.CookieModule"),0); |
2 |
26 Feb 07 |
jari |
159 |
CookieModule.setCookiePolicyHandler(null); |
2 |
26 Feb 07 |
jari |
160 |
connection.setRequestMethod("POST"); |
2 |
26 Feb 07 |
jari |
161 |
connection.setAllowUserInteraction(true); |
2 |
26 Feb 07 |
jari |
162 |
connection.setDoOutput(true); |
2 |
26 Feb 07 |
jari |
163 |
connection.setRequestProperty("Content-Type","text/plain"); |
2 |
26 Feb 07 |
jari |
164 |
connection.setRequestProperty("Connection","close"); |
2 |
26 Feb 07 |
jari |
165 |
} catch (Exception ex) { |
2 |
26 Feb 07 |
jari |
166 |
throw new IOException("HttpCommunicator: cannot create send connection"); |
2 |
26 Feb 07 |
jari |
167 |
} |
2 |
26 Feb 07 |
jari |
168 |
} |
2 |
26 Feb 07 |
jari |
169 |
|
2 |
26 Feb 07 |
jari |
170 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a connection to receive response. |
2 |
26 Feb 07 |
jari |
172 |
*/ |
2 |
26 Feb 07 |
jari |
173 |
private void createReceiveConnection() throws IOException { |
2 |
26 Feb 07 |
jari |
174 |
connection = new HttpURLConnection(receiveURL); |
2 |
26 Feb 07 |
jari |
175 |
connection.setContext(context); |
2 |
26 Feb 07 |
jari |
176 |
connection.setRequestMethod("GET"); |
2 |
26 Feb 07 |
jari |
177 |
connection.setAllowUserInteraction(true); |
2 |
26 Feb 07 |
jari |
178 |
connection.setRequestProperty("Connection","close"); |
2 |
26 Feb 07 |
jari |
179 |
} |
2 |
26 Feb 07 |
jari |
180 |
|
2 |
26 Feb 07 |
jari |
181 |
/** |
2 |
26 Feb 07 |
jari |
* Sends the specified properties. |
2 |
26 Feb 07 |
jari |
183 |
*/ |
2 |
26 Feb 07 |
jari |
184 |
private void setRequestProperties(Properties props) { |
2 |
26 Feb 07 |
jari |
185 |
String name; |
2 |
26 Feb 07 |
jari |
186 |
String value; |
2 |
26 Feb 07 |
jari |
187 |
for (Enumeration _enum = props.propertyNames(); _enum.hasMoreElements();) { |
2 |
26 Feb 07 |
jari |
188 |
name = (String)_enum.nextElement(); |
2 |
26 Feb 07 |
jari |
189 |
value = props.getProperty(name, ""); |
2 |
26 Feb 07 |
jari |
190 |
connection.setRequestProperty(name, value); |
2 |
26 Feb 07 |
jari |
191 |
} |
2 |
26 Feb 07 |
jari |
192 |
} |
2 |
26 Feb 07 |
jari |
193 |
|
2 |
26 Feb 07 |
jari |
194 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the response properties. |
2 |
26 Feb 07 |
jari |
196 |
*/ |
2 |
26 Feb 07 |
jari |
197 |
private Properties getResponseProperties() { |
2 |
26 Feb 07 |
jari |
198 |
Properties props = new Properties(); |
2 |
26 Feb 07 |
jari |
199 |
int i = 1; |
2 |
26 Feb 07 |
jari |
200 |
String name; |
2 |
26 Feb 07 |
jari |
201 |
String value; |
2 |
26 Feb 07 |
jari |
202 |
while (true) { |
2 |
26 Feb 07 |
jari |
203 |
name = connection.getHeaderFieldKey(i); |
2 |
26 Feb 07 |
jari |
204 |
value = connection.getHeaderField(i); |
2 |
26 Feb 07 |
jari |
205 |
if (name == null) { |
2 |
26 Feb 07 |
jari |
206 |
break; |
2 |
26 Feb 07 |
jari |
207 |
} |
2 |
26 Feb 07 |
jari |
208 |
props.setProperty(name, value); |
2 |
26 Feb 07 |
jari |
209 |
i++; |
2 |
26 Feb 07 |
jari |
210 |
} |
2 |
26 Feb 07 |
jari |
211 |
return props; |
2 |
26 Feb 07 |
jari |
212 |
} |
2 |
26 Feb 07 |
jari |
213 |
|
2 |
26 Feb 07 |
jari |
214 |
/** |
2 |
26 Feb 07 |
jari |
* Sets the specified length of http content. |
2 |
26 Feb 07 |
jari |
216 |
*/ |
2 |
26 Feb 07 |
jari |
217 |
private void setContentLength(int length) { |
2 |
26 Feb 07 |
jari |
218 |
contentLength = length; |
2 |
26 Feb 07 |
jari |
219 |
} |
2 |
26 Feb 07 |
jari |
220 |
|
2 |
26 Feb 07 |
jari |
221 |
/** |
2 |
26 Feb 07 |
jari |
* Checkes response code. |
2 |
26 Feb 07 |
jari |
* @throws IOException if the code is not 200. |
2 |
26 Feb 07 |
jari |
224 |
*/ |
2 |
26 Feb 07 |
jari |
225 |
public void checkResponseCode() throws IOException { |
2 |
26 Feb 07 |
jari |
226 |
int code = connection.getResponseCode(); |
2 |
26 Feb 07 |
jari |
227 |
if (code != HttpURLConnection.HTTP_OK) { |
2 |
26 Feb 07 |
jari |
228 |
throw new IOException("Response code is other than 200 OK. Url: \n" + connection.toString() + "\n Code: " + code); |
2 |
26 Feb 07 |
jari |
229 |
} |
2 |
26 Feb 07 |
jari |
230 |
} |
2 |
26 Feb 07 |
jari |
231 |
|
2 |
26 Feb 07 |
jari |
232 |
/** |
2 |
26 Feb 07 |
jari |
* Removes temporary file. |
2 |
26 Feb 07 |
jari |
234 |
*/ |
2 |
26 Feb 07 |
jari |
235 |
private void deleteTempFile( File file ) throws IOException { |
2 |
26 Feb 07 |
jari |
236 |
if (this.keepTempFile == false) { |
2 |
26 Feb 07 |
jari |
237 |
if (!file.delete()) throw new IOException("HttpCommunicator: deleting temp file error"); |
2 |
26 Feb 07 |
jari |
238 |
} |
2 |
26 Feb 07 |
jari |
239 |
} |
2 |
26 Feb 07 |
jari |
240 |
} |