1 45 package org.exolab.jms.net.http; 46 47 import java.io.BufferedReader ; 48 import java.io.IOException ; 49 import java.io.InputStream ; 50 import java.io.InputStreamReader ; 51 import java.io.OutputStream ; 52 import java.net.HttpURLConnection ; 53 import java.net.URL ; 54 import java.net.ConnectException ; 55 56 import org.exolab.jms.net.multiplexer.Endpoint; 57 import org.exolab.jms.net.uri.URI; 58 import org.exolab.jms.net.util.SSLHelper; 59 60 61 67 class HTTPEndpoint implements Endpoint { 68 69 72 private final URL _url; 73 74 77 private final String _id; 78 79 82 private final URI _uri; 83 84 87 private HTTPInputStream _in; 88 89 92 private HTTPOutputStream _out; 93 94 97 private HTTPRequestInfo _info; 98 99 102 private static final String OPEN_RESPONSE = "OPEN "; 103 104 108 private static final String HTTP_PROXY_HOST = "http.proxyHost"; 109 110 114 private static final String HTTP_PROXY_PORT = "http.proxyPort"; 115 116 120 private static final String HTTPS_PROXY_HOST = "https.proxyHost"; 121 122 126 private static final String HTTPS_PROXY_PORT = "https.proxyPort"; 127 128 129 136 public HTTPEndpoint(HTTPRequestInfo info) throws IOException { 137 final int bufferSize = 2048; 138 139 _uri = info.getURI(); 140 _url = new URL (_uri.toString()); 141 _info = info; 142 143 boolean isSSL = _uri.getScheme().equals("https"); 144 145 if (isSSL && info.getSSLProperties() != null) { 146 SSLHelper.configure(info.getSSLProperties()); 147 } 148 149 if (_info.getProxyHost() != null) { 150 System.setProperty("proxySet", "true"); 152 153 String hostProp = (isSSL) ? HTTPS_PROXY_HOST : HTTP_PROXY_HOST; 154 String portProp = (isSSL) ? HTTPS_PROXY_PORT : HTTP_PROXY_PORT; 155 156 try { 157 System.setProperty(hostProp, _info.getProxyHost()); 158 if (_info.getProxyPort() != 0) { 159 System.setProperty(portProp, "" + _info.getProxyPort()); 160 } 161 } catch (SecurityException exception) { 162 throw new ConnectException ( 163 "Failed to set proxy system properties: " 164 + exception.getMessage()); 165 } 166 } 167 168 HttpURLConnection connection = getConnection("open"); 169 BufferedReader reader = null; 170 try { 171 reader = new BufferedReader (new InputStreamReader ( 172 connection.getInputStream())); 173 String line = reader.readLine(); 174 if (line == null || !line.startsWith(OPEN_RESPONSE)) { 175 throw new IOException ("Invalid response returned from URL=" 176 + _url + ": " + line); 177 178 } 179 _id = line.substring(line.indexOf(OPEN_RESPONSE) 180 + OPEN_RESPONSE.length()); 181 182 } finally { 183 reader.close(); 184 } 185 _in = new HTTPInputStream(_id, _url, info); 186 _out = new HTTPOutputStream(_id, _url, bufferSize, info); 187 } 188 189 194 public URI getURI() { 195 return _uri; 196 } 197 198 205 public InputStream getInputStream() throws IOException { 206 return _in; 207 } 208 209 216 public OutputStream getOutputStream() throws IOException { 217 return _out; 218 } 219 220 225 public void close() throws IOException { 226 try { 227 getConnection("close"); 228 } finally { 229 _in.close(); 230 _out.close(); 231 } 232 } 233 234 241 private HttpURLConnection getConnection(String action) throws IOException { 242 return TunnelHelper.connect(_url, _id, action, _info); 243 } 244 245 } 246 | Popular Tags |