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,7 +88,33 @@ public class StreamArchive extends StreamContent
File tempArchiveFile = null; File tempArchiveFile = null;
try try
{ {
// get JSON body NodeRef[] nodeRefs = null;
String contentType = req.getContentType();
if (MULTIPART_FORMDATA.equals(contentType))
{
// get nodeRefs parameter from form
String nodeRefsParam = req.getParameter(PARAM_NODE_REFS);
// check the list of NodeRefs is present
if (nodeRefsParam == null)
{
throw new WebScriptException(Status.STATUS_BAD_REQUEST,
"Mandatory 'nodeRefs' parameter was not provided in form data");
}
List<NodeRef> listNodeRefs = new ArrayList<NodeRef>(8);
StringTokenizer tokenizer = new StringTokenizer(nodeRefsParam, ",");
while (tokenizer.hasMoreTokens())
{
listNodeRefs.add(new NodeRef(tokenizer.nextToken().trim()));
}
nodeRefs = new NodeRef[listNodeRefs.size()];
nodeRefs = listNodeRefs.toArray(nodeRefs);
}
else
{
// presume the request is a JSON request so get JSON body
json = new JSONObject(new JSONTokener(req.getContent().getContent())); json = new JSONObject(new JSONTokener(req.getContent().getContent()));
// check the list of NodeRefs is present // check the list of NodeRefs is present
@@ -99,12 +128,14 @@ public class StreamArchive extends StreamContent
if (jsonArray.length() != 0) if (jsonArray.length() != 0)
{ {
// build the list of NodeRefs // build the list of NodeRefs
NodeRef[] nodeRefs = new NodeRef[jsonArray.length()]; nodeRefs = new NodeRef[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) for (int i = 0; i < jsonArray.length(); i++)
{ {
NodeRef nodeRef = new NodeRef(jsonArray.getString(i)); NodeRef nodeRef = new NodeRef(jsonArray.getString(i));
nodeRefs[i] = nodeRef; nodeRefs[i] = nodeRef;
} }
}
}
// create an archive of the nodes // create an archive of the nodes
tempArchiveFile = createArchive(nodeRefs); tempArchiveFile = createArchive(nodeRefs);
@@ -112,7 +143,6 @@ public class StreamArchive extends StreamContent
// stream the archive back to the client as an attachment (forcing save as) // stream the archive back to the client as an attachment (forcing save as)
streamContent(req, res, tempArchiveFile, true, tempArchiveFile.getName()); streamContent(req, res, tempArchiveFile, true, tempArchiveFile.getName());
} }
}
catch (IOException iox) catch (IOException iox)
{ {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, throw new WebScriptException(Status.STATUS_BAD_REQUEST,