Added form handling to RM export REST API meaning it can now be called with a content of either application/json or multipart/form-data.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@15741 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2009-08-13 20:19:36 +00:00
parent 8f1bc603d4
commit 7c575fab00

View File

@@ -28,7 +28,9 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -63,6 +65,7 @@ public class StreamArchive extends StreamContent
protected static final String TEMP_FILE_PREFIX = "export_"; protected static final String TEMP_FILE_PREFIX = "export_";
protected static final String PARAM_NODE_REFS = "nodeRefs"; protected static final String PARAM_NODE_REFS = "nodeRefs";
protected static final String MULTIPART_FORMDATA = "multipart/form-data";
protected ExporterService exporterService; protected ExporterService exporterService;
@@ -85,33 +88,60 @@ public class StreamArchive extends StreamContent
File tempArchiveFile = null; File tempArchiveFile = null;
try try
{ {
// get JSON body NodeRef[] nodeRefs = null;
json = new JSONObject(new JSONTokener(req.getContent().getContent())); String contentType = req.getContentType();
if (MULTIPART_FORMDATA.equals(contentType))
// check the list of NodeRefs is present
if (!json.has(PARAM_NODE_REFS))
{ {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, // get nodeRefs parameter from form
"Mandatory 'nodeRefs' parameter was not provided in request body"); String nodeRefsParam = req.getParameter(PARAM_NODE_REFS);
}
// check the list of NodeRefs is present
JSONArray jsonArray = json.getJSONArray(PARAM_NODE_REFS); if (nodeRefsParam == null)
if (jsonArray.length() != 0)
{
// build the list of NodeRefs
NodeRef[] nodeRefs = new NodeRef[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++)
{ {
NodeRef nodeRef = new NodeRef(jsonArray.getString(i)); throw new WebScriptException(Status.STATUS_BAD_REQUEST,
nodeRefs[i] = nodeRef; "Mandatory 'nodeRefs' parameter was not provided in form data");
} }
// create an archive of the nodes List<NodeRef> listNodeRefs = new ArrayList<NodeRef>(8);
tempArchiveFile = createArchive(nodeRefs); StringTokenizer tokenizer = new StringTokenizer(nodeRefsParam, ",");
while (tokenizer.hasMoreTokens())
{
listNodeRefs.add(new NodeRef(tokenizer.nextToken().trim()));
}
// stream the archive back to the client as an attachment (forcing save as) nodeRefs = new NodeRef[listNodeRefs.size()];
streamContent(req, res, tempArchiveFile, true, tempArchiveFile.getName()); nodeRefs = listNodeRefs.toArray(nodeRefs);
} }
else
{
// presume the request is a JSON request so get JSON body
json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// check the list of NodeRefs is present
if (!json.has(PARAM_NODE_REFS))
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Mandatory 'nodeRefs' parameter was not provided in request body");
}
JSONArray jsonArray = json.getJSONArray(PARAM_NODE_REFS);
if (jsonArray.length() != 0)
{
// build the list of NodeRefs
nodeRefs = new NodeRef[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++)
{
NodeRef nodeRef = new NodeRef(jsonArray.getString(i));
nodeRefs[i] = nodeRef;
}
}
}
// create an archive of the nodes
tempArchiveFile = createArchive(nodeRefs);
// stream the archive back to the client as an attachment (forcing save as)
streamContent(req, res, tempArchiveFile, true, tempArchiveFile.getName());
} }
catch (IOException iox) catch (IOException iox)
{ {