diff --git a/source/java/org/alfresco/rest/api/PublicApiHttpServletRequest.java b/source/java/org/alfresco/rest/api/PublicApiHttpServletRequest.java
index c5ba3929d7..ff2fcd6628 100644
--- a/source/java/org/alfresco/rest/api/PublicApiHttpServletRequest.java
+++ b/source/java/org/alfresco/rest/api/PublicApiHttpServletRequest.java
@@ -1,3 +1,4 @@
+
package org.alfresco.rest.api;
import java.io.BufferedInputStream;
@@ -10,50 +11,80 @@ import javax.servlet.http.HttpServletRequestWrapper;
public class PublicApiHttpServletRequest extends HttpServletRequestWrapper
{
- public PublicApiHttpServletRequest(HttpServletRequest request) throws IOException
- {
- super(getWrappedHttpServletRequest(request));
- }
+ private static final String HEADER_CONTENT_TYPE = "Content-Type";
+ public static final String MULTIPART_FORM_DATA = "multipart/form-data";
- public void resetInputStream() throws IOException
- {
- ServletInputStream stream = getInputStream();
- stream.reset();
- }
-
- private static HttpServletRequest getWrappedHttpServletRequest(HttpServletRequest request) throws IOException
- {
- final PublicApiServletInputStream sis = new PublicApiServletInputStream(request.getInputStream());
- HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request)
- {
- public ServletInputStream getInputStream() throws java.io.IOException
- {
- return sis;
- }
- };
- return wrapper;
- }
-
- private static class PublicApiServletInputStream extends ServletInputStream
- {
- private BufferedInputStream in;
+ public PublicApiHttpServletRequest(HttpServletRequest request) throws IOException
+ {
+ super(getWrappedHttpServletRequest(request));
+ }
- PublicApiServletInputStream(InputStream in)
- {
- this.in = new BufferedInputStream(in);
- this.in.mark(8096);
- }
+ public void resetInputStream() throws IOException
+ {
+ ServletInputStream stream = getInputStream();
+ if (stream.markSupported())
+ {
+ stream.reset();
+ }
+ }
- @Override
- public int read() throws IOException
- {
- return in.read();
- }
+ private static HttpServletRequest getWrappedHttpServletRequest(HttpServletRequest request) throws IOException
+ {
+ //TODO is it really necessary to wrap the request into a BufferedInputStream?
+ // If not, then we could remove the check for multipart upload.
+ // The check is needed as we get an IOException (Resetting to invalid mark) for files more than 8193 bytes.
+ boolean resetSupported = true;
+ String contentType = request.getHeader(HEADER_CONTENT_TYPE);
+ if (contentType != null && contentType.startsWith(MULTIPART_FORM_DATA))
+ {
+ resetSupported = false;
+ }
+ final PublicApiServletInputStream sis = new PublicApiServletInputStream(request.getInputStream(), resetSupported);
+ HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request)
+ {
+ public ServletInputStream getInputStream() throws java.io.IOException
+ {
+ return sis;
+ }
+ };
+ return wrapper;
+ }
- @Override
- public void reset() throws IOException
- {
- in.reset();
- }
- }
+ private static class PublicApiServletInputStream extends ServletInputStream
+ {
+ private final InputStream in;
+ private final boolean resetSupported;
+
+ PublicApiServletInputStream(InputStream in, boolean resetSupported)
+ {
+ this.resetSupported = resetSupported;
+ if (resetSupported)
+ {
+ this.in = new BufferedInputStream(in);
+ this.in.mark(8096);
+ }
+ else
+ {
+ this.in = in;
+ }
+ }
+
+ @Override
+ public int read() throws IOException
+ {
+ return in.read();
+ }
+
+ @Override
+ public void reset() throws IOException
+ {
+ in.reset();
+ }
+
+ @Override
+ public boolean markSupported()
+ {
+ return resetSupported;
+ }
+ }
}
diff --git a/source/java/org/alfresco/rest/framework/core/ResourceInspector.java b/source/java/org/alfresco/rest/framework/core/ResourceInspector.java
index 64c7ce0d41..ec35cc49df 100644
--- a/source/java/org/alfresco/rest/framework/core/ResourceInspector.java
+++ b/source/java/org/alfresco/rest/framework/core/ResourceInspector.java
@@ -27,6 +27,8 @@ import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.UniqueId;
import org.alfresco.rest.framework.resource.actions.interfaces.BinaryResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
+import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction;
+import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.ResourceAction;
import org.alfresco.util.Pair;
@@ -58,13 +60,15 @@ public class ResourceInspector
ALL_ENTITY_RESOURCE_INTERFACES.add(EntityResourceAction.Update.class);
ALL_ENTITY_RESOURCE_INTERFACES.add(EntityResourceAction.Delete.class);
ALL_ENTITY_RESOURCE_INTERFACES.add(BinaryResourceAction.Read.class);
-
+ ALL_ENTITY_RESOURCE_INTERFACES.add(MultiPartResourceAction.Create.class);
+
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.Create.class);
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.Read.class);
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.ReadById.class);
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.Update.class);
ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(RelationshipResourceAction.Delete.class);
-
+ ALL_RELATIONSHIP_RESOURCE_INTERFACES.add(MultiPartRelationshipResourceAction.Create.class);
+
ALL_PROPERTY_RESOURCE_INTERFACES.add(BinaryResourceAction.Read.class);
ALL_PROPERTY_RESOURCE_INTERFACES.add(BinaryResourceAction.Delete.class);
ALL_PROPERTY_RESOURCE_INTERFACES.add(BinaryResourceAction.Update.class);
@@ -90,6 +94,7 @@ public class ResourceInspector
findOperation(EntityResourceAction.ReadById.class, HttpMethod.GET, helper);
findOperation(EntityResourceAction.Update.class, HttpMethod.PUT, helper);
findOperation(EntityResourceAction.Delete.class, HttpMethod.DELETE, helper);
+ findOperation(MultiPartResourceAction.Create.class, HttpMethod.POST, helper);
if (resource.isAnnotationPresent(WebApiDeleted.class))
{
@@ -189,7 +194,8 @@ public class ResourceInspector
findOperation(RelationshipResourceAction.Read.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.ReadById.class, HttpMethod.GET, helper);
findOperation(RelationshipResourceAction.Update.class, HttpMethod.PUT, helper);
- findOperation(RelationshipResourceAction.Delete.class, HttpMethod.DELETE, helper);
+ findOperation(RelationshipResourceAction.Delete.class, HttpMethod.DELETE, helper);
+ findOperation(MultiPartRelationshipResourceAction.Create.class, HttpMethod.POST, helper);
if (resource.isAnnotationPresent(WebApiDeleted.class))
{
diff --git a/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartRelationshipResourceAction.java b/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartRelationshipResourceAction.java
new file mode 100644
index 0000000000..740e4c3ae5
--- /dev/null
+++ b/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartRelationshipResourceAction.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2005-2015 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 .
+ */
+
+package org.alfresco.rest.framework.resource.actions.interfaces;
+
+import org.alfresco.rest.framework.resource.parameters.Parameters;
+import org.springframework.extensions.webscripts.servlet.FormData;
+
+/**
+ * @author Jamal Kaabi-Mofrad
+ */
+public interface MultiPartRelationshipResourceAction
+{
+
+ /**
+ * HTTP POST - Upload file content and meta-data into repository
+ */
+ public static interface Create extends ResourceAction
+ {
+ public E create(String entityResourceId, FormData formData, Parameters parameters);
+ }
+}
diff --git a/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartResourceAction.java b/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartResourceAction.java
new file mode 100644
index 0000000000..0e3b5e6318
--- /dev/null
+++ b/source/java/org/alfresco/rest/framework/resource/actions/interfaces/MultiPartResourceAction.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2005-2015 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 .
+ */
+
+package org.alfresco.rest.framework.resource.actions.interfaces;
+
+import org.alfresco.rest.framework.resource.parameters.Parameters;
+import org.springframework.extensions.webscripts.servlet.FormData;
+
+/**
+ * @author Jamal Kaabi-Mofrad
+ */
+public interface MultiPartResourceAction
+{
+
+ /**
+ * HTTP POST - Upload file content and meta-data into repository
+ */
+ public static interface Create extends ResourceAction
+ {
+ public E create(FormData formData, Parameters parameters);
+ }
+}
diff --git a/source/java/org/alfresco/rest/framework/resource/parameters/Parameters.java b/source/java/org/alfresco/rest/framework/resource/parameters/Parameters.java
index f83e547cb0..56b0bf66d4 100644
--- a/source/java/org/alfresco/rest/framework/resource/parameters/Parameters.java
+++ b/source/java/org/alfresco/rest/framework/resource/parameters/Parameters.java
@@ -7,6 +7,7 @@ import org.alfresco.rest.framework.jacksonextensions.BeanPropertiesFilter;
import org.alfresco.rest.framework.resource.content.BasicContentInfo;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.apache.poi.ss.formula.functions.T;
+import org.springframework.extensions.webscripts.Status;
/**
@@ -90,5 +91,12 @@ public interface Parameters
* Gets the basic information about content, typically taken from a HTTPServletRequest.
* @return BasicContentInfo the content info
*/
- BasicContentInfo getContentInfo();
+ BasicContentInfo getContentInfo();
+
+ /**
+ * Gets Web Script status
+ *
+ * @return {@link Status}
+ */
+ public Status getStatus();
}
diff --git a/source/java/org/alfresco/rest/framework/resource/parameters/Params.java b/source/java/org/alfresco/rest/framework/resource/parameters/Params.java
index dc20e9fca2..1415b874bc 100644
--- a/source/java/org/alfresco/rest/framework/resource/parameters/Params.java
+++ b/source/java/org/alfresco/rest/framework/resource/parameters/Params.java
@@ -14,6 +14,7 @@ import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryImpl;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.poi.ss.formula.functions.T;
+import org.springframework.extensions.webscripts.Status;
/**
* Parameters passed in from a Rest client for use in calls to the rest api.
@@ -29,7 +30,8 @@ public class Params implements Parameters
private final RecognizedParams recognizedParams;
private final String addressedProperty;
private final BasicContentInfo contentInfo;
-
+ private final Status status;
+
//Constants
private static final RecognizedParams NULL_PARAMS = new RecognizedParams(null, null, null, null, null, null, null);
private static final BasicContentInfo DEFAULT_CONTENT_INFO = new ContentInfoImpl(MimetypeMap.MIMETYPE_BINARY, "UTF-8", -1, null);
@@ -44,6 +46,7 @@ public class Params implements Parameters
this.recognizedParams = recognizedParams;
this.addressedProperty = addressedProperty;
this.contentInfo = contentInfo==null?DEFAULT_CONTENT_INFO:contentInfo;
+ this.status = new Status();
}
public static Params valueOf(BeanPropertiesFilter paramFilter, String entityId)
@@ -196,10 +199,17 @@ public class Params implements Parameters
}
@Override
- public BasicContentInfo getContentInfo() {
- return contentInfo;
- }
-
+ public BasicContentInfo getContentInfo()
+ {
+ return contentInfo;
+ }
+
+ @Override
+ public Status getStatus()
+ {
+ return status;
+ }
+
/**
* A formal set of params that any rest service could potentially have passed in as request params
*/
diff --git a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java
index c66d2dc9c7..271f30d2b3 100644
--- a/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java
+++ b/source/java/org/alfresco/rest/framework/webscripts/AbstractResourceWebScript.java
@@ -72,7 +72,14 @@ public abstract class AbstractResourceWebScript extends ApiWebScript implements
{
respons.put("toSerialize", result);
respons.put("contentInfo", contentInfo);
- setSuccessResponseStatus(res);
+ if (params.getStatus().getRedirect())
+ {
+ res.setStatus(params.getStatus().getCode());
+ }
+ else
+ {
+ setSuccessResponseStatus(res);
+ }
}
});
diff --git a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java
index 28bfd190b6..fcd4364913 100644
--- a/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java
+++ b/source/java/org/alfresco/rest/framework/webscripts/ResourceWebScriptPost.java
@@ -13,6 +13,8 @@ import org.alfresco.rest.framework.core.exceptions.DeletedResourceException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException;
import org.alfresco.rest.framework.resource.actions.interfaces.EntityResourceAction;
+import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartResourceAction;
+import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Params;
@@ -20,7 +22,9 @@ import org.alfresco.rest.framework.resource.parameters.Params.RecognizedParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
+import org.springframework.extensions.webscripts.WebScriptRequestImpl;
import org.springframework.extensions.webscripts.WebScriptResponse;
+import org.springframework.extensions.webscripts.servlet.FormData;
import org.springframework.http.HttpMethod;
/**
@@ -55,7 +59,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
}
else
{
- Object postedObj = extractObjFromJson(resourceMeta, req);
+ Object postedObj = processRequest(resourceMeta, req);
return Params.valueOf(null, params, postedObj);
}
case RELATIONSHIP:
@@ -63,18 +67,33 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
String relationshipId = req.getServiceMatch().getTemplateVars().get(ResourceLocator.RELATIONSHIP_ID);
if (StringUtils.isNotBlank(relationshipId))
{
- throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
+ throw new UnsupportedResourceOperationException("POST is executed against the collection URL");
}
else
{
- Object postedRel = extractObjFromJson(resourceMeta, req);
- return Params.valueOf(entityId,params,postedRel);
+ Object postedRel = processRequest(resourceMeta, req);
+ return Params.valueOf(entityId, params, postedRel);
}
default:
throw new UnsupportedResourceOperationException("POST not supported for Actions");
}
}
+ /**
+ * If the request content-type is multipart/form-data then it
+ * returns the {@link FormData}, otherwise it tries to extract the required
+ * object from the JSON payload.
+ */
+ private Object processRequest(ResourceMetadata resourceMeta, WebScriptRequest req)
+ {
+ if (WebScriptRequestImpl.MULTIPART_FORM_DATA.equals(req.getContentType()))
+ {
+ return (FormData) req.parseContent();
+ }
+
+ return extractObjFromJson(resourceMeta, req);
+ }
+
/**
* If the @WebApiParam has been used and set allowMultiple to false then this will get a single entry. It
* should error if an array is passed in.
@@ -86,7 +105,7 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
{
List params = resourceMeta.getParameters(HttpMethod.POST);
Class> objType = resourceMeta.getObjectType(HttpMethod.POST);
-
+
if (!params.isEmpty())
{
for (ResourceParameter resourceParameter : params)
@@ -125,39 +144,60 @@ public class ResourceWebScriptPost extends AbstractResourceWebScript implements
@SuppressWarnings("unchecked")
private Object executeInternal(ResourceWithMetadata resource, Params params)
{
+ final Object resObj = resource.getResource();
switch (resource.getMetaData().getType())
{
case ENTITY:
if (resource.getMetaData().isDeleted(EntityResourceAction.Create.class))
{
- throw new DeletedResourceException("(DELETE) "+resource.getMetaData().getUniqueId());
+ throw new DeletedResourceException("(DELETE) " + resource.getMetaData().getUniqueId());
}
- EntityResourceAction.Create