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.

ServletContextURIResolver.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.servlet;
  19. import java.io.InputStream;
  20. import java.net.MalformedURLException;
  21. import java.net.URL;
  22. import javax.servlet.ServletContext;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.URIResolver;
  26. import javax.xml.transform.stream.StreamSource;
  27. /**
  28. * This class is a URIResolver implementation that provides access to resources in the WEB-INF
  29. * directory of a web application using "servlet-content:" URIs.
  30. */
  31. public class ServletContextURIResolver implements URIResolver {
  32. /** The protocol name for the servlet context URIs. */
  33. public static final String SERVLET_CONTEXT_PROTOCOL = "servlet-context:";
  34. private ServletContext servletContext;
  35. /**
  36. * Main constructor
  37. * @param servletContext the servlet context to access the resources through
  38. */
  39. public ServletContextURIResolver(ServletContext servletContext) {
  40. this.servletContext = servletContext;
  41. }
  42. /** {@inheritDoc} */
  43. public Source resolve(String href, String base) throws TransformerException {
  44. if (href.startsWith(SERVLET_CONTEXT_PROTOCOL)) {
  45. return resolveServletContextURI(href.substring(SERVLET_CONTEXT_PROTOCOL.length()));
  46. } else {
  47. if (base != null
  48. && base.startsWith(SERVLET_CONTEXT_PROTOCOL)
  49. && (href.indexOf(':') < 0)) {
  50. String abs = base + href;
  51. return resolveServletContextURI(
  52. abs.substring(SERVLET_CONTEXT_PROTOCOL.length()));
  53. } else {
  54. return null;
  55. }
  56. }
  57. }
  58. /**
  59. * Resolves the "servlet-context:" URI.
  60. * @param path the path part after the protocol (should start with a "/")
  61. * @return the resolved Source or null if the resource was not found
  62. * @throws TransformerException if no URL can be constructed from the path
  63. */
  64. protected Source resolveServletContextURI(String path) throws TransformerException {
  65. while (path.startsWith("//")) {
  66. path = path.substring(1);
  67. }
  68. try {
  69. URL url = this.servletContext.getResource(path);
  70. InputStream in = this.servletContext.getResourceAsStream(path);
  71. if (in != null) {
  72. if (url != null) {
  73. return new StreamSource(in, url.toExternalForm());
  74. } else {
  75. return new StreamSource(in);
  76. }
  77. } else {
  78. throw new TransformerException("Resource does not exist. \"" + path
  79. + "\" is not accessible through the servlet context.");
  80. }
  81. } catch (MalformedURLException mfue) {
  82. throw new TransformerException(
  83. "Error accessing resource using servlet context: " + path, mfue);
  84. }
  85. }
  86. }