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

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