Author: rmannibucau Date: Sun Aug 31 19:33:39 2014 New Revision: 1621619 URL: http://svn.apache.org/r1621619 Log: TOMEE-1339 try static resources first for JAXRS - jsp/servlets will not work and that's expected, fixing it would mean container didn't through any exception while deploying which is a biggest error ;)
Added: tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/ tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/AvoidConflictTest.java tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/TheResource.java Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java Added: tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/AvoidConflictTest.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/AvoidConflictTest.java?rev=1621619&view=auto ============================================================================== --- tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/AvoidConflictTest.java (added) +++ tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/AvoidConflictTest.java Sun Aug 31 19:33:39 2014 @@ -0,0 +1,56 @@ +/* + * 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.openejb.arquillian.tests.jaxrs.staticresources; + +import org.apache.ziplock.IO; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.Archive; +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.runner.RunWith; + +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.assertEquals; + +@RunWith(Arquillian.class) +public class AvoidConflictTest { + @Deployment(testable = false) + public static Archive<?> war() { + return ShrinkWrap.create(WebArchive.class, "app.war") + .addClass(TheResource.class) + .addAsWebResource(new StringAsset("static"), "index.html"); + } + + @ArquillianResource + private URL url; + + @Test + public void jaxrs() throws IOException { + assertEquals("resource", IO.slurp(new URL(url.toExternalForm() + "the"))); + } + + @Test + public void staticResource() throws IOException { + assertEquals("static", IO.slurp(url)); + } +} Added: tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/TheResource.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/TheResource.java?rev=1621619&view=auto ============================================================================== --- tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/TheResource.java (added) +++ tomee/tomee/trunk/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/staticresources/TheResource.java Sun Aug 31 19:33:39 2014 @@ -0,0 +1,29 @@ +/* + * 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.openejb.arquillian.tests.jaxrs.staticresources; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("/") +public class TheResource { + @GET + @Path("the") + public String get() { + return "resource"; + } +} Modified: tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java?rev=1621619&r1=1621618&r2=1621619&view=diff ============================================================================== --- tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java (original) +++ tomee/tomee/trunk/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java Sun Aug 31 19:33:39 2014 @@ -103,6 +103,8 @@ import java.util.concurrent.CopyOnWriteA import java.util.logging.Level; import java.util.regex.Pattern; +import static java.util.Arrays.asList; + public class CxfRsHttpListener implements RsHttpListener { private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB_RS, CxfRsHttpListener.class); @@ -116,6 +118,7 @@ public class CxfRsHttpListener implement public static final String RESOURCE_COMPARATOR_KEY = CXF_JAXRS_PREFIX + "resourceComparator"; private static final String GLOBAL_PROVIDERS = SystemInstance.get().getProperty(PROVIDERS_KEY); + private static final boolean TRY_STATIC_RESOURCES = "true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.jaxrs.static-first", "true")); private static final Map<String, String> STATIC_CONTENT_TYPES; @@ -132,6 +135,7 @@ public class CxfRsHttpListener implement static { STATIC_CONTENT_TYPES = new HashMap<>(); STATIC_CONTENT_TYPES.put("html", "text/html"); + STATIC_CONTENT_TYPES.put("htm", "text/html"); STATIC_CONTENT_TYPES.put("xhtml", "text/html"); STATIC_CONTENT_TYPES.put("txt", "text/plain"); STATIC_CONTENT_TYPES.put("css", "text/css"); @@ -149,11 +153,6 @@ public class CxfRsHttpListener implement @Override public void onMessage(final HttpRequest httpRequest, final HttpResponse httpResponse) throws Exception { - if (matchPath(httpRequest)) { - serveStaticContent(httpRequest, httpResponse, httpRequest.getPathInfo()); - return; - } - // fix the address (to manage multiple connectors) if (HttpRequestImpl.class.isInstance(httpRequest)) { final HttpRequestImpl requestImpl = HttpRequestImpl.class.cast(httpRequest); @@ -162,15 +161,20 @@ public class CxfRsHttpListener implement } String baseURL = BaseUrlHelper.getBaseURL(httpRequest); - if (!baseURL.endsWith("/")) { baseURL += "/"; } - httpRequest.setAttribute("org.apache.cxf.transport.endpoint.address", baseURL); - if (null == destination.getRegistry().checkRestfulRequest(httpRequest.getRequestURL().toString())) { - serveStaticContent(httpRequest, httpResponse, httpRequest.getPathInfo()); - return; + + boolean matchedStatic = false; + if (TRY_STATIC_RESOURCES || (matchedStatic = matchPath(httpRequest))) { + final String pathInfo = httpRequest.getPathInfo(); + if (serveStaticContent(httpRequest, httpResponse, pathInfo)) { + if (matchedStatic) { // we should have gotten the resource + throw new ServletException("Static resource " + pathInfo + " is not available"); + } + return; // ok that's a surely rest service + } } // delegate invocation @@ -202,12 +206,20 @@ public class CxfRsHttpListener implement return false; } - protected void serveStaticContent(final HttpServletRequest request, + protected boolean serveStaticContent(final HttpServletRequest request, final HttpServletResponse response, final String pathInfo) throws ServletException { - final InputStream is = request.getServletContext().getResourceAsStream(pathInfo); + InputStream is = request.getServletContext().getResourceAsStream(pathInfo); + if (is == null && "/".equals(pathInfo)) { + for (final String n : asList("/index.html", "/index.htm")) { + is = request.getServletContext().getResourceAsStream(n); + if (is != null) { + break; + } + } + } if (is == null) { - throw new ServletException("Static resource " + pathInfo + " is not available"); + return false; } try { final int ind = pathInfo.lastIndexOf("."); @@ -221,10 +233,11 @@ public class CxfRsHttpListener implement final ServletOutputStream os = response.getOutputStream(); IOUtils.copy(is, os); os.flush(); + response.setStatus(HttpURLConnection.HTTP_OK); } catch (final IOException ex) { throw new ServletException("Static resource " + pathInfo + " can not be written to the output stream"); } - + return true; } @Override