Merged HEAD (5.2) to 5.2.N (5.2.1)

126496 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      122700 gjames: RA-211: Response header handling


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126840 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 11:45:04 +00:00
parent b6eec668c7
commit 551eb0b4d7
4 changed files with 219 additions and 7 deletions

View File

@@ -27,8 +27,12 @@ package org.alfresco.rest.framework.webscripts;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.alfresco.repo.node.integrity.IntegrityException;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.web.scripts.content.ContentStreamer;
@@ -134,6 +138,10 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
}
}
catch (IntegrityException intException)
{
renderErrorResponse(resolveException(intException), res);
}
catch (ApiException apiException)
{
renderErrorResponse(resolveException(apiException), res);
@@ -204,17 +212,39 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
/**
* The response 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.
*
* If you set content information via the contentInfo object and ALSO the headers then "headers" will win because they are
* set last.
*
* @param res
* @param status
* @param cache
* @param contentInfo
* @param headers
*/
protected void setResponse(final WebScriptResponse res, int status, Cache cache, ContentInfo contentInfo, Map<String, String> headers)
public void setResponse(final WebScriptResponse res, int status, Cache cache, ContentInfo contentInfo, Map<String, List<String>> headers)
{
res.setStatus(status);
res.setCache(cache);
if (cache != null) res.setCache(cache);
setContentInfoOnResponse(res,contentInfo);
if (headers != null && !headers.isEmpty())
{
for (Map.Entry<String, List<String>> header:headers.entrySet())
{
for (int i=0; i < header.getValue().size(); i++) {
if (i==0)
{
//If its the first one then set the header overwriting.
res.setHeader(header.getKey(), header.getValue().get(i));
}
else
{
//If its not the first one than update the header
res.addHeader(header.getKey(), header.getValue().get(i));
}
}
}
}
}
protected void setResponse(final WebScriptResponse res, WithResponse withResponse)

View File

@@ -3,7 +3,10 @@ package org.alfresco.rest.framework.webscripts;
import org.alfresco.rest.framework.resource.content.ContentInfo;
import org.springframework.extensions.webscripts.Cache;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@@ -17,7 +20,7 @@ public class WithResponse
{
private ContentInfo contentInfo;
private int status;
private Map<String, String> headers;
private Map<String, List<String>> headers;
private Cache cache;
public WithResponse(int status, ContentInfo contentInfo, Cache cache)
@@ -57,7 +60,27 @@ public class WithResponse
*/
public void setHeader(String name, String value)
{
headers.put(name, value);
headers.put(name, new ArrayList<String>(Arrays.asList(value)));
}
/**
* Adds a response header with the given name and value. This method
* allows a response header to have multiple values.
*
* @param name header name
* @param value header value
*/
public void addHeader(String name, String value)
{
List<String> existing = headers.get(name);
if (existing != null)
{
existing.add(value);
}
else
{
setHeader(name, value);
}
}
/**
@@ -85,10 +108,9 @@ public class WithResponse
return cache;
}
public Map<String, String> getHeaders()
public Map<String, List<String>> getHeaders()
{
return headers;
}
}