Updated Branches: refs/heads/master db3605e8e -> 89309c311
DELTASPIKE-492 Create ExternalResourceProvider for the Servlet Module Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/89309c31 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/89309c31 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/89309c31 Branch: refs/heads/master Commit: 89309c311eae12ee868a9dc20e79d7b5acc9afbb Parents: db3605e Author: Christian Kaltepoth <[email protected]> Authored: Fri Jan 3 07:49:04 2014 +0100 Committer: Christian Kaltepoth <[email protected]> Committed: Fri Jan 3 07:49:04 2014 +0100 ---------------------------------------------------------------------- .../servlet/api/resourceloader/WebStorage.java | 28 +++++ .../resourceloader/WebResourceProvider.java | 63 ++++++++++++ .../resourceloader/WebResourceProviderTest.java | 101 +++++++++++++++++++ 3 files changed, 192 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/89309c31/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebStorage.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebStorage.java b/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebStorage.java new file mode 100644 index 0000000..f9bf971 --- /dev/null +++ b/deltaspike/modules/servlet/api/src/main/java/org/apache/deltaspike/servlet/api/resourceloader/WebStorage.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.servlet.api.resourceloader; + +import org.apache.deltaspike.core.api.resourceloader.ExternalResourceStorage; + +/** + * Loads resources using <code>ServletContext.getResource()</code> + */ +public interface WebStorage extends ExternalResourceStorage +{ +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/89309c31/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/resourceloader/WebResourceProvider.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/resourceloader/WebResourceProvider.java b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/resourceloader/WebResourceProvider.java new file mode 100644 index 0000000..e32640a --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/main/java/org/apache/deltaspike/servlet/impl/resourceloader/WebResourceProvider.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.servlet.impl.resourceloader; + +import java.io.InputStream; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.servlet.ServletContext; + +import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.core.impl.resourceloader.BaseResourceProvider; +import org.apache.deltaspike.core.spi.resourceloader.StorageType; +import org.apache.deltaspike.servlet.api.Web; +import org.apache.deltaspike.servlet.api.resourceloader.WebStorage; + +/** + * Loads resources using {@link ServletContext#getResource(String)}. + */ +@ApplicationScoped +@StorageType(WebStorage.class) +public class WebResourceProvider extends BaseResourceProvider +{ + + @Inject + @Web + private ServletContext servletContext; + + @Override + public InputStream readStream(ExternalResource externalResource) + { + + /* + * ServletContext.getResourceAsStream() requires the path to start with "/". We add it here if it is missing + * because it is a common mistake to miss it. + */ + String path = externalResource.location(); + if (!path.startsWith("/")) + { + path = "/" + path; + } + + return servletContext.getResourceAsStream(path); + + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/89309c31/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java new file mode 100644 index 0000000..f72998e --- /dev/null +++ b/deltaspike/modules/servlet/impl/src/test/java/org/apache/deltaspike/test/servlet/impl/resourceloader/WebResourceProviderTest.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.deltaspike.test.servlet.impl.resourceloader; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.InputStream; +import java.util.Properties; + +import javax.inject.Inject; + +import org.apache.deltaspike.core.api.resourceloader.ExternalResource; +import org.apache.deltaspike.servlet.api.resourceloader.WebStorage; +import org.apache.deltaspike.test.category.WebProfileCategory; +import org.apache.deltaspike.test.servlet.impl.Deployments; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +@RunWith(Arquillian.class) +@Category(WebProfileCategory.class) +public class WebResourceProviderTest +{ + + @Deployment + public static WebArchive getDeployment() + { + return ShrinkWrap.create(WebArchive.class, WebResourceProviderTest.class.getSimpleName() + ".war") + .addAsLibraries(Deployments.getDeltaSpikeCoreArchives()) + .addAsLibraries(Deployments.getDeltaSpikeServletArchives()) + .addAsLibraries(Deployments.getTestSupportArchives()) + .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml") + .addAsWebResource(new StringAsset("foobar"), "foobar.txt") + .addAsWebResource(new StringAsset("foobar"), "foo/bar.txt") + .addAsWebResource(new StringAsset("foo=bar"), "foobar.properties"); + } + + @Inject + @ExternalResource(location = "/foobar.txt", storage = WebStorage.class) + private InputStream streamAbsolutePath; + + @Inject + @ExternalResource(location = "foobar.txt", storage = WebStorage.class) + private InputStream streamRelativePath; + + @Inject + @ExternalResource(location = "/foo/bar.txt", storage = WebStorage.class) + private InputStream streamDirectory; + + @Inject + @ExternalResource(location = "/foobar.properties", storage = WebStorage.class) + private Properties propertiesAbsolutePath; + + @Test + public void testStreamWithAbsolutePath() + { + assertNotNull("Stream not injected", streamAbsolutePath); + } + + @Test + public void testStreamWithRelativePath() + { + assertNotNull("Stream not injected", streamRelativePath); + } + + @Test + public void testStreamWithFileInDirectory() + { + assertNotNull("Stream not injected", streamDirectory); + } + + @Test + public void testPropertiesWithAbsolutePath() + { + assertNotNull("Properties not injected", propertiesAbsolutePath); + assertEquals("bar", propertiesAbsolutePath.get("foo")); + } + +}
