mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged HEAD (5.2) to 5.2.N (5.2.1)
127571 jkaabimofrad: Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2) 126242 gjames: RA-878:All api errors should return the standard error object git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@127665 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -150,6 +150,10 @@
|
||||
<entry key="org.alfresco.rest.framework.core.exceptions.UnsupportedMediaTypeException" value="#{T(org.springframework.extensions.webscripts.Status).STATUS_UNSUPPORTED_MEDIA_TYPE}" />
|
||||
</map>
|
||||
</property>
|
||||
</bean>
|
||||
<bean id="apiAssistant" class="org.alfresco.rest.framework.tools.ApiAssistant">
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
</bean>
|
||||
<!-- Using annotation-config=false means AutowiredAnnotationBeanPostProcessor
|
||||
and CommonAnnotationBeanPostProcessor are both NOT included implicitly -->
|
||||
@@ -160,8 +164,7 @@
|
||||
<context:component-scan base-package="org.alfresco.rest.api.modules"/>
|
||||
|
||||
<bean id="apiWebScriptParent" abstract="true" parent="webscript" init-method="init">
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="assistant" ref="apiAssistant" />
|
||||
<property name="encryptTempFiles" value="${webscripts.encryptTempFiles}"/>
|
||||
<property name="tempDirectoryName" value="${webscripts.tempDirectoryName}"/>
|
||||
<property name="memoryThreshold" value="${webscripts.memoryThreshold}"/>
|
||||
|
@@ -76,8 +76,8 @@ public class NetworkWebScriptGet extends ApiWebScript
|
||||
{
|
||||
// apply content type
|
||||
res.setContentType(Format.JSON.mimetype() + ";charset=UTF-8");
|
||||
|
||||
jsonHelper.withWriter(res.getOutputStream(), new Writer()
|
||||
|
||||
assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
@@ -106,17 +106,13 @@ public class NetworkWebScriptGet extends ApiWebScript
|
||||
}
|
||||
}, true, true);
|
||||
}
|
||||
catch (ApiException apiException)
|
||||
catch (ApiException | WebScriptException apiException)
|
||||
{
|
||||
renderErrorResponse(resolveException(apiException), res);
|
||||
}
|
||||
catch (WebScriptException webException)
|
||||
{
|
||||
renderErrorResponse(resolveException(webException), res);
|
||||
assistant.renderException(apiException, res);
|
||||
}
|
||||
catch (RuntimeException runtimeException)
|
||||
{
|
||||
renderErrorResponse(resolveException(runtimeException), res);
|
||||
assistant.renderException(runtimeException, res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -86,8 +86,8 @@ public class NetworksWebScriptGet extends ApiWebScript
|
||||
|
||||
// apply content type
|
||||
res.setContentType(Format.JSON.mimetype() + ";charset=UTF-8");
|
||||
|
||||
jsonHelper.withWriter(res.getOutputStream(), new Writer()
|
||||
|
||||
assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
@@ -114,17 +114,13 @@ public class NetworksWebScriptGet extends ApiWebScript
|
||||
}
|
||||
}, true, true);
|
||||
}
|
||||
catch (ApiException apiException)
|
||||
catch (ApiException | WebScriptException apiException)
|
||||
{
|
||||
renderErrorResponse(resolveException(apiException), res);
|
||||
}
|
||||
catch (WebScriptException webException)
|
||||
{
|
||||
renderErrorResponse(resolveException(webException), res);
|
||||
assistant.renderException(apiException, res);
|
||||
}
|
||||
catch (RuntimeException runtimeException)
|
||||
{
|
||||
renderErrorResponse(resolveException(runtimeException), res);
|
||||
assistant.renderException(runtimeException, res);
|
||||
}
|
||||
}
|
||||
}
|
@@ -41,6 +41,7 @@ import org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAct
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction;
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.extensions.webscripts.ArgumentTypeDescription;
|
||||
import org.springframework.extensions.webscripts.Container;
|
||||
@@ -216,7 +217,7 @@ public class PublicApiDeclarativeRegistry extends DeclarativeRegistry
|
||||
if (templateVars.get("apiName") != null)
|
||||
{
|
||||
// NOTE: noAuth currently only exposed for GET or Create Ticket (login)
|
||||
Api api = determineApi(templateVars);
|
||||
Api api = ApiAssistant.determineApi(templateVars);
|
||||
|
||||
// TODO can we avoid locating resource more than once (or at least provide a common code to determine the GET resourceAction) ?
|
||||
return locator.locateResource(api, templateVars, method);
|
||||
@@ -428,15 +429,6 @@ public class PublicApiDeclarativeRegistry extends DeclarativeRegistry
|
||||
return new Match(match.getTemplate(), match.getTemplateVars(), match.getPath(), noAuthWebScriptWrapper);
|
||||
}
|
||||
|
||||
// note: same as ApiWebscript (apiName must not be null)
|
||||
private Api determineApi(Map<String, String> templateVars)
|
||||
{
|
||||
String apiScope = templateVars.get("apiScope");
|
||||
String apiVersion = templateVars.get("apiVersion");
|
||||
String apiName = templateVars.get("apiName");
|
||||
return Api.valueOf(apiName,apiScope,apiVersion);
|
||||
}
|
||||
|
||||
private void initWebScript(WebScript webScript, String name)
|
||||
{
|
||||
DescriptionImpl serviceDesc = new DescriptionImpl(name, name, name, name);
|
||||
|
@@ -25,27 +25,29 @@
|
||||
*/
|
||||
package org.alfresco.rest.api;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.repo.web.scripts.TenantWebScriptServletRuntime;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.springframework.extensions.config.ServerProperties;
|
||||
import org.springframework.extensions.surf.util.URLDecoder;
|
||||
import org.springframework.extensions.webscripts.Match;
|
||||
import org.springframework.extensions.webscripts.RuntimeContainer;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.*;
|
||||
import org.springframework.extensions.webscripts.servlet.ServletAuthenticatorFactory;
|
||||
|
||||
public class PublicApiTenantWebScriptServletRuntime extends TenantWebScriptServletRuntime
|
||||
{
|
||||
private static final Pattern CMIS_URI_PATTERN = Pattern.compile(".*/cmis/versions/[0-9]+\\.[0-9]+/.*");
|
||||
|
||||
private ApiAssistant apiAssistant;
|
||||
|
||||
public PublicApiTenantWebScriptServletRuntime(RuntimeContainer container, ServletAuthenticatorFactory authFactory, HttpServletRequest req,
|
||||
HttpServletResponse res, ServerProperties serverProperties)
|
||||
HttpServletResponse res, ServerProperties serverProperties, ApiAssistant apiAssistant)
|
||||
{
|
||||
super(container, authFactory, req, res, serverProperties);
|
||||
this.apiAssistant = apiAssistant;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -128,4 +130,24 @@ public class PublicApiTenantWebScriptServletRuntime extends TenantWebScriptServl
|
||||
{
|
||||
return "PublicApiTenantServletRuntime";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void renderErrorResponse(Match match, Throwable exception, WebScriptRequest request, WebScriptResponse response) {
|
||||
|
||||
//If its cmis or not an exception then use the default behaviour
|
||||
if (CMIS_URI_PATTERN.matcher(req.getRequestURI()).matches() || !(exception instanceof Exception))
|
||||
{
|
||||
super.renderErrorResponse(match, exception, request, response);
|
||||
}
|
||||
else
|
||||
{
|
||||
try {
|
||||
apiAssistant.renderException((Exception)exception, response);
|
||||
} catch (IOException e) {
|
||||
logger.error("Internal error", e);
|
||||
throw new WebScriptException("Internal error", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -32,6 +32,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.repo.web.scripts.TenantWebScriptServlet;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.extensions.webscripts.RuntimeContainer;
|
||||
import org.springframework.extensions.webscripts.servlet.WebScriptServletRuntime;
|
||||
@@ -40,7 +41,7 @@ import org.springframework.web.context.support.WebApplicationContextUtils;
|
||||
public class PublicApiWebScriptServlet extends TenantWebScriptServlet
|
||||
{
|
||||
private static final long serialVersionUID = 726730674397482039L;
|
||||
|
||||
private ApiAssistant apiAssistant;
|
||||
@Override
|
||||
public void init() throws ServletException
|
||||
{
|
||||
@@ -48,6 +49,7 @@ public class PublicApiWebScriptServlet extends TenantWebScriptServlet
|
||||
|
||||
ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
|
||||
container = (RuntimeContainer)context.getBean("publicapi.container");
|
||||
apiAssistant = (ApiAssistant) context.getBean("apiAssistant");
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -62,7 +64,7 @@ public class PublicApiWebScriptServlet extends TenantWebScriptServlet
|
||||
|
||||
protected WebScriptServletRuntime getRuntime(HttpServletRequest req, HttpServletResponse res)
|
||||
{
|
||||
WebScriptServletRuntime runtime = new PublicApiTenantWebScriptServletRuntime(container, authenticatorFactory, req, res, serverProperties);
|
||||
WebScriptServletRuntime runtime = new PublicApiTenantWebScriptServletRuntime(container, authenticatorFactory, req, res, serverProperties, apiAssistant);
|
||||
return runtime;
|
||||
}
|
||||
}
|
||||
|
205
source/java/org/alfresco/rest/framework/tools/ApiAssistant.java
Normal file
205
source/java/org/alfresco/rest/framework/tools/ApiAssistant.java
Normal file
@@ -0,0 +1,205 @@
|
||||
/*
|
||||
* 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.tools;
|
||||
|
||||
import org.alfresco.rest.framework.Api;
|
||||
import org.alfresco.rest.framework.core.exceptions.DefaultExceptionResolver;
|
||||
import org.alfresco.rest.framework.core.exceptions.ErrorResponse;
|
||||
import org.alfresco.rest.framework.core.exceptions.ExceptionResolver;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfo;
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfoImpl;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.codehaus.jackson.JsonGenerationException;
|
||||
import org.codehaus.jackson.JsonGenerator;
|
||||
import org.codehaus.jackson.map.JsonMappingException;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.springframework.extensions.surf.util.I18NUtil;
|
||||
import org.springframework.extensions.webscripts.*;
|
||||
import org.springframework.extensions.webscripts.Description.RequiredCache;
|
||||
import org.springframework.extensions.webscripts.servlet.WebScriptServletResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Assists you in creating a great Rest API.
|
||||
*
|
||||
* @author Gethin James
|
||||
*/
|
||||
public class ApiAssistant {
|
||||
|
||||
private static Log logger = LogFactory.getLog(ApiAssistant.class);
|
||||
|
||||
public final static String UTF8 = "UTF-8";
|
||||
public final static ContentInfo DEFAULT_JSON_CONTENT = new ContentInfoImpl(Format.JSON.mimetype(),UTF8, 0, null);
|
||||
public final static Cache CACHE_NEVER = new Cache(new RequiredCache()
|
||||
{
|
||||
@Override
|
||||
public boolean getNeverCache()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getIsPublic()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMustRevalidate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
private ExceptionResolver<Exception> defaultResolver = new DefaultExceptionResolver();
|
||||
private ExceptionResolver<Exception> resolver;
|
||||
private JacksonHelper jsonHelper;
|
||||
|
||||
public static Api determineApi(Map<String, String> templateVars)
|
||||
{
|
||||
String apiScope = templateVars.get("apiScope");
|
||||
String apiVersion = templateVars.get("apiVersion");
|
||||
String apiName = templateVars.get("apiName");
|
||||
return Api.valueOf(apiName,apiScope,apiVersion);
|
||||
}
|
||||
|
||||
public ErrorResponse resolveException(Exception ex)
|
||||
{
|
||||
ErrorResponse error = resolver.resolveException(ex);
|
||||
if (error == null)
|
||||
{
|
||||
error = defaultResolver.resolveException(ex);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the response headers with any information we know about the content
|
||||
* @param res WebScriptResponse
|
||||
* @param contentInfo Content Information
|
||||
*/
|
||||
public void setContentInfoOnResponse(WebScriptResponse res, ContentInfo contentInfo)
|
||||
{
|
||||
if (contentInfo != null)
|
||||
{
|
||||
//Set content info on the response
|
||||
res.setContentType(contentInfo.getMimeType());
|
||||
res.setContentEncoding(contentInfo.getEncoding());
|
||||
|
||||
if (res instanceof WrappingWebScriptResponse)
|
||||
{
|
||||
WrappingWebScriptResponse wrappedRes = ((WrappingWebScriptResponse) res);
|
||||
res = wrappedRes.getNext();
|
||||
}
|
||||
|
||||
if (res instanceof WebScriptServletResponse)
|
||||
{
|
||||
WebScriptServletResponse servletResponse = (WebScriptServletResponse) res;
|
||||
if (contentInfo.getLength() > 0)
|
||||
{
|
||||
if (contentInfo.getLength() > 0 && contentInfo.getLength() < Integer.MAX_VALUE)
|
||||
{
|
||||
servletResponse.getHttpServletResponse().setContentLength((int) contentInfo.getLength());
|
||||
}
|
||||
}
|
||||
if (contentInfo.getLocale() != null)
|
||||
{
|
||||
servletResponse.getHttpServletResponse().setLocale(contentInfo.getLocale());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an exception to the output stream as Json.
|
||||
* @param exception
|
||||
* @param response
|
||||
* @throws IOException
|
||||
*/
|
||||
public void renderException(Exception exception, final WebScriptResponse response) throws IOException {
|
||||
renderErrorResponse(resolveException(exception), response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a JSON error response
|
||||
* @param errorResponse The error
|
||||
* @param res web script response
|
||||
* @throws IOException
|
||||
*/
|
||||
public void renderErrorResponse(ErrorResponse errorResponse, final WebScriptResponse res) throws IOException {
|
||||
|
||||
String logId = "";
|
||||
|
||||
if (Status.STATUS_INTERNAL_SERVER_ERROR == errorResponse.getStatusCode() || logger.isDebugEnabled())
|
||||
{
|
||||
logId = org.alfresco.util.GUID.generate();
|
||||
logger.error(logId+" : "+errorResponse.getStackTrace());
|
||||
}
|
||||
|
||||
String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID);
|
||||
|
||||
final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(),
|
||||
errorResponse.getStatusCode(),
|
||||
errorResponse.getBriefSummary(),
|
||||
stackMessage,
|
||||
logId,
|
||||
errorResponse.getAdditionalState(),
|
||||
DefaultExceptionResolver.ERROR_URL);
|
||||
|
||||
setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT);
|
||||
|
||||
// Status must be set before the response is written by Jackson (which will by default close and commit the response).
|
||||
// In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
|
||||
res.setStatus(errorToWrite.getStatusCode());
|
||||
|
||||
jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer()
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
throws JsonGenerationException, JsonMappingException, IOException
|
||||
{
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("error", errorToWrite);
|
||||
objectMapper.writeValue(generator, obj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public JacksonHelper getJsonHelper() {
|
||||
return jsonHelper;
|
||||
}
|
||||
|
||||
public void setDefaultResolver(ExceptionResolver<Exception> defaultResolver) {
|
||||
this.defaultResolver = defaultResolver;
|
||||
}
|
||||
|
||||
public void setResolver(ExceptionResolver<Exception> resolver) {
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
public void setJsonHelper(JacksonHelper jsonHelper) {
|
||||
this.jsonHelper = jsonHelper;
|
||||
}
|
||||
}
|
@@ -52,6 +52,7 @@ import org.alfresco.rest.framework.resource.content.ContentInfo;
|
||||
import org.alfresco.rest.framework.resource.content.FileBinaryResource;
|
||||
import org.alfresco.rest.framework.resource.content.NodeBinaryResource;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -152,11 +153,11 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
||||
}
|
||||
catch (AlfrescoRuntimeException | ApiException | WebScriptException xception )
|
||||
{
|
||||
renderErrorResponse(resolveException(xception), res);
|
||||
assistant.renderException(xception, res);
|
||||
}
|
||||
catch (RuntimeException runtimeException)
|
||||
{
|
||||
renderErrorResponse(resolveException(runtimeException), res);
|
||||
assistant.renderException(runtimeException, res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +165,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
||||
{
|
||||
final String entityCollectionName = ResourceInspector.findEntityCollectionNameName(resource.getMetaData());
|
||||
final ResourceOperation operation = resource.getMetaData().getOperation(getHttpMethod());
|
||||
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(),DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER);
|
||||
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), ApiAssistant.DEFAULT_JSON_CONTENT,ApiAssistant.CACHE_NEVER);
|
||||
Object toReturn = transactionService.getRetryingTransactionHelper().doInTransaction(
|
||||
new RetryingTransactionHelper.RetryingTransactionCallback<Object>()
|
||||
{
|
||||
@@ -198,7 +199,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
||||
{
|
||||
NodeBinaryResource nodeResource = (NodeBinaryResource) resource;
|
||||
ContentInfo contentInfo = nodeResource.getContentInfo();
|
||||
setContentInfoOnResponse(res, contentInfo);
|
||||
assistant.setContentInfoOnResponse(res, contentInfo);
|
||||
// if requested, set attachment
|
||||
boolean attach = StringUtils.isNotEmpty(nodeResource.getAttachFileName());
|
||||
Map<String, Object> model = getModelForCacheDirective(nodeResource.getCacheDirective());
|
||||
@@ -233,7 +234,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
||||
{
|
||||
res.setStatus(status);
|
||||
if (cache != null) res.setCache(cache);
|
||||
setContentInfoOnResponse(res,contentInfo);
|
||||
assistant.setContentInfoOnResponse(res,contentInfo);
|
||||
if (headers != null && !headers.isEmpty())
|
||||
{
|
||||
for (Map.Entry<String, List<String>> header:headers.entrySet())
|
||||
@@ -269,7 +270,7 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
|
||||
protected void renderJsonResponse(final WebScriptResponse res, final Object toSerialize)
|
||||
throws IOException
|
||||
{
|
||||
jsonHelper.withWriter(res.getOutputStream(), new JacksonHelper.Writer()
|
||||
assistant.getJsonHelper().withWriter(res.getOutputStream(), new JacksonHelper.Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
|
@@ -39,6 +39,7 @@ import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer;
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfo;
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfoImpl;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.GUID;
|
||||
import org.alfresco.util.TempFileProvider;
|
||||
@@ -64,10 +65,7 @@ import org.springframework.extensions.webscripts.servlet.WebScriptServletRespons
|
||||
public abstract class ApiWebScript extends AbstractWebScript
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(ApiWebScript.class);
|
||||
protected JacksonHelper jsonHelper;
|
||||
ExceptionResolver<Exception> defaultResolver = new DefaultExceptionResolver();
|
||||
ExceptionResolver<Exception> resolver;
|
||||
|
||||
protected ApiAssistant assistant;
|
||||
protected boolean encryptTempFiles = false;
|
||||
protected String tempDirectoryName = null;
|
||||
protected int memoryThreshold = 4 * 1024 * 1024; // 4mb
|
||||
@@ -80,9 +78,8 @@ public abstract class ApiWebScript extends AbstractWebScript
|
||||
this.transactionService = transactionService;
|
||||
}
|
||||
|
||||
public void setDefaultResolver(ExceptionResolver<Exception> defaultResolver)
|
||||
{
|
||||
this.defaultResolver = defaultResolver;
|
||||
public void setAssistant(ApiAssistant assistant) {
|
||||
this.assistant = assistant;
|
||||
}
|
||||
|
||||
public void setTempDirectoryName(String tempDirectoryName)
|
||||
@@ -109,41 +106,18 @@ public abstract class ApiWebScript extends AbstractWebScript
|
||||
{
|
||||
this.streamFactory = streamFactory;
|
||||
}
|
||||
|
||||
|
||||
public void init()
|
||||
{
|
||||
File tempDirectory = TempFileProvider.getTempDir(tempDirectoryName);
|
||||
this.streamFactory = ThresholdOutputStreamFactory.newInstance(tempDirectory, memoryThreshold, maxContentSize, encryptTempFiles);
|
||||
}
|
||||
|
||||
public final static String UTF8 = "UTF-8";
|
||||
public final static Cache CACHE_NEVER = new Cache(new RequiredCache()
|
||||
{
|
||||
@Override
|
||||
public boolean getNeverCache()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getIsPublic()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getMustRevalidate()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
});
|
||||
public final static ContentInfo DEFAULT_JSON_CONTENT = new ContentInfoImpl(Format.JSON.mimetype(),UTF8, 0, null);
|
||||
|
||||
@Override
|
||||
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException
|
||||
{
|
||||
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
|
||||
Api api = determineApi(templateVars);
|
||||
Api api = assistant.determineApi(templateVars);
|
||||
|
||||
final BufferedRequest bufferedReq = getRequest(req);
|
||||
final BufferedResponse bufferedRes = getResponse(res);
|
||||
@@ -168,24 +142,6 @@ public abstract class ApiWebScript extends AbstractWebScript
|
||||
}
|
||||
}
|
||||
|
||||
public Api determineApi(Map<String, String> templateVars)
|
||||
{
|
||||
String apiScope = templateVars.get("apiScope");
|
||||
String apiVersion = templateVars.get("apiVersion");
|
||||
String apiName = templateVars.get("apiName");
|
||||
return Api.valueOf(apiName,apiScope,apiVersion);
|
||||
}
|
||||
|
||||
protected ErrorResponse resolveException(Exception ex)
|
||||
{
|
||||
ErrorResponse error = resolver.resolveException(ex);
|
||||
if (error == null)
|
||||
{
|
||||
error = defaultResolver.resolveException(ex);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
protected BufferedRequest getRequest(final WebScriptRequest req)
|
||||
{
|
||||
// create buffered request and response that allow transaction retrying
|
||||
@@ -202,97 +158,4 @@ public abstract class ApiWebScript extends AbstractWebScript
|
||||
|
||||
public abstract void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException;
|
||||
|
||||
/**
|
||||
* Renders a JSON error response
|
||||
* @param errorResponse The error
|
||||
* @param res web script response
|
||||
* @throws IOException
|
||||
*/
|
||||
public void renderErrorResponse(ErrorResponse errorResponse, final WebScriptResponse res) throws IOException {
|
||||
|
||||
String logId = "";
|
||||
|
||||
if (Status.STATUS_INTERNAL_SERVER_ERROR == errorResponse.getStatusCode() || logger.isDebugEnabled())
|
||||
{
|
||||
logId = GUID.generate();
|
||||
logger.error(logId+" : "+errorResponse.getStackTrace());
|
||||
}
|
||||
|
||||
String stackMessage = I18NUtil.getMessage(DefaultExceptionResolver.STACK_MESSAGE_ID);
|
||||
|
||||
final ErrorResponse errorToWrite = new ErrorResponse(errorResponse.getErrorKey(),
|
||||
errorResponse.getStatusCode(),
|
||||
errorResponse.getBriefSummary(),
|
||||
stackMessage,
|
||||
logId,
|
||||
errorResponse.getAdditionalState(),
|
||||
DefaultExceptionResolver.ERROR_URL);
|
||||
|
||||
setContentInfoOnResponse(res, DEFAULT_JSON_CONTENT);
|
||||
|
||||
// Status must be set before the response is written by Jackson (which will by default close and commit the response).
|
||||
// In a r/w txn, web script buffered responses ensure that it doesn't really matter but for r/o txns this is important.
|
||||
res.setStatus(errorToWrite.getStatusCode());
|
||||
|
||||
jsonHelper.withWriter(res.getOutputStream(), new Writer()
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
throws JsonGenerationException, JsonMappingException, IOException
|
||||
{
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("error", errorToWrite);
|
||||
objectMapper.writeValue(generator, obj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the response headers with any information we know about the content
|
||||
* @param res WebScriptResponse
|
||||
* @param contentInfo Content Information
|
||||
*/
|
||||
protected void setContentInfoOnResponse(WebScriptResponse res, ContentInfo contentInfo)
|
||||
{
|
||||
if (contentInfo != null)
|
||||
{
|
||||
//Set content info on the response
|
||||
res.setContentType(contentInfo.getMimeType());
|
||||
res.setContentEncoding(contentInfo.getEncoding());
|
||||
|
||||
if (res instanceof WrappingWebScriptResponse)
|
||||
{
|
||||
WrappingWebScriptResponse wrappedRes = ((WrappingWebScriptResponse) res);
|
||||
res = wrappedRes.getNext();
|
||||
}
|
||||
|
||||
if (res instanceof WebScriptServletResponse)
|
||||
{
|
||||
WebScriptServletResponse servletResponse = (WebScriptServletResponse) res;
|
||||
if (contentInfo.getLength() > 0)
|
||||
{
|
||||
if (contentInfo.getLength() > 0 && contentInfo.getLength() < Integer.MAX_VALUE)
|
||||
{
|
||||
servletResponse.getHttpServletResponse().setContentLength((int) contentInfo.getLength());
|
||||
}
|
||||
}
|
||||
if (contentInfo.getLocale() != null)
|
||||
{
|
||||
servletResponse.getHttpServletResponse().setLocale(contentInfo.getLocale());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setResolver(ExceptionResolver<Exception> resolver)
|
||||
{
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
public void setJsonHelper(JacksonHelper jsonHelper)
|
||||
{
|
||||
this.jsonHelper = jsonHelper;
|
||||
}
|
||||
}
|
||||
|
@@ -37,6 +37,7 @@ import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAct
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
|
||||
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceBinaryAction;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WebScriptResponse;
|
||||
@@ -214,7 +215,7 @@ public class ResourceWebScriptDelete extends AbstractResourceWebScript implement
|
||||
public Void execute(final ResourceWithMetadata resource, final Params params, final WebScriptResponse res, boolean isReadOnly)
|
||||
{
|
||||
final ResourceOperation operation = resource.getMetaData().getOperation(HttpMethod.DELETE);
|
||||
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(),DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER);
|
||||
final WithResponse callBack = new WithResponse(operation.getSuccessStatus(), ApiAssistant.DEFAULT_JSON_CONTENT,ApiAssistant.CACHE_NEVER);
|
||||
transactionService.getRetryingTransactionHelper().doInTransaction(
|
||||
new RetryingTransactionCallback<Void>()
|
||||
{
|
||||
|
@@ -66,6 +66,7 @@ import org.alfresco.rest.framework.resource.parameters.where.InvalidQueryExcepti
|
||||
import org.alfresco.rest.framework.resource.parameters.where.Query;
|
||||
import org.alfresco.rest.framework.resource.parameters.where.QueryImpl;
|
||||
import org.alfresco.rest.framework.resource.parameters.where.WhereCompiler;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.util.Pair;
|
||||
import org.alfresco.util.PropertyCheck;
|
||||
import org.antlr.runtime.RecognitionException;
|
||||
@@ -676,7 +677,7 @@ public class ResourceWebScriptHelper
|
||||
paramFilter = filters.get(resourceKey);
|
||||
}
|
||||
final Params executionParams = Params.valueOf(paramFilter, uniqueEntityId, params.getRequest());
|
||||
final WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiWebScript.DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER);
|
||||
final WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT,ApiAssistant.CACHE_NEVER);
|
||||
//Read only because this only occurs for GET requests
|
||||
Object result = executor.executeAction(resource, executionParams, callBack);
|
||||
return processAdditionsToTheResponse(null, api, null, executionParams, result);
|
||||
|
@@ -108,7 +108,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
|
||||
if (objectType!= null)
|
||||
{
|
||||
//Operations don't support a List as json body
|
||||
postedObj = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, objectType);
|
||||
postedObj = ResourceWebScriptHelper.extractJsonContent(req, assistant.getJsonHelper(), objectType);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(propertyName))
|
||||
@@ -162,7 +162,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
|
||||
// Only allow 1 value.
|
||||
try
|
||||
{
|
||||
Object content = ResourceWebScriptHelper.extractJsonContent(req,jsonHelper, objType);
|
||||
Object content = ResourceWebScriptHelper.extractJsonContent(req,assistant.getJsonHelper(), objType);
|
||||
return Arrays.asList(content);
|
||||
}
|
||||
catch (InvalidArgumentException iae)
|
||||
@@ -179,7 +179,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
|
||||
}
|
||||
}
|
||||
}
|
||||
return ResourceWebScriptHelper.extractJsonContentAsList(req, jsonHelper, objType);
|
||||
return ResourceWebScriptHelper.extractJsonContentAsList(req, assistant.getJsonHelper(), objType);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -87,7 +87,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
|
||||
} else
|
||||
{
|
||||
|
||||
Object putEnt = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(operation));
|
||||
Object putEnt = ResourceWebScriptHelper.extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
|
||||
return Params.valueOf(entityId,params,putEnt, req);
|
||||
}
|
||||
case RELATIONSHIP:
|
||||
@@ -96,7 +96,7 @@ public class ResourceWebScriptPut extends AbstractResourceWebScript implements P
|
||||
throw new UnsupportedResourceOperationException("PUT is executed against the instance URL");
|
||||
} else
|
||||
{
|
||||
Object putRel = ResourceWebScriptHelper.extractJsonContent(req, jsonHelper, resourceMeta.getObjectType(operation));
|
||||
Object putRel = ResourceWebScriptHelper.extractJsonContent(req, assistant.getJsonHelper(), resourceMeta.getObjectType(operation));
|
||||
ResourceWebScriptHelper.setUniqueId(putRel,relationshipId);
|
||||
return Params.valueOf(entityId, params, putRel, req);
|
||||
}
|
||||
|
@@ -77,8 +77,8 @@ public class InfoWebScriptGet extends ApiWebScript
|
||||
{
|
||||
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
|
||||
}
|
||||
|
||||
jsonHelper.withWriter(res.getOutputStream(), new Writer()
|
||||
|
||||
assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
|
@@ -155,8 +155,8 @@ public class WebScriptOptionsMetaData extends ApiWebScript implements ResourceMe
|
||||
{
|
||||
|
||||
final Object result = processResult(resource,allApiResources);
|
||||
|
||||
jsonHelper.withWriter(out, new Writer()
|
||||
|
||||
assistant.getJsonHelper().withWriter(out, new Writer()
|
||||
{
|
||||
@Override
|
||||
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper)
|
||||
|
@@ -47,7 +47,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
|
||||
/**
|
||||
* Created by gethin on 31/03/16.
|
||||
* @author Gethin James
|
||||
*/
|
||||
public class AbstractSingleNetworkSiteTest extends AbstractBaseApiTest
|
||||
{
|
||||
|
@@ -36,11 +36,14 @@ import static org.junit.Assert.assertTrue;
|
||||
import org.alfresco.rest.api.model.ModulePackage;
|
||||
import org.alfresco.rest.api.tests.client.HttpResponse;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiClient;
|
||||
import org.alfresco.rest.api.tests.client.RequestContext;
|
||||
import org.alfresco.rest.api.tests.util.RestApiUtil;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Basic modulepackages api calls
|
||||
@@ -89,6 +92,26 @@ public class ModulePackagesApiTest extends AbstractBaseApiTest
|
||||
assertTrue("Simple module must be the correct version","1.0.0-SNAPSHOT".equals(simpleModule.getVersion().toString()));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testErrorUrls() throws Exception
|
||||
{
|
||||
publicApiClient.setRequestContext(new RequestContext(null));
|
||||
Map<String, String> params = createParams(null, null);
|
||||
|
||||
//Call an endpoint that doesn't exist
|
||||
HttpResponse response = publicApiClient.get(getScope(), MODULEPACKAGES+"/fred/blogs/king/kong/got/if/wrong", null, null, null, params);
|
||||
assertNotNull(response);
|
||||
assertEquals(HttpStatus.SC_NOT_FOUND, response.getStatusCode());
|
||||
assertEquals("no-cache", response.getHeaders().get("Cache-Control"));
|
||||
assertEquals("application/json;charset=UTF-8", response.getHeaders().get("Content-Type"));
|
||||
|
||||
PublicApiClient.ExpectedErrorResponse errorResponse = RestApiUtil.parseErrorResponse(response.getJsonResponse());
|
||||
assertNotNull(errorResponse);
|
||||
assertNotNull(errorResponse.getErrorKey());
|
||||
assertNotNull(errorResponse.getBriefSummary());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getScope()
|
||||
{
|
||||
|
@@ -38,6 +38,7 @@ import org.alfresco.rest.framework.jacksonextensions.JacksonHelper;
|
||||
import org.alfresco.rest.framework.resource.EntityResource;
|
||||
import org.alfresco.rest.framework.resource.RelationshipResource;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ApiWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ResourceWebScriptHelper;
|
||||
@@ -75,9 +76,12 @@ public abstract class AbstractContextTest
|
||||
@Autowired
|
||||
JacksonHelper jsonHelper;
|
||||
|
||||
@Autowired
|
||||
ApiAssistant apiAssistant;
|
||||
|
||||
static Params NOT_USED = Params.valueOf("notUsed", null, mock(WebScriptRequest.class));
|
||||
static final Params.RecognizedParams NULL_PARAMS = new Params.RecognizedParams(null, null, null, null, null, null, null, null, false);
|
||||
static final WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiWebScript.DEFAULT_JSON_CONTENT,ApiWebScript.CACHE_NEVER);
|
||||
static final WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT,ApiAssistant.CACHE_NEVER);
|
||||
static Api api = Api.valueOf("alfrescomock", "private", "1");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -110,4 +114,18 @@ public abstract class AbstractContextTest
|
||||
putExecutor.setTransactionService(transerv);
|
||||
deleteExecutor.setTransactionService(transerv);
|
||||
}
|
||||
|
||||
protected AbstractResourceWebScript getExecutor()
|
||||
{
|
||||
return getExecutor("executorOfGets");
|
||||
}
|
||||
|
||||
protected AbstractResourceWebScript getExecutor(String beanName)
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean(beanName);
|
||||
executor.setLocator(locator);
|
||||
executor.setAssistant(apiAssistant);
|
||||
return executor;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -50,6 +50,7 @@ import org.alfresco.rest.framework.tests.api.mocks.Goat;
|
||||
import org.alfresco.rest.framework.tests.api.mocks.Grass;
|
||||
import org.alfresco.rest.framework.tests.api.mocks.Sheep;
|
||||
import org.alfresco.rest.framework.tests.api.mocks3.FlockEntityResource;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ApiWebScript;
|
||||
import org.junit.Test;
|
||||
@@ -90,7 +91,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
public void testInvokeGet() throws IOException
|
||||
{
|
||||
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.GET);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
AbstractResourceWebScript executor = getExecutor();
|
||||
Object result = executor.execute(entityResource, Params.valueOf((String)null, null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), true);
|
||||
assertNotNull(result);
|
||||
|
||||
@@ -98,7 +99,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
entityResource = locator.locateEntityResource(api,"cow", HttpMethod.GET);
|
||||
result = executor.execute(entityResource, Params.valueOf((String)null, null, mock(WebScriptRequest.class)), response, true);
|
||||
assertNotNull(result);
|
||||
verify(response, times(1)).setCache((Cache) ApiWebScript.CACHE_NEVER);
|
||||
verify(response, times(1)).setCache((Cache) ApiAssistant.CACHE_NEVER);
|
||||
|
||||
response = mock(WebScriptResponse.class);
|
||||
result = executor.execute(entityResource, Params.valueOf("543", null, mock(WebScriptRequest.class)), response, true);
|
||||
@@ -135,7 +136,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
@Test
|
||||
public void testInvokePost() throws IOException
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPost");
|
||||
AbstractResourceWebScript executor = getExecutor("executorOfPost");
|
||||
|
||||
ResourceWithMetadata resource = locator.locateRelationResource(api, "sheep", "blacksheep", HttpMethod.POST);
|
||||
final Sheep aSheep = new Sheep("xyz");
|
||||
@@ -178,7 +179,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
public void testInvokeDelete() throws IOException
|
||||
{
|
||||
ResourceWithMetadata grassResource = locator.locateEntityResource(api,"grass", HttpMethod.DELETE);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfDelete");
|
||||
AbstractResourceWebScript executor = getExecutor("executorOfDelete");
|
||||
Object result = executor.execute(grassResource, Params.valueOf("4", null, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
|
||||
assertNull(result);
|
||||
|
||||
@@ -216,7 +217,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
public void testInvokePut() throws IOException
|
||||
{
|
||||
ResourceWithMetadata entityResource = locator.locateEntityResource(api,"sheep", HttpMethod.PUT);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfPut");
|
||||
AbstractResourceWebScript executor = getExecutor("executorOfPut");
|
||||
final Sheep aSheep = new Sheep("xyz");
|
||||
Object result = executor.execute(entityResource, Params.valueOf("654", null, NULL_PARAMS, aSheep, mock(WebScriptRequest.class)), mock(WebScriptResponse.class), false);
|
||||
assertNotNull(result);
|
||||
@@ -258,16 +259,13 @@ public class ExecutionTests extends AbstractContextTest
|
||||
@Test
|
||||
public void testInvokeAbstract() throws IOException
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.setLocator(locator);
|
||||
executor.setResolver(simpleMappingExceptionResolver);
|
||||
executor.setJsonHelper(jsonHelper);
|
||||
AbstractResourceWebScript executor = getExecutor();
|
||||
Map<String, String> templateVars = new HashMap();
|
||||
templateVars.put("apiScope", "private");
|
||||
templateVars.put("apiVersion", "1");
|
||||
templateVars.put("apiName", "alfrescomock");
|
||||
templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "sheep");
|
||||
executor.execute(executor.determineApi(templateVars), mockRequest(templateVars,new HashMap<String, List<String>>(1)), mock(WebScriptResponse.class));
|
||||
executor.execute(ApiAssistant.determineApi(templateVars), mockRequest(templateVars,new HashMap<String, List<String>>(1)), mock(WebScriptResponse.class));
|
||||
|
||||
WebScriptResponse response = mockResponse();
|
||||
templateVars.put(ResourceLocator.COLLECTION_RESOURCE, "bad");
|
||||
@@ -286,10 +284,8 @@ public class ExecutionTests extends AbstractContextTest
|
||||
@Test
|
||||
public void testInvalidUrls() throws IOException
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.setLocator(locator);
|
||||
executor.setResolver(simpleMappingExceptionResolver);
|
||||
executor.setJsonHelper(jsonHelper);
|
||||
AbstractResourceWebScript executor = getExecutor();
|
||||
|
||||
Map<String, String> templateVars = new HashMap();
|
||||
templateVars.put("apiScope", "private");
|
||||
templateVars.put("apiVersion", "1");
|
||||
@@ -305,13 +301,11 @@ public class ExecutionTests extends AbstractContextTest
|
||||
@Test
|
||||
public void testRenderError() throws IOException
|
||||
{
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
executor.setResolver(simpleMappingExceptionResolver);
|
||||
executor.setJsonHelper(jsonHelper);
|
||||
AbstractResourceWebScript executor = getExecutor();
|
||||
|
||||
ErrorResponse defaultError = new DefaultExceptionResolver().resolveException(new NullPointerException());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
executor.renderErrorResponse(defaultError, mockResponse(out));
|
||||
apiAssistant.renderErrorResponse(defaultError, mockResponse(out));
|
||||
String errorMessage = out.toString();
|
||||
// System.out.println(errorMessage);
|
||||
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.ApiDefault\""));
|
||||
@@ -322,7 +316,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
|
||||
ErrorResponse anError = simpleMappingExceptionResolver.resolveException(new ApiException("nothing"));
|
||||
out = new ByteArrayOutputStream();
|
||||
executor.renderErrorResponse(anError, mockResponse(out));
|
||||
apiAssistant.renderErrorResponse(anError, mockResponse(out));
|
||||
errorMessage = out.toString();
|
||||
// System.out.println(errorMessage);
|
||||
assertTrue(errorMessage.contains("\"errorKey\":\"nothing\""));
|
||||
@@ -332,7 +326,7 @@ public class ExecutionTests extends AbstractContextTest
|
||||
|
||||
anError = simpleMappingExceptionResolver.resolveException(new EntityNotFoundException("2"));
|
||||
out = new ByteArrayOutputStream();
|
||||
executor.renderErrorResponse(anError, mockResponse(out));
|
||||
apiAssistant.renderErrorResponse(anError, mockResponse(out));
|
||||
errorMessage = out.toString();
|
||||
System.out.println(errorMessage);
|
||||
assertTrue(errorMessage.contains("\"errorKey\":\"framework.exception.EntityNotFound\""));
|
||||
|
@@ -76,6 +76,7 @@ import org.alfresco.rest.framework.tests.api.mocks3.GrassEntityResourceNowDelete
|
||||
import org.alfresco.rest.framework.tests.api.mocks3.SheepBlackSheepResourceIsNoMore;
|
||||
import org.alfresco.rest.framework.tests.api.mocks3.SheepEntityResourceWithDeletedMethods;
|
||||
import org.alfresco.rest.framework.tests.api.mocks3.SlimGoat;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.webscripts.ApiWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.WithResponse;
|
||||
import org.alfresco.util.Pair;
|
||||
@@ -465,7 +466,7 @@ public class InspectorTests
|
||||
OperationResourceMetaData operationResourceMetaData = (OperationResourceMetaData) resourceMetadata;
|
||||
Method actionMethod = operationResourceMetaData.getOperationMethod();
|
||||
String result = null;
|
||||
final WithResponse wr = new WithResponse(Status.STATUS_OK, ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
final WithResponse wr = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
|
||||
switch (resourceMetadata.getUniqueId())
|
||||
{
|
||||
|
@@ -58,6 +58,7 @@ import org.alfresco.rest.framework.jacksonextensions.RestJsonModule;
|
||||
import org.alfresco.rest.framework.resource.parameters.Paging;
|
||||
import org.alfresco.rest.framework.resource.parameters.Params;
|
||||
import org.alfresco.rest.framework.tests.api.mocks.Farmer;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.webscripts.ParamsExtractor;
|
||||
import org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete;
|
||||
import org.alfresco.rest.framework.webscripts.ResourceWebScriptGet;
|
||||
@@ -82,7 +83,8 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||
public class ParamsExtractorTests
|
||||
{
|
||||
static JacksonHelper jsonHelper = null;
|
||||
|
||||
static ApiAssistant assistant = null;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTests() throws Exception
|
||||
{
|
||||
@@ -90,6 +92,9 @@ public class ParamsExtractorTests
|
||||
RestJsonModule module = new RestJsonModule();
|
||||
jsonHelper.setModule(module);
|
||||
jsonHelper.afterPropertiesSet();
|
||||
|
||||
assistant = new ApiAssistant();
|
||||
assistant.setJsonHelper(jsonHelper);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -157,7 +162,7 @@ public class ParamsExtractorTests
|
||||
{
|
||||
//Put together the stubs
|
||||
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
|
||||
extractor.setJsonHelper(jsonHelper);
|
||||
extractor.setAssistant(assistant);
|
||||
Map<String, String> templateVars = new HashMap<String, String>();
|
||||
|
||||
Content content = mock(Content.class);
|
||||
@@ -253,7 +258,7 @@ public class ParamsExtractorTests
|
||||
public void testMultiPartPostExtractor() throws Exception
|
||||
{
|
||||
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
|
||||
extractor.setJsonHelper(jsonHelper);
|
||||
extractor.setAssistant(assistant);
|
||||
Map<String, String> templateVars = new HashMap<String, String>();
|
||||
|
||||
WebScriptRequest request = mock(WebScriptRequest.class);
|
||||
@@ -310,7 +315,7 @@ public class ParamsExtractorTests
|
||||
{
|
||||
//Put together the stubs
|
||||
ResourceWebScriptPut extractor = new ResourceWebScriptPut();
|
||||
extractor.setJsonHelper(jsonHelper);
|
||||
extractor.setAssistant(assistant);
|
||||
Map<String, String> templateVars = new HashMap<String, String>();
|
||||
|
||||
Content content = mock(Content.class);
|
||||
@@ -430,7 +435,7 @@ public class ParamsExtractorTests
|
||||
{
|
||||
String specialChars = new String(new char[] { (char) '香' }) + " 香蕉";
|
||||
ResourceWebScriptPost extractor = new ResourceWebScriptPost();
|
||||
extractor.setJsonHelper(jsonHelper);
|
||||
extractor.setAssistant(assistant);
|
||||
Map<String, String> templateVars = new HashMap<String, String>();
|
||||
String mockMe = "{\"name\":\""+specialChars+"\",\"created\":\"2012-03-23T15:56:18.552+0000\",\"age\":54,\"id\":\"1234A3\",\"farm\":\"LARGE\"}";
|
||||
Content content = mock(Content.class);
|
||||
@@ -452,7 +457,7 @@ public class ParamsExtractorTests
|
||||
|
||||
//Test passing in special characters as a param.
|
||||
ResourceWebScriptGet getExtractor = new ResourceWebScriptGet();
|
||||
getExtractor.setJsonHelper(jsonHelper);
|
||||
getExtractor.setAssistant(assistant);
|
||||
Map<String, String> getTemplateVars = new HashMap<String, String>();
|
||||
WebScriptRequest getRequest = mock(WebScriptRequest.class);
|
||||
when(getRequest.getServiceMatch()).thenReturn(new Match(null, getTemplateVars, null));
|
||||
|
@@ -391,7 +391,7 @@ public class SerializeTests extends AbstractContextTest
|
||||
{
|
||||
Api api3 = Api.valueOf("alfrescomock", "private", "3");
|
||||
ResourceWithMetadata propResource = locator.locateRelationResource(api3,"flock", "photo", HttpMethod.GET);
|
||||
AbstractResourceWebScript executor = (AbstractResourceWebScript) applicationContext.getBean("executorOfGets");
|
||||
AbstractResourceWebScript executor = getExecutor();
|
||||
Object result = executor.execute(propResource, Params.valueOf("234", null, null), mock(WebScriptResponse.class), true);
|
||||
assertNotNull(result);
|
||||
}
|
||||
|
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfo;
|
||||
import org.alfresco.rest.framework.resource.content.ContentInfoImpl;
|
||||
import org.alfresco.rest.framework.tools.ApiAssistant;
|
||||
import org.alfresco.rest.framework.webscripts.AbstractResourceWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ApiWebScript;
|
||||
import org.alfresco.rest.framework.webscripts.ResourceWebScriptDelete;
|
||||
@@ -69,17 +70,17 @@ public class WithResponseTest
|
||||
@Test
|
||||
public void testDefaults() throws Exception
|
||||
{
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
assertEquals(Status.STATUS_OK, callBack.getStatus());
|
||||
assertEquals(ApiWebScript.DEFAULT_JSON_CONTENT, callBack.getContentInfo());
|
||||
assertEquals(ApiWebScript.CACHE_NEVER, callBack.getCache());
|
||||
assertEquals(ApiAssistant.DEFAULT_JSON_CONTENT, callBack.getContentInfo());
|
||||
assertEquals(ApiAssistant.CACHE_NEVER, callBack.getCache());
|
||||
assertTrue(callBack.getHeaders().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetHeader() throws Exception
|
||||
{
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
callBack.setHeader("king", "can");
|
||||
callBack.setHeader("king", "kong");
|
||||
assertTrue(callBack.getHeaders().size() == 1);
|
||||
@@ -91,7 +92,7 @@ public class WithResponseTest
|
||||
@Test
|
||||
public void testAddHeader() throws Exception
|
||||
{
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK,ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
callBack.addHeader("king", "can");
|
||||
callBack.addHeader("king", "kong");
|
||||
assertTrue(callBack.getHeaders().size() == 1);
|
||||
@@ -104,7 +105,7 @@ public class WithResponseTest
|
||||
@Test
|
||||
public void testSetters() throws Exception
|
||||
{
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
WithResponse callBack = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
callBack.setStatus(Status.STATUS_GONE);
|
||||
Cache myCache = new Cache(new Description.RequiredCache()
|
||||
{
|
||||
@@ -140,7 +141,8 @@ public class WithResponseTest
|
||||
public void testSetResponse() throws Exception
|
||||
{
|
||||
AbstractResourceWebScript responseWriter = new ResourceWebScriptDelete();
|
||||
WithResponse wr = new WithResponse(Status.STATUS_OK, ApiWebScript.DEFAULT_JSON_CONTENT, ApiWebScript.CACHE_NEVER);
|
||||
responseWriter.setAssistant(new ApiAssistant());
|
||||
WithResponse wr = new WithResponse(Status.STATUS_OK, ApiAssistant.DEFAULT_JSON_CONTENT, ApiAssistant.CACHE_NEVER);
|
||||
|
||||
WebScriptResponse response = mock(WebScriptResponse.class);
|
||||
|
||||
|
@@ -32,6 +32,10 @@
|
||||
<bean id="jsonHelper" class="org.alfresco.rest.framework.jacksonextensions.JacksonHelper">
|
||||
<property name="module" ref="restJsonModule" />
|
||||
</bean>
|
||||
<bean id="apiAssistant" class="org.alfresco.rest.framework.tools.ApiAssistant">
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
</bean>
|
||||
<bean id="simpleMappingExceptionResolver" class="org.alfresco.rest.framework.core.exceptions.SimpleMappingExceptionResolver">
|
||||
<property name="exceptionMappings">
|
||||
<map>
|
||||
@@ -77,7 +81,6 @@
|
||||
|
||||
<bean id="defaultMetaWriter" class="org.alfresco.rest.framework.webscripts.metadata.WebScriptOptionsMetaData">
|
||||
<property name="lookupDictionary" ref="apiLookup" />
|
||||
<property name="resolver" ref="simpleMappingExceptionResolver" />
|
||||
<property name="jsonHelper" ref="jsonHelper" />
|
||||
<property name="assistant" ref="apiAssistant" />
|
||||
</bean>
|
||||
</beans>
|
||||
|
Reference in New Issue
Block a user