You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ow...@apache.org on 2014/01/26 22:59:47 UTC

svn commit: r1561554 - in /cxf/fediz/trunk/services/idp/src/main: java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java webapp/WEB-INF/applicationContext.xml

Author: owulff
Date: Sun Jan 26 21:59:47 2014
New Revision: 1561554

URL: http://svn.apache.org/r1561554
Log:
REST ExceptionMapper added

Added:
    cxf/fediz/trunk/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java
Modified:
    cxf/fediz/trunk/services/idp/src/main/webapp/WEB-INF/applicationContext.xml

Added: cxf/fediz/trunk/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java
URL: http://svn.apache.org/viewvc/cxf/fediz/trunk/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java?rev=1561554&view=auto
==============================================================================
--- cxf/fediz/trunk/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java (added)
+++ cxf/fediz/trunk/services/idp/src/main/java/org/apache/cxf/fediz/service/idp/rest/RestServiceExceptionMapper.java Sun Jan 26 21:59:47 2014
@@ -0,0 +1,75 @@
+/**
+ * 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.cxf.fediz.service.idp.rest;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Provider;
+import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.dao.DataRetrievalFailureException;
+import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.security.access.AccessDeniedException;
+
+@Provider
+public class RestServiceExceptionMapper implements ExceptionMapper<Exception>, ResponseExceptionMapper<Exception> {
+
+    private static final String BASIC_REALM_UNAUTHORIZED = "Basic realm=\"Apache Fediz authentication\"";
+
+    private static final Logger LOG = LoggerFactory.getLogger(RestServiceExceptionMapper.class);
+
+    @Override
+    public Response toResponse(final Exception ex) {
+        LOG.error("Exception thrown by REST method: " + ex.getMessage(), ex);
+
+        if (ex instanceof AccessDeniedException) {
+            return Response.status(Response.Status.UNAUTHORIZED).
+                    header(HttpHeaders.WWW_AUTHENTICATE, BASIC_REALM_UNAUTHORIZED).
+                    build();
+        }
+        // EmptyResultDataAccessException
+        // DataIntegrityViolationException
+        // DataRetrievalFailureException / JpaObjectRetrievalFailureException
+        
+        if (ex instanceof DataIntegrityViolationException) {
+            return Response.status(Response.Status.CONFLICT).build();
+        }
+        
+        if (ex instanceof EmptyResultDataAccessException) {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+        
+        if (ex instanceof DataRetrievalFailureException) {
+            return Response.status(Response.Status.NOT_FOUND).build();
+        }
+
+        // Rest is interpreted as InternalServerError
+        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
+    }
+
+    @Override
+    public Exception fromResponse(final Response r) {
+        throw new UnsupportedOperationException(
+                "Call of fromResponse() method is not expected");
+    }
+
+}

Modified: cxf/fediz/trunk/services/idp/src/main/webapp/WEB-INF/applicationContext.xml
URL: http://svn.apache.org/viewvc/cxf/fediz/trunk/services/idp/src/main/webapp/WEB-INF/applicationContext.xml?rev=1561554&r1=1561553&r2=1561554&view=diff
==============================================================================
--- cxf/fediz/trunk/services/idp/src/main/webapp/WEB-INF/applicationContext.xml (original)
+++ cxf/fediz/trunk/services/idp/src/main/webapp/WEB-INF/applicationContext.xml Sun Jan 26 21:59:47 2014
@@ -90,6 +90,8 @@
     </property>
   </bean>
 
+  <bean id="exceptionMapper" class="org.apache.cxf.fediz.service.idp.rest.RestServiceExceptionMapper"/>
+  
   <bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider" />
     
     <jaxrs:server id="idpService" address="/rs">
@@ -108,7 +110,7 @@
         <jaxrs:providers>
       		<ref bean="jaxbProvider"/>
       		<ref bean="jsonProvider"/>
-      		<!--<ref bean="exceptionMapper"/>-->
+      		<ref bean="exceptionMapper"/>
     	</jaxrs:providers>
     	<jaxrs:extensionMappings>
       		<entry key="json" value="application/json;charset=UTF-8"/>