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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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.fop.util.DataURIResolver;
  34. /**
  35. * Provides FOP specific URI resolution. This is the default URIResolver
  36. * {@link FOUserAgent} will use unless overidden.
  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. /** URIResolver for RFC 2397 data URLs */
  44. private URIResolver dataURIResolver = new DataURIResolver();
  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 errorStr
  96. * error string
  97. * @param strict
  98. * strict user config
  99. */
  100. private void handleException(Exception e, String errorStr, boolean strict)
  101. throws TransformerException {
  102. if (strict) {
  103. throw new TransformerException(errorStr, e);
  104. }
  105. log.error(e.getMessage());
  106. }
  107. /**
  108. * Called by the processor through {@link FOUserAgent} when it encounters an
  109. * uri in an external-graphic element. (see also
  110. * {@link javax.xml.transform.URIResolver#resolve(String, String)} This
  111. * resolver will allow URLs without a scheme, i.e. it assumes 'file:' as the
  112. * default scheme. It also allows relative URLs with scheme, e.g.
  113. * file:../../abc.jpg which is not strictly RFC compliant as long as the
  114. * scheme is the same as the scheme of the base URL. If the base URL is null
  115. * a 'file:' URL referencing the current directory is used as the base URL.
  116. * If the method is successful it will return a Source of type
  117. * {@link javax.xml.transform.stream.StreamSource} with its SystemID set to
  118. * the resolved URL used to open the underlying InputStream.
  119. *
  120. * @param href
  121. * An href attribute, which may be relative or absolute.
  122. * @param base
  123. * The base URI against which the first argument will be made
  124. * absolute if the absolute URI is required.
  125. * @return A {@link javax.xml.transform.Source} object, or null if the href
  126. * cannot be resolved.
  127. * @throws javax.xml.transform.TransformerException
  128. * Never thrown by this implementation.
  129. * @see javax.xml.transform.URIResolver#resolve(String, String)
  130. */
  131. public Source resolve(String href, String base) throws TransformerException {
  132. Source source = null;
  133. // data URLs can be quite long so evaluate early and don't try to build a File
  134. // (can lead to problems)
  135. source = dataURIResolver.resolve(href, base);
  136. // Custom uri resolution
  137. if (source == null && uriResolver != null) {
  138. source = uriResolver.resolve(href, base);
  139. }
  140. // Fallback to default resolution mechanism
  141. if (source == null) {
  142. URL absoluteURL = null;
  143. int hashPos = href.indexOf('#');
  144. String fileURL, fragment;
  145. if (hashPos >= 0) {
  146. fileURL = href.substring(0, hashPos);
  147. fragment = href.substring(hashPos);
  148. } else {
  149. fileURL = href;
  150. fragment = null;
  151. }
  152. File file = new File(fileURL);
  153. if (file.canRead() && file.isFile()) {
  154. try {
  155. if (fragment != null) {
  156. absoluteURL = new URL(file.toURI().toURL().toExternalForm() + fragment);
  157. } else {
  158. absoluteURL = file.toURI().toURL();
  159. }
  160. } catch (MalformedURLException mfue) {
  161. handleException(mfue, "Could not convert filename '" + href
  162. + "' to URL", throwExceptions);
  163. }
  164. } else {
  165. // no base provided
  166. if (base == null) {
  167. // We don't have a valid file protocol based URL
  168. try {
  169. absoluteURL = new URL(href);
  170. } catch (MalformedURLException mue) {
  171. try {
  172. // the above failed, we give it another go in case
  173. // the href contains only a path then file: is
  174. // assumed
  175. absoluteURL = new URL("file:" + href);
  176. } catch (MalformedURLException mfue) {
  177. handleException(mfue, "Error with URL '" + href
  178. + "'", throwExceptions);
  179. }
  180. }
  181. // try and resolve from context of base
  182. } else {
  183. URL baseURL = null;
  184. try {
  185. baseURL = new URL(base);
  186. } catch (MalformedURLException mfue) {
  187. handleException(mfue, "Error with base URL '" + base
  188. + "'", throwExceptions);
  189. }
  190. /*
  191. * This piece of code is based on the following statement in
  192. * RFC2396 section 5.2:
  193. *
  194. * 3) If the scheme component is defined, indicating that
  195. * the reference starts with a scheme name, then the
  196. * reference is interpreted as an absolute URI and we are
  197. * done. Otherwise, the reference URI's scheme is inherited
  198. * from the base URI's scheme component.
  199. *
  200. * Due to a loophole in prior specifications [RFC1630], some
  201. * parsers allow the scheme name to be present in a relative
  202. * URI if it is the same as the base URI scheme.
  203. * Unfortunately, this can conflict with the correct parsing
  204. * of non-hierarchical URI. For backwards compatibility, an
  205. * implementation may work around such references by
  206. * removing the scheme if it matches that of the base URI
  207. * and the scheme is known to always use the <hier_part>
  208. * syntax.
  209. *
  210. * The URL class does not implement this work around, so we
  211. * do.
  212. */
  213. String scheme = baseURL.getProtocol() + ":";
  214. if (href.startsWith(scheme)) {
  215. href = href.substring(scheme.length());
  216. if ("file:".equals(scheme)) {
  217. int colonPos = href.indexOf(':');
  218. int slashPos = href.indexOf('/');
  219. if (slashPos >= 0 && colonPos >= 0
  220. && colonPos < slashPos) {
  221. href = "/" + href; // Absolute file URL doesn't
  222. // have a leading slash
  223. }
  224. }
  225. }
  226. try {
  227. absoluteURL = new URL(baseURL, href);
  228. } catch (MalformedURLException mfue) {
  229. handleException(mfue, "Error with URL; base '" + base
  230. + "' " + "href '" + href + "'", throwExceptions);
  231. }
  232. }
  233. }
  234. if (absoluteURL != null) {
  235. String effURL = absoluteURL.toExternalForm();
  236. try {
  237. URLConnection connection = absoluteURL.openConnection();
  238. connection.setAllowUserInteraction(false);
  239. connection.setDoInput(true);
  240. updateURLConnection(connection, href);
  241. connection.connect();
  242. return new StreamSource(connection.getInputStream(), effURL);
  243. } catch (FileNotFoundException fnfe) {
  244. // Note: This is on "debug" level since the caller is
  245. // supposed to handle this
  246. log.debug("File not found: " + effURL);
  247. } catch (java.io.IOException ioe) {
  248. log.error("Error with opening URL '" + effURL + "': "
  249. + ioe.getMessage());
  250. }
  251. }
  252. }
  253. return source;
  254. }
  255. /**
  256. * This method allows you to set special values on a URLConnection just
  257. * before the connect() method is called. Subclass FOURIResolver and
  258. * override this method to do things like adding the user name and password
  259. * for HTTP basic authentication.
  260. *
  261. * @param connection
  262. * the URLConnection instance
  263. * @param href
  264. * the original URI
  265. */
  266. protected void updateURLConnection(URLConnection connection, String href) {
  267. // nop
  268. }
  269. /**
  270. * This is a convenience method for users who want to override
  271. * updateURLConnection for HTTP basic authentication. Simply call it using
  272. * the right username and password.
  273. *
  274. * @param connection
  275. * the URLConnection to set up for HTTP basic authentication
  276. * @param username
  277. * the username
  278. * @param password
  279. * the password
  280. */
  281. protected void applyHttpBasicAuthentication(URLConnection connection,
  282. String username, String password) {
  283. String combined = username + ":" + password;
  284. try {
  285. ByteArrayOutputStream baout = new ByteArrayOutputStream(combined
  286. .length() * 2);
  287. Base64EncodeStream base64 = new Base64EncodeStream(baout);
  288. // TODO Not sure what charset/encoding can be used with basic
  289. // authentication
  290. base64.write(combined.getBytes("UTF-8"));
  291. base64.close();
  292. connection.setRequestProperty("Authorization", "Basic "
  293. + new String(baout.toByteArray(), "UTF-8"));
  294. } catch (IOException e) {
  295. // won't happen. We're operating in-memory.
  296. throw new RuntimeException(
  297. "Error during base64 encodation of username/password");
  298. }
  299. }
  300. /**
  301. * Sets the custom URI Resolver. It is used for resolving factory-level URIs like
  302. * hyphenation patterns and as backup for URI resolution performed during a
  303. * rendering run.
  304. *
  305. * @param resolver
  306. * the new URI resolver
  307. */
  308. public void setCustomURIResolver(URIResolver resolver) {
  309. this.uriResolver = resolver;
  310. }
  311. /**
  312. * Returns the custom URI Resolver.
  313. *
  314. * @return the URI Resolver or null, if none is set
  315. */
  316. public URIResolver getCustomURIResolver() {
  317. return this.uriResolver;
  318. }
  319. /**
  320. * @param throwExceptions
  321. * Whether or not to throw exceptions on resolution error
  322. */
  323. public void setThrowExceptions(boolean throwExceptions) {
  324. this.throwExceptions = throwExceptions;
  325. }
  326. }