You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FOURIResolver.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2005-2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.apps;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.FileNotFoundException;
  22. import java.io.IOException;
  23. import java.net.MalformedURLException;
  24. import java.net.URL;
  25. import java.net.URLConnection;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.stream.StreamSource;
  28. // commons logging
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. // base64 support for "data" urls
  32. // TODO Move Base64 support from Batik to XML Graphics Commons
  33. import org.apache.batik.util.Base64DecodeStream;
  34. import org.apache.batik.util.Base64EncoderStream;
  35. /**
  36. * Provides FOP specific URI resolution.
  37. * This is the default URIResolver {@link FOUserAgent} will use unless overidden.
  38. * @see javax.xml.transform.URIResolver
  39. */
  40. public class FOURIResolver
  41. implements javax.xml.transform.URIResolver {
  42. private Log log = LogFactory.getLog("FOP");
  43. /**
  44. * Called by the processor through {@link FOUserAgent} when it encounters an
  45. * uri in an external-graphic element.
  46. * (see also {@link javax.xml.transform.URIResolver#resolve(String, String)}
  47. * This resolver will allow URLs without a scheme, i.e. it assumes 'file:' as
  48. * the default scheme. It also allows relative URLs with scheme,
  49. * e.g. file:../../abc.jpg which is not strictly RFC compliant as long as the
  50. * scheme is the same as the scheme of the base URL. If the base URL is null
  51. * a 'file:' URL referencing the current directory is used as the base URL.
  52. * If the method is successful it will return a Source of type
  53. * {@link javax.xml.transform.stream.StreamSource} with its SystemID set to
  54. * the resolved URL used to open the underlying InputStream.
  55. *
  56. * @param href An href attribute, which may be relative or absolute.
  57. * @param base The base URI against which the first argument will be made
  58. * absolute if the absolute URI is required.
  59. * @return A {@link javax.xml.transform.Source} object, or null if the href
  60. * cannot be resolved.
  61. * @throws javax.xml.transform.TransformerException Never thrown by this implementation.
  62. * @see javax.xml.transform.URIResolver#resolve(String, String)
  63. */
  64. public Source resolve(String href, String base)
  65. throws javax.xml.transform.TransformerException {
  66. URL absoluteURL = null;
  67. File f = new File(href);
  68. if (f.exists()) {
  69. try {
  70. absoluteURL = f.toURL();
  71. } catch (MalformedURLException mfue) {
  72. log.error("Could not convert filename to URL: " + mfue.getMessage(), mfue);
  73. }
  74. } else if (href.startsWith("data:")) {
  75. return parseDataURI(href);
  76. } else {
  77. URL baseURL = toBaseURL(base);
  78. if (baseURL == null) {
  79. // We don't have a valid baseURL just use the URL as given
  80. try {
  81. absoluteURL = new URL(href);
  82. } catch (MalformedURLException mue) {
  83. try {
  84. // the above failed, we give it another go in case
  85. // the href contains only a path then file: is assumed
  86. absoluteURL = new URL("file:" + href);
  87. } catch (MalformedURLException mfue) {
  88. log.error("Error with URL '" + href + "': " + mue.getMessage(), mue);
  89. return null;
  90. }
  91. }
  92. } else {
  93. try {
  94. /*
  95. This piece of code is based on the following statement in
  96. RFC2396 section 5.2:
  97. 3) If the scheme component is defined, indicating that the reference
  98. starts with a scheme name, then the reference is interpreted as an
  99. absolute URI and we are done. Otherwise, the reference URI's
  100. scheme is inherited from the base URI's scheme component.
  101. Due to a loophole in prior specifications [RFC1630], some parsers
  102. allow the scheme name to be present in a relative URI if it is the
  103. same as the base URI scheme. Unfortunately, this can conflict
  104. with the correct parsing of non-hierarchical URI. For backwards
  105. compatibility, an implementation may work around such references
  106. by removing the scheme if it matches that of the base URI and the
  107. scheme is known to always use the <hier_part> syntax.
  108. The URL class does not implement this work around, so we do.
  109. */
  110. String scheme = baseURL.getProtocol() + ":";
  111. if (href.startsWith(scheme)) {
  112. href = href.substring(scheme.length());
  113. if ("file:".equals(scheme)) {
  114. int colonPos = href.indexOf(':');
  115. int slashPos = href.indexOf('/');
  116. if (slashPos >= 0 && colonPos >= 0 && colonPos < slashPos) {
  117. href = "/" + href; //Absolute file URL doesn't have a leading slash
  118. }
  119. }
  120. }
  121. absoluteURL = new URL(baseURL, href);
  122. } catch (MalformedURLException mfue) {
  123. log.error("Error with URL '" + href + "': " + mfue.getMessage(), mfue);
  124. return null;
  125. }
  126. }
  127. }
  128. String effURL = absoluteURL.toExternalForm();
  129. try {
  130. URLConnection connection = absoluteURL.openConnection();
  131. connection.setAllowUserInteraction(false);
  132. connection.setDoInput(true);
  133. updateURLConnection(connection, href);
  134. connection.connect();
  135. return new StreamSource(connection.getInputStream(), effURL);
  136. } catch (FileNotFoundException fnfe) {
  137. //Note: This is on "debug" level since the caller is supposed to handle this
  138. log.debug("File not found: " + effURL);
  139. } catch (java.io.IOException ioe) {
  140. log.error("Error with opening URL '" + href + "': " + ioe.getMessage(), ioe);
  141. }
  142. return null;
  143. }
  144. /**
  145. * This method allows you to set special values on a URLConnection just before the connect()
  146. * method is called. Subclass FOURIResolver and override this method to do things like
  147. * adding the user name and password for HTTP basic authentication.
  148. * @param connection the URLConnection instance
  149. * @param href the original URI
  150. */
  151. protected void updateURLConnection(URLConnection connection, String href) {
  152. //nop
  153. }
  154. /**
  155. * This is a convenience method for users who want to override updateURLConnection for
  156. * HTTP basic authentication. Simply call it using the right username and password.
  157. * @param connection the URLConnection to set up for HTTP basic authentication
  158. * @param username the username
  159. * @param password the password
  160. */
  161. protected void applyHttpBasicAuthentication(URLConnection connection,
  162. String username, String password) {
  163. String combined = username + ":" + password;
  164. try {
  165. ByteArrayOutputStream baout = new ByteArrayOutputStream(combined.length() * 2);
  166. Base64EncoderStream base64 = new Base64EncoderStream(baout);
  167. base64.write(combined.getBytes());
  168. base64.close();
  169. connection.setRequestProperty("Authorization",
  170. "Basic " + new String(baout.toByteArray()));
  171. } catch (IOException e) {
  172. //won't happen. We're operating in-memory.
  173. throw new RuntimeException("Error during base64 encodation of username/password");
  174. }
  175. }
  176. /**
  177. * Returns the base URL as a java.net.URL.
  178. * If the base URL is not set a default URL pointing to the
  179. * current directory is returned.
  180. * @param baseURL the base URL
  181. * @returns the base URL as java.net.URL
  182. */
  183. private URL toBaseURL(String baseURL) {
  184. try {
  185. return new URL(baseURL == null
  186. ? new java.io.File("").toURL().toExternalForm()
  187. : baseURL);
  188. } catch (MalformedURLException mfue) {
  189. log.error("Error with base URL \"" + baseURL + "\"): " + mfue.getMessage());
  190. }
  191. return null;
  192. }
  193. /**
  194. * Parses inline data URIs as generated by MS Word's XML export and FO stylesheet.
  195. * @see <a href="http://www.ietf.org/rfc/rfc2397">RFC 2397</a>
  196. */
  197. private Source parseDataURI(String href) {
  198. int commaPos = href.indexOf(',');
  199. // header is of the form data:[<mediatype>][;base64]
  200. String header = href.substring(0, commaPos);
  201. String data = href.substring(commaPos + 1);
  202. if (header.endsWith(";base64")) {
  203. byte[] bytes = data.getBytes();
  204. ByteArrayInputStream encodedStream = new ByteArrayInputStream(bytes);
  205. Base64DecodeStream decodedStream = new Base64DecodeStream(encodedStream);
  206. return new StreamSource(decodedStream);
  207. } else {
  208. //Note that this is not quite the full story here. But since we are only interested
  209. //in base64-encoded binary data, the next line will probably never be called.
  210. return new StreamSource(new java.io.StringReader(data));
  211. }
  212. }
  213. }