1 57 58 package org.apache.wsif.wsdl; 59 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.InputStreamReader ; 63 import java.io.Reader ; 64 import java.io.UnsupportedEncodingException ; 65 import java.net.URL ; 66 import java.net.URLConnection ; 67 68 import javax.wsdl.WSDLException; 69 70 77 public class AuthenticatingProxyWSDLLocatorImpl implements javax.wsdl.xml.WSDLLocator { 78 79 private static final String PROXY_AUTH="Proxy-Authorization"; 80 private static final char[] BASE64_CHARS = { 81 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 82 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 83 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 84 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 85 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 86 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 87 '8', '9', '+', '/' 88 }; 89 private static final char BASE64_PAD_CHAR = '='; 90 91 Reader baseReader = null; 92 Reader importReader = null; 93 String documentBase = ""; 94 String importBase = ""; 95 String wsdlLocation = ""; 96 String username = null; 97 String password = null; 98 String authString = null; 99 100 106 public AuthenticatingProxyWSDLLocatorImpl(String wsdlLoc, String un, String passwd) 107 throws WSDLException { 108 109 if (wsdlLoc == null 110 || (wsdlLoc.indexOf("http://") == -1 111 && wsdlLoc.indexOf("ftp://") == -1)) { 112 throw new WSDLException( 113 WSDLException.OTHER_ERROR, 114 "Base wsdl location type not supported. The AuthenticatingProxyWSDLLocatorImpl class " 115 + "only supports http and ftp urls for base wsdl locations"); 116 } 117 this.wsdlLocation = wsdlLoc; 118 this.username = un; 119 this.password = passwd; 120 } 121 122 127 public Reader getBaseReader() { 128 if (baseReader == null) { 129 try { 130 URL url = new URL (wsdlLocation); 131 URLConnection con = url.openConnection(); 132 createAuthString(); 133 if (authString != null) { 134 con.setRequestProperty(PROXY_AUTH, authString); 135 } 136 InputStream in = con.getInputStream(); 137 if (in != null) { 138 baseReader = new InputStreamReader (in); 139 } 140 if (url != null) 141 documentBase = url.toString(); 142 } catch (Exception e) { 143 documentBase = wsdlLocation; 144 } 145 } 146 return baseReader; 147 } 148 149 156 public Reader getImportReader(String base, String relativeLocation) { 157 try { 158 URL contextURL = (base != null) ? new URL (base) : null; 159 URL url = new URL (contextURL, relativeLocation); 160 URLConnection con = url.openConnection(); 161 createAuthString(); 162 if (authString != null) { 163 con.setRequestProperty(PROXY_AUTH, authString); 164 } 165 InputStream in = con.getInputStream(); 166 if (in != null) { 167 importReader = new InputStreamReader (in); 168 } 169 importBase = (url == null) ? relativeLocation : url.toString(); 170 } catch (Exception e2) { 171 importBase = "unknownImportURI"; 174 } 175 return importReader; 176 } 177 178 182 public String getBaseURI() { 183 return documentBase; 184 } 185 186 191 public String getLatestImportURI() { 192 return importBase; 193 } 194 195 200 private void createAuthString() { 201 if (authString != null) 203 return; 204 if (username == null || password == null) 206 return; 207 208 byte[] data = null; 209 try { 210 data = (username + ":" + password).getBytes("8859_1"); 211 } catch (UnsupportedEncodingException uee) { 212 return; 213 } 214 int len = data.length; 215 char[] out = new char[len / 3 * 4 + 4]; 216 int readIndex = 0; 217 int writeIndex = 0; 218 int remainingBytes = len; 219 while (remainingBytes >= 3) { 220 int i = 221 ((data[readIndex] & 0xff) << 16) 222 + ((data[readIndex + 1] & 0xff) << 8) 223 + (data[readIndex + 2] & 0xff); 224 out[writeIndex++] = BASE64_CHARS[i >> 18]; 225 out[writeIndex++] = BASE64_CHARS[(i >> 12) & 0x3f]; 226 out[writeIndex++] = BASE64_CHARS[(i >> 6) & 0x3f]; 227 out[writeIndex++] = BASE64_CHARS[i & 0x3f]; 228 readIndex += 3; 229 remainingBytes -= 3; 230 } 231 if (remainingBytes == 1) { 234 int i = data[readIndex] & 0xff; 235 out[writeIndex++] = BASE64_CHARS[i >> 2]; 236 out[writeIndex++] = BASE64_CHARS[(i << 4) & 0x3f]; 237 out[writeIndex++] = BASE64_PAD_CHAR; 238 out[writeIndex++] = BASE64_PAD_CHAR; 239 } else if (remainingBytes == 2) { 240 int i = 241 ((data[readIndex] & 0xff) << 8) + (data[readIndex + 1] & 0xff); 242 out[writeIndex++] = BASE64_CHARS[i >> 10]; 243 out[writeIndex++] = BASE64_CHARS[(i >> 4) & 0x3f]; 244 out[writeIndex++] = BASE64_CHARS[(i << 2) & 0x3f]; 245 out[writeIndex++] = BASE64_PAD_CHAR; 246 } 247 248 String encoded = new String (out, 0, writeIndex); 249 authString = "Basic " + encoded; 250 } 251 252 256 public void close() throws IOException { 257 if (baseReader != null) baseReader.close(); 258 if (importReader != null) importReader.close(); 259 } 260 } | Popular Tags |