You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2012/05/25 16:31:42 UTC
svn commit: r1342640 - in /openejb/trunk/openejb/server/openejb-cxf-rs/src:
main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java
Author: rmannibucau
Date: Fri May 25 14:31:42 2012
New Revision: 1342640
URL: http://svn.apache.org/viewvc?rev=1342640&view=rev
Log:
adding a unit test on exception mapper
Added:
openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java
Modified:
openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
Modified: openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java?rev=1342640&r1=1342639&r2=1342640&view=diff
==============================================================================
--- openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java (original)
+++ openejb/trunk/openejb/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/OpenEJBEJBInvoker.java Fri May 25 14:31:42 2012
@@ -1,27 +1,30 @@
-/*
- * 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.
- */
+/*
+ * 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.server.cxf.rs;
+import java.rmi.RemoteException;
+import javax.xml.ws.WebFault;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.jaxrs.JAXRSInvoker;
import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.FaultMode;
import org.apache.cxf.message.MessageContentsList;
import org.apache.openejb.BeanContext;
import org.apache.openejb.OpenEJBException;
@@ -93,7 +96,15 @@ public class OpenEJBEJBInvoker extends J
method.getDeclaringClass(), method, parameters, null);
return new MessageContentsList(result);
} catch (OpenEJBException e) {
- Response excResponse = JAXRSUtils.convertFaultToResponse(e, exchange.getInMessage());
+ Throwable cause = e.getCause();
+ if (cause == null) {
+ cause = e;
+ }
+ if (cause instanceof RemoteException && cause.getCause() != null) {
+ cause = cause.getCause();
+ }
+
+ final Response excResponse = JAXRSUtils.convertFaultToResponse(cause, exchange.getInMessage());
return new MessageContentsList(excResponse);
} finally {
ThreadLocalContextManager.reset();
Added: openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java?rev=1342640&view=auto
==============================================================================
--- openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java (added)
+++ openejb/trunk/openejb/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/CustomExceptionMapperTest.java Fri May 25 14:31:42 2012
@@ -0,0 +1,85 @@
+/**
+ * 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.server.cxf.rs;
+
+import java.util.Properties;
+import javax.ejb.Singleton;
+import javax.ejb.embeddable.EJBContainer;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.openejb.OpenEjbContainer;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+public class CustomExceptionMapperTest {
+ private static EJBContainer container;
+ private static String providers;
+
+ @BeforeClass public static void start() throws Exception {
+ providers = System.getProperty(CxfRsHttpListener.OPENEJB_CXF_JAXRS_PROVIDERS_KEY);
+ final Properties properties = new Properties();
+ properties.setProperty(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true");
+ properties.setProperty(CxfRsHttpListener.OPENEJB_CXF_JAXRS_PROVIDERS_KEY, EM.class.getName());
+ container = EJBContainer.createEJBContainer(properties);
+ }
+
+ @AfterClass public static void close() throws Exception {
+ if (container != null) {
+ container.close();
+ }
+ if (providers == null) {
+ System.clearProperty(CxfRsHttpListener.OPENEJB_CXF_JAXRS_PROVIDERS_KEY);
+ } else {
+ System.setProperty(CxfRsHttpListener.OPENEJB_CXF_JAXRS_PROVIDERS_KEY, providers);
+ }
+ }
+
+ @Test public void exceptionMapper() {
+ final String response = WebClient.create("http://localhost:4204/openejb-cxf-rs")
+ .path("/exception-mapper/throw").get(String.class);
+ assertEquals(FooException.class.getName(), response);
+ }
+
+ @Singleton
+ @Path("/exception-mapper")
+ public static class RestWithExceptionMapper {
+ @GET
+ @Path("/throw")
+ public String go() {
+ throw new FooException();
+ }
+ }
+
+ public static class EM implements ExceptionMapper<FooException> {
+ @Override
+ public Response toResponse(final FooException t) {
+ return Response.ok(t.getClass().getName()).build();
+ }
+ }
+
+ public static class FooException extends RuntimeException {
+
+ }
+}