1 7 package org.jboss.remoting.transport.http; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.net.HttpURLConnection ; 13 import java.net.URL ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Set ; 17 import org.jboss.logging.Logger; 18 import org.jboss.remoting.CannotConnectException; 19 import org.jboss.remoting.ConnectionFailedException; 20 import org.jboss.remoting.InvokerLocator; 21 import org.jboss.remoting.RemoteClientInvoker; 22 import org.jboss.remoting.marshal.Marshaller; 23 import org.jboss.remoting.marshal.UnMarshaller; 24 import org.jboss.remoting.marshal.http.HTTPMarshaller; 25 import org.jboss.util.Base64; 26 27 32 public class HTTPClientInvoker extends RemoteClientInvoker 33 { 34 35 protected final Logger log = Logger.getLogger(getClass()); 36 37 public HTTPClientInvoker(InvokerLocator locator) 38 { 39 super(locator); 40 } 41 42 51 protected Object transport(String sessionId, Object invocation, Map metadata, 52 Marshaller marshaller, UnMarshaller unmarshaller) 53 throws IOException , ConnectionFailedException 54 { 55 String targetURL = getLocator().getOriginalURI(); 56 Object httpResponse = useHttpURLConnection(targetURL, invocation, metadata, marshaller, unmarshaller); 57 58 return httpResponse; 59 } 60 61 private Object useHttpURLConnection(String url, Object invocation, Map metadata, 62 Marshaller marshaller, UnMarshaller unmarshaller) 63 { 64 Object result = null; 65 try 66 { 67 HttpURLConnection conn = createURLConnection(url, metadata); 68 69 String basicAuth = getBasicAuth(metadata); 71 if(basicAuth != null) 72 { 73 conn.setRequestProperty("Authorization", basicAuth); 74 } 75 76 boolean isPost = true; 78 if(metadata != null) 79 { 80 String type = (String ) metadata.get("TYPE"); 81 if(type != null && type.equals("GET")) 82 { 83 isPost = false; 84 } 85 86 Map header = (Map ) metadata.get("HEADER"); 88 if(header != null) 89 { 90 Set keys = header.keySet(); 91 Iterator itr = keys.iterator(); 92 while(itr.hasNext()) 93 { 94 String key = (String ) itr.next(); 95 String value = (String ) header.get(key); 96 log.debug("Setting request header with " + key + " : " + value); 97 conn.setRequestProperty(key, value); 98 } 99 } 100 } 101 102 if(isPost) 103 { 104 conn.setDoOutput(true); 106 conn.setDoInput(true); 107 conn.setRequestMethod("POST"); 108 109 OutputStream stream = conn.getOutputStream(); 110 marshaller.write(invocation, stream); 111 InputStream is = conn.getInputStream(); 112 Map headers = conn.getHeaderFields(); 113 result = unmarshaller.read(is, headers); 114 } 115 else 116 { 117 throw new Exception ("HTTP GET opperation not currently supported."); 118 } 119 } 120 catch(Exception e) 121 { 122 log.debug("Error invoking http client invoker.", e); 123 throw new CannotConnectException("Can not connect http client invoker.", e); 124 } 125 126 return result; 127 } 128 129 protected HttpURLConnection createURLConnection(String url, Map metadata) throws IOException 130 { 131 URL externalURL = null; 132 HttpURLConnection httpURLConn = null; 133 134 String proxyHost = null; 136 String proxyportString = null; 137 int proxyPort = -1; 138 boolean proxyOn = true; 139 140 if(metadata != null) 141 { 142 proxyHost = (String ) metadata.get("http.proxyHost"); 144 proxyportString = (String ) metadata.get("http.proxyPort"); 145 if(proxyportString != null && proxyportString.length() > 0) 146 { 147 try 148 { 149 proxyPort = Integer.parseInt(proxyportString); 150 } 151 catch(NumberFormatException e) 152 { 153 log.warn("Error converting proxy port specified (" + proxyportString + ") to a number."); 154 } 155 } 156 } 157 158 if(proxyHost == null) 160 { 161 proxyHost = System.getProperty("http.proxyHost"); 164 if(proxyHost == null) 165 { 166 proxyHost = System.getProperty("proxyHost"); 167 } 168 169 String proxyOnString = System.getProperty("http.proxySet"); 171 if(proxyOnString == null) 172 { 173 proxyOnString = System.getProperty("proxySet"); 174 } 175 if(proxyOnString != null) 176 { 177 proxyOn = Boolean.getBoolean(proxyOnString); 178 } 179 } 180 if(proxyPort < 0) 181 { 182 String proxyPortString = System.getProperty("http.proxyPort"); 183 if(proxyPortString == null) 184 { 185 proxyPortString = System.getProperty("proxyPort"); 186 } 187 if(proxyPortString != null) 188 { 189 try 190 { 191 proxyPort = Integer.parseInt(proxyPortString); 192 } 193 catch(NumberFormatException e) 194 { 195 log.warn("Error converting proxy port specified (" + proxyportString + ") to a number."); 196 } 197 } 198 } 199 200 if(proxyHost != null && proxyOn) 202 { 203 externalURL = new URL ("http", proxyHost, proxyPort, url); 205 206 httpURLConn = (HttpURLConnection ) externalURL.openConnection(); 207 208 String proxyAuth = getProxyAuth(metadata); 210 if(proxyAuth != null) 211 { 212 httpURLConn.setRequestProperty("Proxy-Authorization", proxyAuth); 213 } 214 } 215 else 216 { 217 externalURL = new URL (url); 218 httpURLConn = (HttpURLConnection ) externalURL.openConnection(); 219 } 220 221 return httpURLConn; 222 } 223 224 private String getProxyAuth(Map metadata) 225 { 226 String authString = null; 227 String username = null; 228 String password = null; 229 230 if(metadata != null) 231 { 232 username = (String ) metadata.get("http.proxy.username"); 233 } 234 if(username == null || username.length() == 0) 235 { 236 username = System.getProperty("http.proxy.username"); 237 } 238 if(metadata != null) 239 { 240 password = (String ) metadata.get("http.proxy.password"); 241 } 242 if(password == null || password.length() == 0) 243 { 244 password = System.getProperty("http.proxy.password"); 245 } 246 247 if(username != null && password != null) 248 { 249 StringBuffer buffer = new StringBuffer (); 250 buffer.append(username); 251 buffer.append(":"); 252 buffer.append(password); 253 254 String encoded = Base64.encodeBytes(buffer.toString().getBytes()); 255 256 authString = "Basic " + encoded; 257 258 } 259 260 return authString; 261 } 262 263 private String getBasicAuth(Map metadata) 264 { 265 String authString = null; 266 String username = null; 267 String password = null; 268 269 if(metadata != null) 270 { 271 username = (String ) metadata.get("http.basic.username"); 272 } 273 if(username == null || username.length() == 0) 274 { 275 username = System.getProperty("http.basic.username"); 276 } 277 if(metadata != null) 278 { 279 password = (String ) metadata.get("http.basic.password"); 280 } 281 if(password == null || password.length() == 0) 282 { 283 password = System.getProperty("http.basic.password"); 284 } 285 286 if(username != null && password != null) 287 { 288 StringBuffer buffer = new StringBuffer (); 289 buffer.append(username); 290 buffer.append(":"); 291 buffer.append(password); 292 293 String encoded = Base64.encodeBytes(buffer.toString().getBytes()); 294 295 authString = "Basic " + encoded; 296 297 } 298 299 return authString; 300 } 301 302 303 312 protected void handleConnect() throws ConnectionFailedException 313 { 314 } 316 317 323 protected void handleDisconnect() 324 { 325 } 327 328 335 protected String getDefaultDataType() 336 { 337 return HTTPMarshaller.DATATYPE; 338 } 339 340 } | Popular Tags |