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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.apps;
  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.TransformerException;
  28. import javax.xml.transform.URIResolver;
  29. import javax.xml.transform.stream.StreamSource;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.xmlgraphics.util.io.Base64EncodeStream;
  33. import org.apache.xmlgraphics.util.uri.CommonURIResolver;
  34. /**
  35. * Provides FOP specific URI resolution. This is the default URIResolver
  36. * {@link FOUserAgent} will use unless overridden.
  37. *
  38. * @see javax.xml.transform.URIResolver
  39. */
  40. public class FOURIResolver implements javax.xml.transform.URIResolver {
  41. // log
  42. private Log log = LogFactory.getLog("FOP");
  43. /** Common URIResolver */
  44. private CommonURIResolver commonURIResolver = new CommonURIResolver();
  45. /** A user settable URI Resolver */
  46. private URIResolver uriResolver = null;
  47. /** true if exceptions are to be thrown if the URIs cannot be resolved. */
  48. private boolean throwExceptions = false;
  49. /**
  50. * Checks if the given base URL is acceptable. It also normalizes the URL.
  51. * @param base the base URL to check
  52. * @return the normalized URL
  53. * @throws MalformedURLException if there's a problem with a file URL
  54. */
  55. public String checkBaseURL(String base) throws MalformedURLException {
  56. if (!base.endsWith("/")) {
  57. // The behavior described by RFC 3986 regarding resolution of relative
  58. // references may be misleading for normal users:
  59. // file://path/to/resources + myResource.res -> file://path/to/myResource.res
  60. // file://path/to/resources/ + myResource.res -> file://path/to/resources/myResource.res
  61. // We assume that even when the ending slash is missing, users have the second
  62. // example in mind
  63. base += "/";
  64. }
  65. File dir = new File(base);
  66. try {
  67. base = (dir.isDirectory() ? dir.toURI().toURL() : new URL(base)).toExternalForm();
  68. } catch (MalformedURLException mfue) {
  69. if (throwExceptions) {
  70. throw mfue;
  71. }
  72. log.error(mfue.getMessage());
  73. }
  74. return base;
  75. }
  76. /**
  77. * Default constructor
  78. */
  79. public FOURIResolver() {
  80. this(false);
  81. }
  82. /**
  83. * Additional constructor
  84. *
  85. * @param throwExceptions
  86. * true if exceptions are to be thrown if the URIs cannot be
  87. * resolved.
  88. */
  89. public FOURIResolver(boolean throwExceptions) {
  90. this.throwExceptions = throwExceptions;
  91. }
  92. /**
  93. * Handles resolve exceptions appropriately.
  94. *
  95. * @param e
  96. * the exception
  97. * @param errorStr
  98. * error string
  99. * @param strict
  100. * strict user config
  101. */
  102. private void handleException(Exception e, String errorStr, boolean strict)
  103. throws TransformerException {
  104. if (strict) {
  105. throw new TransformerException(errorStr, e);
  106. }
  107. log.error(e.getMessage());
  108. }
  109. /**
  110. * Called by the processor through {@link FOUserAgent} when it encounters an
  111. * uri in an external-graphic element. (see also
  112. * {@link javax.xml.transform.URIResolver#resolve(String, String)} This
  113. * resolver will allow URLs without a scheme, i.e. it assumes 'file:' as the
  114. * default scheme. It also allows relative URLs with scheme, e.g.
  115. * file:../../abc.jpg which is not strictly RFC compliant as long as the
  116. * scheme is the same as the scheme of the base URL. If the base URL is null
  117. * a 'file:' URL referencing the current directory is used as the base URL.
  118. * If the method is successful it will return a Source of type
  119. * {@link javax.xml.transform.stream.StreamSource} with its SystemID set to
  120. * the resolved URL used to open the underlying InputStream.
  121. *
  122. * @param href
  123. * An href attribute, which may be relative or absolute.
  124. * @param base
  125. * The base URI against which the first argument will be made
  126. * absolute if the absolute URI is required.
  127. * @return A {@link javax.xml.transform.Source} object, or null if the href
  128. * cannot be resolved.
  129. * @throws javax.xml.transform.TransformerException
  130. * Never thrown by this implementation.
  131. * @see javax.xml.transform.URIResolver#resolve(String, String)
  132. */
  133. public Source resolve(String href, String base) throws TransformerException {
  134. Source source = null;
  135. // data URLs can be quite long so evaluate early and don't try to build a File
  136. // (can lead to problems)
  137. source = commonURIResolver.resolve(href, base);
  138. // Custom uri resolution
  139. if (source == null && uriResolver != null) {
  140. source = uriResolver.resolve(href, base);
  141. }
  142. // Fallback to default resolution mechanism
  143. if (source == null) {
  144. URL absoluteURL = null;
  145. int hashPos = href.indexOf('#');
  146. String fileURL, fragment;
  147. if (hashPos >= 0) {
  148. fileURL = href.substring(0, hashPos);
  149. fragment = href.substring(hashPos);
  150. } else {
  151. fileURL = href;
  152. fragment = null;
  153. }
  154. File file = new File(fileURL);
  155. if (file.canRead() && file.isFile()) {
  156. try {
  157. if (fragment != null) {
  158. absoluteURL = new URL(file.toURI().toURL().toExternalForm() + fragment);
  159. } else {
  160. absoluteURL = file.toURI().toURL();
  161. }
  162. } catch (MalformedURLException mfue) {
  163. handleException(mfue, "Could not convert filename '" + href
  164. + "' to URL", throwExceptions);
  165. }
  166. } else {
  167. // no base provided
  168. if (base == null) {
  169. // We don't have a valid file protocol based URL
  170. try {
  171. absoluteURL = new URL(href);
  172. } catch (MalformedURLException mue) {
  173. try {
  174. // the above failed, we give it another go in case
  175. // the href contains only a path then file: is
  176. // assumed
  177. absoluteURL = new URL("file:" + href);
  178. } catch (MalformedURLException mfue) {
  179. handleException(mfue, "Error with URL '" + href
  180. + "'", throwExceptions);
  181. }
  182. }
  183. // try and resolve from context of base
  184. } else {
  185. URL baseURL = null;
  186. try {
  187. baseURL = new URL(base);
  188. } catch (MalformedURLException mfue) {
  189. handleException(mfue, "Error with base URL '" + base
  190. + "'", throwExceptions);
  191. }
  192. /*
  193. * This piece of code is based on the following statement in
  194. * RFC2396 section 5.2:
  195. *
  196. * 3) If the scheme component is defined, indicating that
  197. * the reference starts with a scheme name, then the
  198. * reference is interpreted as an absolute URI and we are
  199. * done. Otherwise, the reference URI's scheme is inherited
  200. * from the base URI's scheme component.
  201. *
  202. * Due to a loophole in prior specifications [RFC1630], some
  203. * parsers allow the scheme name to be present in a relative
  204. * URI if it is the same as the base URI scheme.
  205. * Unfortunately, this can conflict with the correct parsing
  206. * of non-hierarchical URI. For backwards compatibility, an
  207. * implementation may work around such references by
  208. * removing the scheme if it matches that of the base URI
  209. * and the scheme is known to always use the <hier_part>
  210. * syntax.
  211. *
  212. * The URL class does not implement this work around, so we
  213. * do.
  214. */
  215. assert (baseURL != null);
  216. String scheme = baseURL.getProtocol() + ":";
  217. if (href.startsWith(scheme) && "file:".equals(scheme)) {
  218. href = href.substring(scheme.length());
  219. int colonPos = href.indexOf(':');
  220. int slashPos = href.indexOf('/');
  221. if (slashPos >= 0 && colonPos >= 0
  222. && colonPos < slashPos) {
  223. href = "/" + href; // Absolute file URL doesn't
  224. // have a leading slash
  225. }
  226. }
  227. try {
  228. absoluteURL = new URL(baseURL, href);
  229. } catch (MalformedURLException mfue) {
  230. handleException(mfue, "Error with URL; base '" + base
  231. + "' " + "href '" + href + "'", throwExceptions);
  232. }
  233. }
  234. }
  235. if (absoluteURL != null) {
  236. String effURL = absoluteURL.toExternalForm();
  237. try {
  238. URLConnection connection = absoluteURL.openConnection();
  239. connection.setAllowUserInteraction(false);
  240. connection.setDoInput(true);
  241. updateURLConnection(connection, href);
  242. connection.connect();
  243. return new StreamSource(connection.getInputStream(), effURL);
  244. } catch (FileNotFoundException fnfe) {
  245. // Note: This is on "debug" level since the caller is
  246. // supposed to handle this
  247. log.debug("File not found: " + effURL);
  248. } catch (java.io.IOException ioe) {
  249. log.error("Error with opening URL '" + effURL + "': "
  250. + ioe.getMessage());
  251. }
  252. }
  253. }
  254. return source;
  255. }
  256. /**
  257. * This method allows you to set special values on a URLConnection just
  258. * before the connect() method is called. Subclass FOURIResolver and
  259. * override this method to do things like adding the user name and password
  260. * for HTTP basic authentication.
  261. *
  262. * @param connection
  263. * the URLConnection instance
  264. * @param href
  265. * the original URI
  266. */
  267. protected void updateURLConnection(URLConnection connection, String href) {
  268. // nop
  269. }
  270. /**
  271. * This is a convenience method for users who want to override
  272. * updateURLConnection for HTTP basic authentication. Simply call it using
  273. * the right username and password.
  274. *
  275. * @param connection
  276. * the URLConnection to set up for HTTP basic authentication
  277. * @param username
  278. * the username
  279. * @param password
  280. * the password
  281. */
  282. protected void applyHttpBasicAuthentication(URLConnection connection,
  283. String username, String password) {
  284. String combined = username + ":" + password;
  285. try {
  286. ByteArrayOutputStream baout = new ByteArrayOutputStream(combined
  287. .length() * 2);
  288. Base64EncodeStream base64 = new Base64EncodeStream(baout);
  289. // TODO Not sure what charset/encoding can be used with basic
  290. // authentication
  291. base64.write(combined.getBytes("UTF-8"));
  292. base64.close();
  293. connection.setRequestProperty("Authorization", "Basic "
  294. + new String(baout.toByteArray(), "UTF-8"));
  295. } catch (IOException e) {
  296. // won't happen. We're operating in-memory.
  297. throw new RuntimeException(
  298. "Error during base64 encodation of username/password");
  299. }
  300. }
  301. /**
  302. * Sets the custom URI Resolver. It is used for resolving factory-level URIs like
  303. * hyphenation patterns and as backup for URI resolution performed during a
  304. * rendering run.
  305. *
  306. * @param resolver
  307. * the new URI resolver
  308. */
  309. public void setCustomURIResolver(URIResolver resolver) {
  310. this.uriResolver = resolver;
  311. }
  312. /**
  313. * Returns the custom URI Resolver.
  314. *
  315. * @return the URI Resolver or null, if none is set
  316. */
  317. public URIResolver getCustomURIResolver() {
  318. return this.uriResolver;
  319. }
  320. /**
  321. * @param throwExceptions
  322. * Whether or not to throw exceptions on resolution error
  323. */
  324. public void setThrowExceptions(boolean throwExceptions) {
  325. this.throwExceptions = throwExceptions;
  326. }
  327. }