mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-21 18:09:20 +00:00
Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2)
127226 gjames: RA-1064: Added a WebScriptExceptionResolver git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127592 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -130,6 +130,8 @@
|
||||
<bean id="jsonHelper" class="org.alfresco.rest.framework.jacksonextensions.JacksonHelper">
|
||||
<property name="module" ref="restJsonModule" />
|
||||
</bean>
|
||||
<bean id="webScriptExceptionResolver" class="org.alfresco.rest.framework.core.exceptions.WebScriptExceptionResolver">
|
||||
</bean>
|
||||
<bean id="simpleMappingExceptionResolver" class="org.alfresco.rest.framework.core.exceptions.SimpleMappingExceptionResolver">
|
||||
<property name="exceptionMappings">
|
||||
<map>
|
||||
@@ -155,6 +157,7 @@
|
||||
<bean id="apiAssistant" class="org.alfresco.rest.framework.tools.ApiAssistant">
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
<property name="webScriptExceptionResolver" ref="webScriptExceptionResolver" />
|
||||
</bean>
|
||||
<!-- Using annotation-config=false means AutowiredAnnotationBeanPostProcessor
|
||||
and CommonAnnotationBeanPostProcessor are both NOT included implicitly -->
|
||||
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2016 Alfresco Software Limited.
|
||||
*
|
||||
* This file is part of Alfresco
|
||||
*
|
||||
* Alfresco is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Alfresco is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.alfresco.rest.framework.core.exceptions;
|
||||
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* Resolves WebScriptExceptions
|
||||
*
|
||||
* @author Gethin James
|
||||
*/
|
||||
public class WebScriptExceptionResolver implements ExceptionResolver<WebScriptException> {
|
||||
|
||||
@Override
|
||||
public ErrorResponse resolveException(WebScriptException ex) {
|
||||
return new ErrorResponse(DefaultExceptionResolver.DEFAULT_MESSAGE_ID, ex.getStatus(), ex.getLocalizedMessage(), ex.getStackTrace(), null);
|
||||
}
|
||||
}
|
@@ -73,6 +73,7 @@ public class ApiAssistant {
|
||||
});
|
||||
|
||||
private ExceptionResolver<Exception> defaultResolver = new DefaultExceptionResolver();
|
||||
private ExceptionResolver<WebScriptException> webScriptExceptionResolver;
|
||||
private ExceptionResolver<Exception> resolver;
|
||||
private JacksonHelper jsonHelper;
|
||||
|
||||
@@ -86,7 +87,15 @@ public class ApiAssistant {
|
||||
|
||||
public ErrorResponse resolveException(Exception ex)
|
||||
{
|
||||
ErrorResponse error = resolver.resolveException(ex);
|
||||
ErrorResponse error = null;
|
||||
if (ex instanceof WebScriptException)
|
||||
{
|
||||
error = webScriptExceptionResolver.resolveException((WebScriptException) ex);
|
||||
}
|
||||
else
|
||||
{
|
||||
error = resolver.resolveException(ex);
|
||||
}
|
||||
if (error == null)
|
||||
{
|
||||
error = defaultResolver.resolveException(ex);
|
||||
@@ -195,6 +204,10 @@ public class ApiAssistant {
|
||||
this.defaultResolver = defaultResolver;
|
||||
}
|
||||
|
||||
public void setWebScriptExceptionResolver(ExceptionResolver<WebScriptException> webScriptExceptionResolver) {
|
||||
this.webScriptExceptionResolver = webScriptExceptionResolver;
|
||||
}
|
||||
|
||||
public void setResolver(ExceptionResolver<Exception> resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
@@ -16,10 +16,10 @@ import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.core.exceptions.NotFoundException;
|
||||
import org.alfresco.rest.framework.core.exceptions.PermissionDeniedException;
|
||||
import org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException;
|
||||
import org.alfresco.rest.framework.core.exceptions.SimpleMappingExceptionResolver;
|
||||
import org.alfresco.rest.framework.core.exceptions.StaleEntityException;
|
||||
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
|
||||
import org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -27,63 +27,73 @@ import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = { "classpath:test-rest-context.xml" })
|
||||
public class ExceptionResolverTests
|
||||
{
|
||||
@Autowired
|
||||
SimpleMappingExceptionResolver simpleMappingExceptionResolver;
|
||||
|
||||
ApiAssistant assistant;
|
||||
|
||||
@Test
|
||||
public void testMatchException()
|
||||
public void testWebscriptException()
|
||||
{
|
||||
ErrorResponse response = simpleMappingExceptionResolver.resolveException(new ApiException(null));
|
||||
ErrorResponse response = assistant.resolveException(new WebScriptException(null));
|
||||
assertNotNull(response);
|
||||
assertEquals(500, response.getStatusCode()); //default to INTERNAL_SERVER_ERROR
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new InvalidArgumentException(null));
|
||||
response = assistant.resolveException(new WebScriptException(HttpServletResponse.SC_UNAUTHORIZED, "Authentication failed for Web Script "));
|
||||
assertNotNull(response);
|
||||
assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatusCode()); //default to INTERNAL_SERVER_ERROR
|
||||
}
|
||||
|
||||
//04180006 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get
|
||||
@Test
|
||||
public void testMatchException()
|
||||
{
|
||||
ErrorResponse response = assistant.resolveException(new ApiException(null));
|
||||
assertNotNull(response);
|
||||
assertEquals(500, response.getStatusCode()); //default to INTERNAL_SERVER_ERROR
|
||||
|
||||
response = assistant.resolveException(new InvalidArgumentException(null));
|
||||
assertEquals(400, response.getStatusCode()); //default to STATUS_BAD_REQUEST
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new InvalidQueryException(null));
|
||||
response = assistant.resolveException(new InvalidQueryException(null));
|
||||
assertEquals(400, response.getStatusCode()); //default to STATUS_BAD_REQUEST
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new NotFoundException(null));
|
||||
response = assistant.resolveException(new NotFoundException(null));
|
||||
assertEquals(404, response.getStatusCode()); //default to STATUS_NOT_FOUND
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new EntityNotFoundException(null));
|
||||
response = assistant.resolveException(new EntityNotFoundException(null));
|
||||
assertEquals(404, response.getStatusCode()); //default to STATUS_NOT_FOUND
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new RelationshipResourceNotFoundException(null, null));
|
||||
response = assistant.resolveException(new RelationshipResourceNotFoundException(null, null));
|
||||
assertEquals(404, response.getStatusCode()); //default to STATUS_NOT_FOUND
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new PermissionDeniedException(null));
|
||||
response = assistant.resolveException(new PermissionDeniedException(null));
|
||||
assertEquals(403, response.getStatusCode()); //default to STATUS_FORBIDDEN
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new UnsupportedResourceOperationException(null));
|
||||
response = assistant.resolveException(new UnsupportedResourceOperationException(null));
|
||||
assertEquals(405, response.getStatusCode()); //default to STATUS_METHOD_NOT_ALLOWED
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new DeletedResourceException(null));
|
||||
response = assistant.resolveException(new DeletedResourceException(null));
|
||||
assertEquals(405, response.getStatusCode()); //default to STATUS_METHOD_NOT_ALLOWED
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new ConstraintViolatedException(null));
|
||||
response = assistant.resolveException(new ConstraintViolatedException(null));
|
||||
assertEquals(409, response.getStatusCode()); //default to STATUS_CONFLICT
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new StaleEntityException(null));
|
||||
response = assistant.resolveException(new StaleEntityException(null));
|
||||
assertEquals(409, response.getStatusCode()); //default to STATUS_CONFLICT
|
||||
|
||||
//Try a random exception
|
||||
response = simpleMappingExceptionResolver.resolveException(new WebScriptException(null));
|
||||
assertNull(response);
|
||||
response = assistant.resolveException(new FormNotFoundException(null));
|
||||
assertEquals(500, response.getStatusCode()); //default to INTERNAL_SERVER_ERROR
|
||||
|
||||
//Try a random exception
|
||||
response = simpleMappingExceptionResolver.resolveException(new FormNotFoundException(null));
|
||||
assertNull(response);
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new InsufficientStorageException(null));
|
||||
response = assistant.resolveException(new InsufficientStorageException(null));
|
||||
assertEquals(507, response.getStatusCode());
|
||||
|
||||
response = simpleMappingExceptionResolver.resolveException(new IntegrityException(null));
|
||||
response = assistant.resolveException(new IntegrityException(null));
|
||||
assertEquals(422, response.getStatusCode());
|
||||
|
||||
}
|
||||
|
@@ -32,9 +32,12 @@
|
||||
<bean id="jsonHelper" class="org.alfresco.rest.framework.jacksonextensions.JacksonHelper">
|
||||
<property name="module" ref="restJsonModule" />
|
||||
</bean>
|
||||
<bean id="webScriptExceptionResolver" class="org.alfresco.rest.framework.core.exceptions.WebScriptExceptionResolver">
|
||||
</bean>
|
||||
<bean id="apiAssistant" class="org.alfresco.rest.framework.tools.ApiAssistant">
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
<property name="webScriptExceptionResolver" ref="webScriptExceptionResolver" />
|
||||
</bean>
|
||||
<bean id="simpleMappingExceptionResolver" class="org.alfresco.rest.framework.core.exceptions.SimpleMappingExceptionResolver">
|
||||
<property name="exceptionMappings">
|
||||
|
Reference in New Issue
Block a user