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.

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