1 2 18 package com.sun.org.apache.xml.internal.security.utils.resolver.implementations; 19 20 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 29 import com.sun.org.apache.xml.internal.utils.URI; 30 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; 31 import com.sun.org.apache.xml.internal.security.utils.Base64; 32 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException; 33 import com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi; 34 import org.w3c.dom.Attr ; 35 36 37 60 public class ResolverDirectHTTP extends ResourceResolverSpi { 61 62 63 static java.util.logging.Logger log = 64 java.util.logging.Logger.getLogger( 65 ResolverDirectHTTP.class.getName()); 66 67 68 static final String properties[] = { "http.proxy.host", "http.proxy.port", 69 "http.proxy.username", 70 "http.proxy.password", 71 "http.basic.username", 72 "http.basic.password" }; 73 74 75 private static final int HttpProxyHost = 0; 76 77 78 private static final int HttpProxyPort = 1; 79 80 81 private static final int HttpProxyUser = 2; 82 83 84 private static final int HttpProxyPass = 3; 85 86 87 private static final int HttpBasicUser = 4; 88 89 90 private static final int HttpBasicPass = 5; 91 92 102 public XMLSignatureInput engineResolve(Attr uri, String BaseURI) 103 throws ResourceResolverException { 104 105 try { 106 boolean useProxy = false; 107 String proxyHost = 108 engineGetProperty(ResolverDirectHTTP 109 .properties[ResolverDirectHTTP.HttpProxyHost]); 110 String proxyPort = 111 engineGetProperty(ResolverDirectHTTP 112 .properties[ResolverDirectHTTP.HttpProxyPort]); 113 114 if ((proxyHost != null) && (proxyPort != null)) { 115 useProxy = true; 116 } 117 118 String oldProxySet = 119 (String ) System.getProperties().get("http.proxySet"); 120 String oldProxyHost = 121 (String ) System.getProperties().get("http.proxyHost"); 122 String oldProxyPort = 123 (String ) System.getProperties().get("http.proxyPort"); 124 boolean switchBackProxy = ((oldProxySet != null) 125 && (oldProxyHost != null) 126 && (oldProxyPort != null)); 127 128 if (useProxy) { 130 if (true) 131 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Use of HTTP proxy enabled: " + proxyHost + ":" 132 + proxyPort); 133 System.getProperties().put("http.proxySet", "true"); 134 System.getProperties().put("http.proxyHost", proxyHost); 135 System.getProperties().put("http.proxyPort", proxyPort); 136 } 137 138 URI uriNew = getNewURI(uri.getNodeValue(), BaseURI); 140 141 URI uriNewNoFrag = new URI(uriNew); 143 144 uriNewNoFrag.setFragment(null); 145 146 URL url = new URL (uriNewNoFrag.toString()); 147 URLConnection urlConnection = url.openConnection(); 148 149 { 150 151 String proxyUser = 153 engineGetProperty(ResolverDirectHTTP 154 .properties[ResolverDirectHTTP.HttpProxyUser]); 155 String proxyPass = 156 engineGetProperty(ResolverDirectHTTP 157 .properties[ResolverDirectHTTP.HttpProxyPass]); 158 159 if ((proxyUser != null) && (proxyPass != null)) { 160 String password = proxyUser + ":" + proxyPass; 161 String encodedPassword = Base64.encode(password.getBytes()); 162 163 urlConnection.setRequestProperty("Proxy-Authorization", 165 encodedPassword); 166 } 167 } 168 169 { 170 171 String auth = urlConnection.getHeaderField("WWW-Authenticate"); 173 174 if (auth != null) { 175 176 if (auth.startsWith("Basic")) { 178 String user = 179 engineGetProperty(ResolverDirectHTTP 180 .properties[ResolverDirectHTTP.HttpBasicUser]); 181 String pass = 182 engineGetProperty(ResolverDirectHTTP 183 .properties[ResolverDirectHTTP.HttpBasicPass]); 184 185 if ((user != null) && (pass != null)) { 186 urlConnection = url.openConnection(); 187 188 String password = user + ":" + pass; 189 String encodedPassword = 190 Base64.encode(password.getBytes()); 191 192 urlConnection.setRequestProperty("Authorization", 194 "Basic " 195 + encodedPassword); 196 } 197 } 198 } 199 } 200 201 String mimeType = urlConnection.getHeaderField("Content-Type"); 202 InputStream inputStream = urlConnection.getInputStream(); 203 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 204 byte buf[] = new byte[4096]; 205 int read = 0; 206 int summarized = 0; 207 208 while ((read = inputStream.read(buf)) >= 0) { 209 baos.write(buf, 0, read); 210 211 summarized += read; 212 } 213 214 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Fetched " + summarized + " bytes from URI " 215 + uriNew.toString()); 216 217 XMLSignatureInput result = new XMLSignatureInput(baos.toByteArray()); 218 219 result.setSourceURI(uriNew.toString()); 221 result.setMIMEType(mimeType); 222 223 if (switchBackProxy) { 225 System.getProperties().put("http.proxySet", oldProxySet); 226 System.getProperties().put("http.proxyHost", oldProxyHost); 227 System.getProperties().put("http.proxyPort", oldProxyPort); 228 } 229 230 return result; 231 } catch (MalformedURLException ex) { 232 throw new ResourceResolverException("generic.EmptyMessage", ex, uri, 233 BaseURI); 234 } catch (IOException ex) { 235 throw new ResourceResolverException("generic.EmptyMessage", ex, uri, 236 BaseURI); 237 } 238 } 239 240 247 public boolean engineCanResolve(Attr uri, String BaseURI) { 248 if (uri == null) { 249 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "quick fail, uri == null"); 250 251 return false; 252 } 253 254 String uriNodeValue = uri.getNodeValue(); 255 256 if (uriNodeValue.equals("") || (uriNodeValue.charAt(0)=='#')) { 257 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "quick fail for empty URIs and local ones"); 258 259 return false; 260 } 261 262 if (true) 263 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I was asked whether I can resolve " + uriNodeValue); 264 265 if ( uriNodeValue.startsWith("http:") || 266 BaseURI.startsWith("http:")) { 267 if (true) 268 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I state that I can resolve " + uriNodeValue); 269 270 return true; 271 } 272 273 if (true) 274 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I state that I can't resolve " + uriNodeValue); 275 276 return false; 277 } 278 279 282 public String [] engineGetPropertyKeys() { 283 return (String []) ResolverDirectHTTP.properties.clone(); 284 } 285 286 private URI getNewURI(String uri, String BaseURI) 287 throws URI.MalformedURIException { 288 289 if ((BaseURI == null) || "".equals(BaseURI)) { 290 return new URI(uri); 291 } 292 return new URI(new URI(BaseURI), uri); 293 } 294 } 295 | Popular Tags |