1 17 18 19 20 package org.apache.fop.apps; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.net.URLConnection ; 30 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.stream.StreamSource ; 33 34 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import org.apache.xmlgraphics.util.io.Base64DecodeStream; 40 import org.apache.xmlgraphics.util.io.Base64EncodeStream; 41 42 47 public class FOURIResolver 48 implements javax.xml.transform.URIResolver { 49 50 private Log log = LogFactory.getLog("FOP"); 51 52 73 public Source resolve(String href, String base) 74 throws javax.xml.transform.TransformerException { 75 76 if (href.startsWith("data:")) { 78 return parseDataURI(href); 79 } 80 81 URL absoluteURL = null; 82 File f = new File (href); 83 if (f.exists()) { 84 try { 85 absoluteURL = f.toURL(); 86 } catch (MalformedURLException mfue) { 87 log.error("Could not convert filename to URL: " + mfue.getMessage(), mfue); 88 } 89 } else { 90 URL baseURL = toBaseURL(base); 91 if (baseURL == null) { 92 try { 94 absoluteURL = new URL (href); 95 } catch (MalformedURLException mue) { 96 try { 97 absoluteURL = new URL ("file:" + href); 100 } catch (MalformedURLException mfue) { 101 log.error("Error with URL '" + href + "': " + mue.getMessage(), mue); 102 return null; 103 } 104 } 105 } else { 106 try { 107 126 127 String scheme = baseURL.getProtocol() + ":"; 128 if (href.startsWith(scheme)) { 129 href = href.substring(scheme.length()); 130 if ("file:".equals(scheme)) { 131 int colonPos = href.indexOf(':'); 132 int slashPos = href.indexOf('/'); 133 if (slashPos >= 0 && colonPos >= 0 && colonPos < slashPos) { 134 href = "/" + href; } 136 } 137 } 138 absoluteURL = new URL (baseURL, href); 139 } catch (MalformedURLException mfue) { 140 log.error("Error with URL '" + href + "': " + mfue.getMessage(), mfue); 141 return null; 142 } 143 } 144 } 145 146 String effURL = absoluteURL.toExternalForm(); 147 try { 148 URLConnection connection = absoluteURL.openConnection(); 149 connection.setAllowUserInteraction(false); 150 connection.setDoInput(true); 151 updateURLConnection(connection, href); 152 connection.connect(); 153 return new StreamSource (connection.getInputStream(), effURL); 154 } catch (FileNotFoundException fnfe) { 155 log.debug("File not found: " + effURL); 157 } catch (java.io.IOException ioe) { 158 log.error("Error with opening URL '" + href + "': " + ioe.getMessage(), ioe); 159 } 160 return null; 161 } 162 163 170 protected void updateURLConnection(URLConnection connection, String href) { 171 } 173 174 181 protected void applyHttpBasicAuthentication(URLConnection connection, 182 String username, String password) { 183 String combined = username + ":" + password; 184 try { 185 ByteArrayOutputStream baout = new ByteArrayOutputStream (combined.length() * 2); 186 Base64EncodeStream base64 = new Base64EncodeStream(baout); 187 base64.write(combined.getBytes()); 188 base64.close(); 189 connection.setRequestProperty("Authorization", 190 "Basic " + new String (baout.toByteArray())); 191 } catch (IOException e) { 192 throw new RuntimeException ("Error during base64 encodation of username/password"); 194 } 195 } 196 197 204 private URL toBaseURL(String baseURL) { 205 try { 206 return new URL (baseURL == null 207 ? new java.io.File ("").toURL().toExternalForm() 208 : baseURL); 209 } catch (MalformedURLException mfue) { 210 log.error("Error with base URL \"" + baseURL + "\"): " + mfue.getMessage()); 211 } 212 return null; 213 } 214 215 219 private Source parseDataURI(String href) { 220 int commaPos = href.indexOf(','); 221 String header = href.substring(0, commaPos); 223 String data = href.substring(commaPos + 1); 224 if (header.endsWith(";base64")) { 225 byte[] bytes = data.getBytes(); 226 ByteArrayInputStream encodedStream = new ByteArrayInputStream (bytes); 227 Base64DecodeStream decodedStream = new Base64DecodeStream(encodedStream); 228 return new StreamSource (decodedStream); 229 } else { 230 return new StreamSource (new java.io.StringReader (data)); 233 } 234 } 235 } 236 | Popular Tags |