Repository: tomee Updated Branches: refs/heads/master 776166911 -> 97744bcfd
test for a custom 404 handler Project: http://git-wip-us.apache.org/repos/asf/tomee/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/97744bcf Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/97744bcf Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/97744bcf Branch: refs/heads/master Commit: 97744bcfd33d11dbf1efd8b93c2fa32779896a6b Parents: 7761669 Author: Romain manni-Bucau <[email protected]> Authored: Mon Jun 6 09:58:32 2016 +0200 Committer: Romain manni-Bucau <[email protected]> Committed: Mon Jun 6 09:58:32 2016 +0200 ---------------------------------------------------------------------- .../arquillian/tests/jaxrs/notfound/App.java | 24 +++++++ .../tests/jaxrs/notfound/CustomHandler.java | 76 ++++++++++++++++++++ .../tests/jaxrs/notfound/Endpoint.java | 28 ++++++++ .../tests/jaxrs/notfound/NotFoundTest.java | 58 +++++++++++++++ 4 files changed, 186 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee/blob/97744bcf/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/App.java ---------------------------------------------------------------------- diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/App.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/App.java new file mode 100644 index 0000000..d48e674 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/App.java @@ -0,0 +1,24 @@ +/* + * 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.notfound; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("api") +public class App extends Application { +} http://git-wip-us.apache.org/repos/asf/tomee/blob/97744bcf/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/CustomHandler.java ---------------------------------------------------------------------- diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/CustomHandler.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/CustomHandler.java new file mode 100644 index 0000000..faa6047 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/CustomHandler.java @@ -0,0 +1,76 @@ +/* + * 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.notfound; + +import org.apache.cxf.interceptor.Fault; +import org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor; +import org.apache.cxf.jaxrs.utils.JAXRSUtils; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageContentsList; +import org.apache.cxf.phase.AbstractPhaseInterceptor; +import org.apache.cxf.phase.Phase; +import org.apache.cxf.transport.http.AbstractHTTPDestination; + +import javax.net.ssl.HttpsURLConnection; +import javax.ws.rs.core.Response; +import java.util.List; + +public class CustomHandler extends AbstractPhaseInterceptor<Message> { + public CustomHandler() { + super(Phase.MARSHAL); + addBefore(JAXRSOutInterceptor.class.getName()); + } + + @Override + public void handleMessage(final Message message) throws Fault { + if (isResponseAlreadyHandled(message)) { + return; + } + final MessageContentsList objs = MessageContentsList.getContentsList(message); + if (objs == null || objs.isEmpty()) { + return; + } + + final Object responseObj = objs.get(0); + if (Response.class.isInstance(responseObj)) { + final Response response = Response.class.cast(responseObj); + if (is404(message, response)) { + switchResponse(message); + } + } else { + final Object exchangeStatus = message.getExchange().get(Message.RESPONSE_CODE); + final int status = exchangeStatus != null ? Integer.class.cast(exchangeStatus) : HttpsURLConnection.HTTP_OK; + if (status == HttpsURLConnection.HTTP_NOT_FOUND) { + switchResponse(message); + } + } + } + + private void switchResponse(final Message message) { + message.setContent(List.class, new MessageContentsList(Response.ok("failed").build())); + } + + private boolean is404(final Message message, final Response response) { + return response.getStatus() == HttpsURLConnection.HTTP_NOT_FOUND + && message.getExchange().get(JAXRSUtils.EXCEPTION_FROM_MAPPER) == null; + } + + private boolean isResponseAlreadyHandled(final Message m) { + return Boolean.TRUE.equals(m.getExchange().get(AbstractHTTPDestination.RESPONSE_COMMITED)) || + Boolean.TRUE.equals(m.getExchange().get(AbstractHTTPDestination.REQUEST_REDIRECTED)); + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/97744bcf/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/Endpoint.java ---------------------------------------------------------------------- diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/Endpoint.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/Endpoint.java new file mode 100644 index 0000000..87f7e03 --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/Endpoint.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.openejb.arquillian.tests.jaxrs.notfound; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; + +@Path("there") +public class Endpoint { + @GET + public String t() { + return "t"; + } +} http://git-wip-us.apache.org/repos/asf/tomee/blob/97744bcf/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/NotFoundTest.java ---------------------------------------------------------------------- diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/NotFoundTest.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/NotFoundTest.java new file mode 100644 index 0000000..122c11f --- /dev/null +++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jaxrs-tests/src/test/java/org/apache/openejb/arquillian/tests/jaxrs/notfound/NotFoundTest.java @@ -0,0 +1,58 @@ +/* + * 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.notfound; + +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 javax.ws.rs.client.ClientBuilder; +import java.net.URL; + +import static org.junit.Assert.assertEquals; + +@RunWith(Arquillian.class) +public class NotFoundTest { + @Deployment(testable = false) + public static Archive<?> app() { + return ShrinkWrap.create(WebArchive.class, "notfound.war") + .addClasses(App.class, Endpoint.class) + .addAsWebInfResource(new StringAsset( + "<openejb-jar>" + + " <pojo-deployment class-name=\"jaxrs-application\">" + + " <properties>" + + " cxf.jaxrs.out-interceptors = " + CustomHandler.class.getName() + "\n" + + " </properties>" + + " </pojo-deployment>" + + "</openejb-jar>"), "openejb-jar.xml"); + } + + @ArquillianResource + private URL base; + + @Test + public void run() { + assertEquals("failed", ClientBuilder.newClient().target(base.toExternalForm() + "api/missing").request().get(String.class)); + assertEquals("t", ClientBuilder.newClient().target(base.toExternalForm() + "api/there").request().get(String.class)); + } +}
