Author: davsclaus Date: Sat Jan 12 14:37:30 2013 New Revision: 1432437 URL: http://svn.apache.org/viewvc?rev=1432437&view=rev Log: CAMEL-5942: Fixed issue with XSLT was not using custom assigned UriResolver to load the initial xslt resource, but only for other included resources.
Modified: camel/branches/camel-2.10.x/ (props changed) camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java Propchange: camel/branches/camel-2.10.x/ ------------------------------------------------------------------------------ Merged /camel/trunk:r1432435 Propchange: camel/branches/camel-2.10.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java?rev=1432437&r1=1432436&r2=1432437&view=diff ============================================================================== --- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java (original) +++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/builder/xml/XsltUriResolver.java Sat Jan 12 14:37:30 2013 @@ -82,6 +82,13 @@ public class XsltUriResolver implements return new StreamSource(file); } + // if href and location is the same, then its the initial resolve + if (href.equals(location)) { + // default to use classpath: location + String path = "classpath:" + href; + return resolve(path, base); + } + // okay then its relative to the starting location from the XSLT component String path = FileUtil.onlyPath(location); if (ObjectHelper.isEmpty(path)) { Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java?rev=1432437&r1=1432436&r2=1432437&view=diff ============================================================================== --- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java (original) +++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/component/xslt/XsltEndpoint.java Sat Jan 12 14:37:30 2013 @@ -17,9 +17,8 @@ package org.apache.camel.component.xslt; import java.io.IOException; -import java.net.URL; - -import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.TransformerException; import org.apache.camel.Component; import org.apache.camel.Exchange; @@ -28,7 +27,6 @@ import org.apache.camel.api.management.M import org.apache.camel.api.management.ManagedResource; import org.apache.camel.builder.xml.XsltBuilder; import org.apache.camel.impl.ProcessorEndpoint; -import org.apache.camel.util.ResourceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,7 +46,6 @@ public class XsltEndpoint extends Proces this.xslt = xslt; this.resourceUri = resourceUri; this.cacheStylesheet = cacheStylesheet; - loadResource(xslt, resourceUri); } @ManagedOperation(description = "Clears the cached XSLT stylesheet, forcing to re-load the stylesheet on next request") @@ -61,21 +58,12 @@ public class XsltEndpoint extends Proces return cacheStylesheet; } - private synchronized void loadResource(XsltBuilder xslt, String resourceUri) throws TransformerConfigurationException, IOException { - LOG.trace("{} loading schema resource: {}", this, resourceUri); - // prefer to use URL over InputStream as it loads better with http - URL url = ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(), resourceUri); - xslt.setTransformerURL(url); - // now loaded so clear flag - cacheCleared = false; - } - public XsltEndpoint findOrCreateEndpoint(String uri, String newResourceUri) { String newUri = uri.replace(resourceUri, newResourceUri); LOG.trace("Getting endpoint with URI: {}", newUri); return getCamelContext().getEndpoint(newUri, XsltEndpoint.class); } - + @Override protected void onExchange(Exchange exchange) throws Exception { String newResourceUri = exchange.getIn().getHeader(XsltConstants.XSLT_RESOURCE_URI, String.class); @@ -86,12 +74,42 @@ public class XsltEndpoint extends Proces XsltEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri); newEndpoint.onExchange(exchange); return; - } else { + } else { if (!cacheStylesheet || cacheCleared) { - loadResource(xslt, resourceUri); - } + loadResource(resourceUri); + } super.onExchange(exchange); } } + /** + * Loads the resource. + * + * @param resourceUri the resource to load + * + * @throws TransformerException is thrown if error loading resource + * @throws IOException is thrown if error loading resource + */ + protected void loadResource(String resourceUri) throws TransformerException, IOException { + LOG.trace("{} loading schema resource: {}", this, resourceUri); + Source source = xslt.getUriResolver().resolve(resourceUri, null); + if (source == null) { + throw new IOException("Cannot load schema resource " + resourceUri); + } else { + xslt.setTransformerSource(source); + } + // now loaded so clear flag + cacheCleared = false; + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + loadResource(resourceUri); + } + + @Override + protected void doStop() throws Exception { + super.doStop(); + } } Modified: camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java?rev=1432437&r1=1432436&r2=1432437&view=diff ============================================================================== --- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java (original) +++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltCustomizeURIResolverTest.java Sat Jan 12 14:37:30 2013 @@ -17,7 +17,7 @@ package org.apache.camel.component.xslt; import java.io.ByteArrayInputStream; - +import java.io.InputStream; import javax.xml.transform.Source; import javax.xml.transform.TransformerException; import javax.xml.transform.URIResolver; @@ -27,6 +27,7 @@ import org.apache.camel.ContextTestSuppo import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.util.ResourceHelper; /** * @@ -60,7 +61,17 @@ public class XsltCustomizeURIResolverTes @Override public Source resolve(String href, String base) throws TransformerException { - return new StreamSource(new ByteArrayInputStream(EXPECTED_XML_CONSTANT.getBytes())); + if (href.equals("org/apache/camel/component/xslt/include_not_existing_resource.xsl")) { + try { + InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(context.getClassResolver(), href); + return new StreamSource(is); + } catch (Exception e) { + throw new TransformerException(e); + } + } + + Source constantResult = new StreamSource(new ByteArrayInputStream(EXPECTED_XML_CONSTANT.getBytes())); + return constantResult; } }; } Modified: camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java?rev=1432437&r1=1432436&r2=1432437&view=diff ============================================================================== --- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java (original) +++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java Sat Jan 12 14:37:30 2013 @@ -16,7 +16,8 @@ */ package org.apache.camel.component.xslt; -import java.io.FileNotFoundException; +import javax.xml.transform.TransformerException; + import org.apache.camel.CamelContext; import org.apache.camel.FailedToCreateRouteException; import org.apache.camel.ResolveEndpointFailedException; @@ -38,9 +39,9 @@ public class XsltFileNotFoundTest extend fail("Should have thrown an exception due XSLT file not found"); } catch (FailedToCreateRouteException e) { - // expected assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause()); - assertIsInstanceOf(FileNotFoundException.class, e.getCause().getCause()); + assertIsInstanceOf(TransformerException.class, e.getCause().getCause()); + assertEquals("Cannot find org/apache/camel/component/xslt/notfound.xsl in classpath", e.getCause().getCause().getMessage()); } }