mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
ALF-20216 "4.2 Preview: valid API URL returns an error"
ALF-20217 "4.2 Preview: Some errors are formatted with Explorer UI look and feel" Note that the discoverablity urls will not work (this is not a regression from Cloud) - see ALF-20218 ALF-20098 "BM-0012: Run v420b1494_01: Exception from executeScript" Two problems were identified with the public api webscript processing: i) No buffering of requests and responses due to a fix for another bug (this is required due to retrying transactions) ii) Exceptions are handled by the public api framework and JSON responses written out. In some cases, exceptions were slipping through and being handled by the Spring web script framework (which was writing them out as non JSON responses). git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@56385 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
365
source/java/org/alfresco/repo/web/scripts/BufferedRequest.java
Normal file
365
source/java/org/alfresco/repo/web/scripts/BufferedRequest.java
Normal file
@@ -0,0 +1,365 @@
|
||||
package org.alfresco.repo.web.scripts;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream;
|
||||
import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.webscripts.Description.FormatStyle;
|
||||
import org.springframework.extensions.webscripts.Match;
|
||||
import org.springframework.extensions.webscripts.Runtime;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WrappingWebScriptRequest;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
public class BufferedRequest implements WrappingWebScriptRequest
|
||||
{
|
||||
private ThresholdOutputStreamFactory streamFactory;
|
||||
private WebScriptRequest req;
|
||||
private File requestBody;
|
||||
private InputStream contentStream;
|
||||
private BufferedReader contentReader;
|
||||
|
||||
public BufferedRequest(WebScriptRequest req, ThresholdOutputStreamFactory streamFactory)
|
||||
{
|
||||
this.req = req;
|
||||
this.streamFactory = streamFactory;
|
||||
}
|
||||
|
||||
private InputStream bufferInputStream() throws IOException
|
||||
{
|
||||
ThresholdOutputStream bufferStream = streamFactory.newOutputStream();
|
||||
|
||||
try
|
||||
{
|
||||
FileCopyUtils.copy(req.getContent().getInputStream(), bufferStream);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
bufferStream.destroy(); // remove temp file
|
||||
throw e;
|
||||
}
|
||||
|
||||
return bufferStream.getInputStream();
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
if (contentStream != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
contentStream.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
contentStream = null;
|
||||
}
|
||||
if (contentReader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
contentReader.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
contentReader = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
reset();
|
||||
if (requestBody != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
requestBody.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
requestBody = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WrappingWebScriptRequest#getNext()
|
||||
*/
|
||||
@Override
|
||||
public WebScriptRequest getNext()
|
||||
{
|
||||
return req;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#forceSuccessStatus()
|
||||
*/
|
||||
@Override
|
||||
public boolean forceSuccessStatus()
|
||||
{
|
||||
return req.forceSuccessStatus();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getAgent()
|
||||
*/
|
||||
@Override
|
||||
public String getAgent()
|
||||
{
|
||||
return req.getAgent();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContent()
|
||||
*/
|
||||
@Override
|
||||
public Content getContent()
|
||||
{
|
||||
final Content wrapped = req.getContent();
|
||||
return new Content(){
|
||||
|
||||
@Override
|
||||
public String getContent() throws IOException
|
||||
{
|
||||
return wrapped.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEncoding()
|
||||
{
|
||||
return wrapped.getEncoding();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimetype()
|
||||
{
|
||||
return wrapped.getMimetype();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getSize()
|
||||
{
|
||||
return wrapped.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream()
|
||||
{
|
||||
if (BufferedRequest.this.contentReader != null)
|
||||
{
|
||||
throw new IllegalStateException("Reader in use");
|
||||
}
|
||||
if (BufferedRequest.this.contentStream == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
BufferedRequest.this.contentStream = bufferInputStream();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return BufferedRequest.this.contentStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException
|
||||
{
|
||||
if (BufferedRequest.this.contentStream != null)
|
||||
{
|
||||
throw new IllegalStateException("Input Stream in use");
|
||||
}
|
||||
if (BufferedRequest.this.contentReader == null)
|
||||
{
|
||||
String encoding = wrapped.getEncoding();
|
||||
InputStream in = bufferInputStream();
|
||||
BufferedRequest.this.contentReader = new BufferedReader(new InputStreamReader(in, encoding == null ? "ISO-8859-1" : encoding));
|
||||
}
|
||||
return BufferedRequest.this.contentReader;
|
||||
}
|
||||
};
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContentType()
|
||||
*/
|
||||
@Override
|
||||
public String getContentType()
|
||||
{
|
||||
return req.getContentType();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContextPath()
|
||||
*/
|
||||
@Override
|
||||
public String getContextPath()
|
||||
{
|
||||
return req.getContextPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getExtensionPath()
|
||||
*/
|
||||
@Override
|
||||
public String getExtensionPath()
|
||||
{
|
||||
return req.getExtensionPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getFormat()
|
||||
*/
|
||||
@Override
|
||||
public String getFormat()
|
||||
{
|
||||
return req.getFormat();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getFormatStyle()
|
||||
*/
|
||||
@Override
|
||||
public FormatStyle getFormatStyle()
|
||||
{
|
||||
return req.getFormatStyle();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeader(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getHeader(String name)
|
||||
{
|
||||
return req.getHeader(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeaderNames()
|
||||
*/
|
||||
@Override
|
||||
public String[] getHeaderNames()
|
||||
{
|
||||
return req.getHeaderNames();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeaderValues(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String[] getHeaderValues(String name)
|
||||
{
|
||||
return req.getHeaderValues(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getJSONCallback()
|
||||
*/
|
||||
@Override
|
||||
public String getJSONCallback()
|
||||
{
|
||||
return req.getJSONCallback();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameter(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getParameter(String name)
|
||||
{
|
||||
return req.getParameter(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameterNames()
|
||||
*/
|
||||
@Override
|
||||
public String[] getParameterNames()
|
||||
{
|
||||
return req.getParameterNames();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameterValues(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String[] getParameterValues(String name)
|
||||
{
|
||||
return req.getParameterValues(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getPathInfo()
|
||||
*/
|
||||
@Override
|
||||
public String getPathInfo()
|
||||
{
|
||||
return req.getPathInfo();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getQueryString()
|
||||
*/
|
||||
@Override
|
||||
public String getQueryString()
|
||||
{
|
||||
return req.getQueryString();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getRuntime()
|
||||
*/
|
||||
@Override
|
||||
public Runtime getRuntime()
|
||||
{
|
||||
return req.getRuntime();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServerPath()
|
||||
*/
|
||||
@Override
|
||||
public String getServerPath()
|
||||
{
|
||||
return req.getServerPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServiceContextPath()
|
||||
*/
|
||||
@Override
|
||||
public String getServiceContextPath()
|
||||
{
|
||||
return req.getServiceContextPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServiceMatch()
|
||||
*/
|
||||
@Override
|
||||
public Match getServiceMatch()
|
||||
{
|
||||
return req.getServiceMatch();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServicePath()
|
||||
*/
|
||||
@Override
|
||||
public String getServicePath()
|
||||
{
|
||||
return req.getServicePath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getURL()
|
||||
*/
|
||||
@Override
|
||||
public String getURL()
|
||||
{
|
||||
return req.getURL();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#isGuest()
|
||||
*/
|
||||
@Override
|
||||
public boolean isGuest()
|
||||
{
|
||||
return req.isGuest();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#parseContent()
|
||||
*/
|
||||
@Override
|
||||
public Object parseContent()
|
||||
{
|
||||
return req.parseContent();
|
||||
}
|
||||
}
|
||||
231
source/java/org/alfresco/repo/web/scripts/BufferedResponse.java
Normal file
231
source/java/org/alfresco/repo/web/scripts/BufferedResponse.java
Normal file
@@ -0,0 +1,231 @@
|
||||
package org.alfresco.repo.web.scripts;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.extensions.surf.util.StringBuilderWriter;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Runtime;
|
||||
import org.springframework.extensions.webscripts.WebScriptResponse;
|
||||
import org.springframework.extensions.webscripts.WrappingWebScriptResponse;
|
||||
|
||||
/**
|
||||
* Transactional Buffered Response
|
||||
*/
|
||||
public class BufferedResponse implements WrappingWebScriptResponse
|
||||
{
|
||||
// Logger
|
||||
protected static final Log logger = LogFactory.getLog(BufferedResponse.class);
|
||||
|
||||
private WebScriptResponse res;
|
||||
private int bufferSize;
|
||||
private ByteArrayOutputStream outputStream = null;
|
||||
private StringBuilderWriter outputWriter = null;
|
||||
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param res
|
||||
* @param bufferSize
|
||||
*/
|
||||
public BufferedResponse(WebScriptResponse res, int bufferSize)
|
||||
{
|
||||
this.res = res;
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WrappingWebScriptResponse#getNext()
|
||||
*/
|
||||
public WebScriptResponse getNext()
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#addHeader(java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void addHeader(String name, String value)
|
||||
{
|
||||
res.addHeader(name, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#encodeScriptUrl(java.lang.String)
|
||||
*/
|
||||
public String encodeScriptUrl(String url)
|
||||
{
|
||||
return res.encodeScriptUrl(url);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name)
|
||||
{
|
||||
return res.getEncodeScriptUrlFunction(name);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptResponse#encodeResourceUrl(java.lang.String)
|
||||
*/
|
||||
public String encodeResourceUrl(String url)
|
||||
{
|
||||
return res.encodeResourceUrl(url);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptResponse#getEncodeResourceUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeResourceUrlFunction(String name)
|
||||
{
|
||||
return res.getEncodeResourceUrlFunction(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getOutputStream()
|
||||
*/
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
if (outputStream == null)
|
||||
{
|
||||
if (outputWriter != null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Already buffering output writer");
|
||||
}
|
||||
this.outputStream = new ByteArrayOutputStream(bufferSize);
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getRuntime()
|
||||
*/
|
||||
public Runtime getRuntime()
|
||||
{
|
||||
return res.getRuntime();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getWriter()
|
||||
*/
|
||||
public Writer getWriter() throws IOException
|
||||
{
|
||||
if (outputWriter == null)
|
||||
{
|
||||
if (outputStream != null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Already buffering output stream");
|
||||
}
|
||||
outputWriter = new StringBuilderWriter(bufferSize);
|
||||
}
|
||||
return outputWriter;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#reset()
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
if (outputStream != null)
|
||||
{
|
||||
outputStream.reset();
|
||||
}
|
||||
else if (outputWriter != null)
|
||||
{
|
||||
outputWriter = null;
|
||||
}
|
||||
res.reset();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setCache(org.alfresco.web.scripts.Cache)
|
||||
*/
|
||||
public void setCache(Cache cache)
|
||||
{
|
||||
res.setCache(cache);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setContentType(java.lang.String)
|
||||
*/
|
||||
public void setContentType(String contentType)
|
||||
{
|
||||
res.setContentType(contentType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setContentEncoding(java.lang.String)
|
||||
*/
|
||||
public void setContentEncoding(String contentEncoding)
|
||||
{
|
||||
res.setContentEncoding(contentEncoding);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setHeader(java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void setHeader(String name, String value)
|
||||
{
|
||||
res.setHeader(name, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setStatus(int)
|
||||
*/
|
||||
public void setStatus(int status)
|
||||
{
|
||||
res.setStatus(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write buffered response to underlying response
|
||||
*/
|
||||
public void writeResponse()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (logger.isDebugEnabled() && outputStream != null)
|
||||
{
|
||||
logger.debug("Writing Transactional response: size=" + outputStream.size());
|
||||
}
|
||||
|
||||
if (outputWriter != null)
|
||||
{
|
||||
outputWriter.flush();
|
||||
res.getWriter().write(outputWriter.toString());
|
||||
}
|
||||
else if (outputStream != null)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Writing Transactional response: size=" + outputStream.size());
|
||||
|
||||
outputStream.flush();
|
||||
outputStream.writeTo(res.getOutputStream());
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to commit buffered response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,14 +18,8 @@
|
||||
*/
|
||||
package org.alfresco.repo.web.scripts;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -33,7 +27,6 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import javax.transaction.Status;
|
||||
import javax.transaction.UserTransaction;
|
||||
|
||||
import org.alfresco.error.AlfrescoRuntimeException;
|
||||
import org.alfresco.repo.model.Repository;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
|
||||
@@ -48,34 +41,24 @@ import org.alfresco.service.cmr.security.AuthorityService;
|
||||
import org.alfresco.service.descriptor.DescriptorService;
|
||||
import org.alfresco.service.transaction.TransactionService;
|
||||
import org.alfresco.util.TempFileProvider;
|
||||
import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStream;
|
||||
import org.apache.chemistry.opencmis.server.shared.ThresholdOutputStreamFactory;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.extensions.surf.util.Content;
|
||||
import org.springframework.extensions.surf.util.StringBuilderWriter;
|
||||
import org.springframework.extensions.webscripts.AbstractRuntimeContainer;
|
||||
import org.springframework.extensions.webscripts.Authenticator;
|
||||
import org.springframework.extensions.webscripts.Cache;
|
||||
import org.springframework.extensions.webscripts.Description;
|
||||
import org.springframework.extensions.webscripts.Description.FormatStyle;
|
||||
import org.springframework.extensions.webscripts.Description.RequiredAuthentication;
|
||||
import org.springframework.extensions.webscripts.Description.RequiredTransaction;
|
||||
import org.springframework.extensions.webscripts.Description.RequiredTransactionParameters;
|
||||
import org.springframework.extensions.webscripts.Description.TransactionCapability;
|
||||
import org.springframework.extensions.webscripts.Match;
|
||||
import org.springframework.extensions.webscripts.Runtime;
|
||||
import org.springframework.extensions.webscripts.ServerModel;
|
||||
import org.springframework.extensions.webscripts.WebScript;
|
||||
import org.springframework.extensions.webscripts.WebScriptException;
|
||||
import org.springframework.extensions.webscripts.WebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WebScriptResponse;
|
||||
import org.springframework.extensions.webscripts.WrappingWebScriptRequest;
|
||||
import org.springframework.extensions.webscripts.WrappingWebScriptResponse;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -634,565 +617,4 @@ public class RepositoryContainer extends AbstractRuntimeContainer
|
||||
{
|
||||
super.reset();
|
||||
}
|
||||
|
||||
/**
|
||||
* Transactional Buffered Response
|
||||
*/
|
||||
private static class BufferedResponse implements WrappingWebScriptResponse
|
||||
{
|
||||
private WebScriptResponse res;
|
||||
private int bufferSize;
|
||||
private ByteArrayOutputStream outputStream = null;
|
||||
private StringBuilderWriter outputWriter = null;
|
||||
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param res
|
||||
* @param bufferSize
|
||||
*/
|
||||
public BufferedResponse(WebScriptResponse res, int bufferSize)
|
||||
{
|
||||
this.res = res;
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WrappingWebScriptResponse#getNext()
|
||||
*/
|
||||
public WebScriptResponse getNext()
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#addHeader(java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void addHeader(String name, String value)
|
||||
{
|
||||
res.addHeader(name, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#encodeScriptUrl(java.lang.String)
|
||||
*/
|
||||
public String encodeScriptUrl(String url)
|
||||
{
|
||||
return res.encodeScriptUrl(url);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getEncodeScriptUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeScriptUrlFunction(String name)
|
||||
{
|
||||
return res.getEncodeScriptUrlFunction(name);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptResponse#encodeResourceUrl(java.lang.String)
|
||||
*/
|
||||
public String encodeResourceUrl(String url)
|
||||
{
|
||||
return res.encodeResourceUrl(url);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptResponse#getEncodeResourceUrlFunction(java.lang.String)
|
||||
*/
|
||||
public String getEncodeResourceUrlFunction(String name)
|
||||
{
|
||||
return res.getEncodeResourceUrlFunction(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getOutputStream()
|
||||
*/
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
if (outputStream == null)
|
||||
{
|
||||
if (outputWriter != null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Already buffering output writer");
|
||||
}
|
||||
this.outputStream = new ByteArrayOutputStream(bufferSize);
|
||||
}
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getRuntime()
|
||||
*/
|
||||
public Runtime getRuntime()
|
||||
{
|
||||
return res.getRuntime();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#getWriter()
|
||||
*/
|
||||
public Writer getWriter() throws IOException
|
||||
{
|
||||
if (outputWriter == null)
|
||||
{
|
||||
if (outputStream != null)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Already buffering output stream");
|
||||
}
|
||||
outputWriter = new StringBuilderWriter(bufferSize);
|
||||
}
|
||||
return outputWriter;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#reset()
|
||||
*/
|
||||
public void reset()
|
||||
{
|
||||
if (outputStream != null)
|
||||
{
|
||||
outputStream.reset();
|
||||
}
|
||||
else if (outputWriter != null)
|
||||
{
|
||||
outputWriter = null;
|
||||
}
|
||||
res.reset();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setCache(org.alfresco.web.scripts.Cache)
|
||||
*/
|
||||
public void setCache(Cache cache)
|
||||
{
|
||||
res.setCache(cache);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setContentType(java.lang.String)
|
||||
*/
|
||||
public void setContentType(String contentType)
|
||||
{
|
||||
res.setContentType(contentType);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setContentEncoding(java.lang.String)
|
||||
*/
|
||||
public void setContentEncoding(String contentEncoding)
|
||||
{
|
||||
res.setContentEncoding(contentEncoding);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setHeader(java.lang.String, java.lang.String)
|
||||
*/
|
||||
public void setHeader(String name, String value)
|
||||
{
|
||||
res.setHeader(name, value);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.alfresco.web.scripts.WebScriptResponse#setStatus(int)
|
||||
*/
|
||||
public void setStatus(int status)
|
||||
{
|
||||
res.setStatus(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write buffered response to underlying response
|
||||
*/
|
||||
private void writeResponse()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (logger.isDebugEnabled() && outputStream != null)
|
||||
{
|
||||
logger.debug("Writing Transactional response: size=" + outputStream.size());
|
||||
}
|
||||
|
||||
if (outputWriter != null)
|
||||
{
|
||||
outputWriter.flush();
|
||||
res.getWriter().write(outputWriter.toString());
|
||||
}
|
||||
else if (outputStream != null)
|
||||
{
|
||||
if (logger.isDebugEnabled())
|
||||
logger.debug("Writing Transactional response: size=" + outputStream.size());
|
||||
|
||||
outputStream.flush();
|
||||
outputStream.writeTo(res.getOutputStream());
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new AlfrescoRuntimeException("Failed to commit buffered response", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class BufferedRequest implements WrappingWebScriptRequest
|
||||
{
|
||||
private ThresholdOutputStreamFactory streamFactory;
|
||||
private WebScriptRequest req;
|
||||
private File requestBody;
|
||||
private InputStream contentStream;
|
||||
private BufferedReader contentReader;
|
||||
|
||||
public BufferedRequest(WebScriptRequest req, ThresholdOutputStreamFactory streamFactory)
|
||||
{
|
||||
this.req = req;
|
||||
this.streamFactory = streamFactory;
|
||||
}
|
||||
|
||||
private InputStream bufferInputStream() throws IOException
|
||||
{
|
||||
ThresholdOutputStream bufferStream = streamFactory.newOutputStream();
|
||||
|
||||
try
|
||||
{
|
||||
FileCopyUtils.copy(req.getContent().getInputStream(), bufferStream);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
bufferStream.destroy(); // remove temp file
|
||||
throw e;
|
||||
}
|
||||
|
||||
return bufferStream.getInputStream();
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
if (contentStream != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
contentStream.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
contentStream = null;
|
||||
}
|
||||
if (contentReader != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
contentReader.close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
contentReader = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void close()
|
||||
{
|
||||
reset();
|
||||
if (requestBody != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
requestBody.delete();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
}
|
||||
requestBody = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WrappingWebScriptRequest#getNext()
|
||||
*/
|
||||
@Override
|
||||
public WebScriptRequest getNext()
|
||||
{
|
||||
return req;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#forceSuccessStatus()
|
||||
*/
|
||||
@Override
|
||||
public boolean forceSuccessStatus()
|
||||
{
|
||||
return req.forceSuccessStatus();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getAgent()
|
||||
*/
|
||||
@Override
|
||||
public String getAgent()
|
||||
{
|
||||
return req.getAgent();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContent()
|
||||
*/
|
||||
@Override
|
||||
public Content getContent()
|
||||
{
|
||||
final Content wrapped = req.getContent();
|
||||
return new Content(){
|
||||
|
||||
@Override
|
||||
public String getContent() throws IOException
|
||||
{
|
||||
return wrapped.getContent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getEncoding()
|
||||
{
|
||||
return wrapped.getEncoding();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimetype()
|
||||
{
|
||||
return wrapped.getMimetype();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public long getSize()
|
||||
{
|
||||
return wrapped.getSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream()
|
||||
{
|
||||
if (BufferedRequest.this.contentReader != null)
|
||||
{
|
||||
throw new IllegalStateException("Reader in use");
|
||||
}
|
||||
if (BufferedRequest.this.contentStream == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
BufferedRequest.this.contentStream = bufferInputStream();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return BufferedRequest.this.contentStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader() throws IOException
|
||||
{
|
||||
if (BufferedRequest.this.contentStream != null)
|
||||
{
|
||||
throw new IllegalStateException("Input Stream in use");
|
||||
}
|
||||
if (BufferedRequest.this.contentReader == null)
|
||||
{
|
||||
String encoding = wrapped.getEncoding();
|
||||
InputStream in = bufferInputStream();
|
||||
BufferedRequest.this.contentReader = new BufferedReader(new InputStreamReader(in, encoding == null ? "ISO-8859-1" : encoding));
|
||||
}
|
||||
return BufferedRequest.this.contentReader;
|
||||
}
|
||||
};
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContentType()
|
||||
*/
|
||||
@Override
|
||||
public String getContentType()
|
||||
{
|
||||
return req.getContentType();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getContextPath()
|
||||
*/
|
||||
@Override
|
||||
public String getContextPath()
|
||||
{
|
||||
return req.getContextPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getExtensionPath()
|
||||
*/
|
||||
@Override
|
||||
public String getExtensionPath()
|
||||
{
|
||||
return req.getExtensionPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getFormat()
|
||||
*/
|
||||
@Override
|
||||
public String getFormat()
|
||||
{
|
||||
return req.getFormat();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getFormatStyle()
|
||||
*/
|
||||
@Override
|
||||
public FormatStyle getFormatStyle()
|
||||
{
|
||||
return req.getFormatStyle();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeader(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getHeader(String name)
|
||||
{
|
||||
return req.getHeader(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeaderNames()
|
||||
*/
|
||||
@Override
|
||||
public String[] getHeaderNames()
|
||||
{
|
||||
return req.getHeaderNames();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getHeaderValues(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String[] getHeaderValues(String name)
|
||||
{
|
||||
return req.getHeaderValues(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getJSONCallback()
|
||||
*/
|
||||
@Override
|
||||
public String getJSONCallback()
|
||||
{
|
||||
return req.getJSONCallback();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameter(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String getParameter(String name)
|
||||
{
|
||||
return req.getParameter(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameterNames()
|
||||
*/
|
||||
@Override
|
||||
public String[] getParameterNames()
|
||||
{
|
||||
return req.getParameterNames();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getParameterValues(java.lang.String)
|
||||
*/
|
||||
@Override
|
||||
public String[] getParameterValues(String name)
|
||||
{
|
||||
return req.getParameterValues(name);
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getPathInfo()
|
||||
*/
|
||||
@Override
|
||||
public String getPathInfo()
|
||||
{
|
||||
return req.getPathInfo();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getQueryString()
|
||||
*/
|
||||
@Override
|
||||
public String getQueryString()
|
||||
{
|
||||
return req.getQueryString();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getRuntime()
|
||||
*/
|
||||
@Override
|
||||
public Runtime getRuntime()
|
||||
{
|
||||
return req.getRuntime();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServerPath()
|
||||
*/
|
||||
@Override
|
||||
public String getServerPath()
|
||||
{
|
||||
return req.getServerPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServiceContextPath()
|
||||
*/
|
||||
@Override
|
||||
public String getServiceContextPath()
|
||||
{
|
||||
return req.getServiceContextPath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServiceMatch()
|
||||
*/
|
||||
@Override
|
||||
public Match getServiceMatch()
|
||||
{
|
||||
return req.getServiceMatch();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getServicePath()
|
||||
*/
|
||||
@Override
|
||||
public String getServicePath()
|
||||
{
|
||||
return req.getServicePath();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#getURL()
|
||||
*/
|
||||
@Override
|
||||
public String getURL()
|
||||
{
|
||||
return req.getURL();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#isGuest()
|
||||
*/
|
||||
@Override
|
||||
public boolean isGuest()
|
||||
{
|
||||
return req.isGuest();
|
||||
}
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.extensions.webscripts.WebScriptRequest#parseContent()
|
||||
*/
|
||||
@Override
|
||||
public Object parseContent()
|
||||
{
|
||||
return req.parseContent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user