Commit 8ab396fc authored by Brian Long's avatar Brian Long
Browse files

fixed multipart (upload) support

parent 9a24fff0
Loading
Loading
Loading
Loading
+17 −4
Original line number Diff line number Diff line
package com.inteligr8.alfresco.acs.api;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.inteligr8.alfresco.acs.model.AssociationBody;
import com.inteligr8.alfresco.acs.model.AssociationEntry;
import com.inteligr8.alfresco.acs.model.ChildAssociationBody;
@@ -13,14 +29,11 @@ import com.inteligr8.alfresco.acs.model.NodeBodyMove;
import com.inteligr8.alfresco.acs.model.NodeBodyUpdate;
import com.inteligr8.alfresco.acs.model.NodeChildAssociationPaging;
import com.inteligr8.alfresco.acs.model.NodeEntry;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.io.File;
import java.util.Date;
import java.util.List;
import javax.ws.rs.*;

/**
 * Alfresco Content Services REST API
+2 −3
Original line number Diff line number Diff line
@@ -7,9 +7,8 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;

import com.inteligr8.alfresco.acs.model.Error;
import com.inteligr8.alfresco.acs.model.NodeBodyCreateMultipartCxf;
import com.inteligr8.alfresco.acs.model.NodeEntry;

import io.swagger.annotations.Api;
@@ -40,7 +39,7 @@ public interface NodesCxfApi {
        @ApiResponse(code = 200, message = "Unexpected error", response = Error.class) })
    public NodeEntry createNode(
    		@PathParam("nodeId") String nodeId,
    		MultipartBody body);
    		NodeBodyCreateMultipartCxf body);
/*
 * This better impl doesn't work
 *
+2 −15
Original line number Diff line number Diff line
package com.inteligr8.alfresco.acs.api;

import java.io.InputStream;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@@ -10,11 +7,8 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import com.inteligr8.alfresco.acs.model.Error;
import com.inteligr8.alfresco.acs.model.NodeBodyCreate;
import com.inteligr8.alfresco.acs.model.NodeBodyCreateMultipartJersey;
import com.inteligr8.alfresco.acs.model.NodeEntry;

import io.swagger.annotations.Api;
@@ -45,13 +39,6 @@ public interface NodesJerseyApi {
        @ApiResponse(code = 200, message = "Unexpected error", response = Error.class) })
    public NodeEntry createNode(
    		@PathParam("nodeId") String nodeId,
    		NodeBodyCreate nodeBodyCreate,
    		@FormDataParam("autoRename") Boolean autoRename,
    		@FormDataParam("majorVersion") Boolean majorVersion,
    		@FormDataParam("versioningEnabled") Boolean versioningEnabled,
    		@FormDataParam("include") List<String> include,
    		@FormDataParam("fields") List<String> fields,
    		@FormDataParam("filedata") InputStream filedataStream,
    		@FormDataParam("filedata") FormDataContentDisposition filedataDisposition);
    		NodeBodyCreateMultipartJersey file);

}
+0 −28
Original line number Diff line number Diff line
package com.inteligr8.alfresco.acs.model;

import java.io.File;

import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModelProperty;

public class NodeBodyCreateFile extends NodeBodyCreate {
	
	@ApiModelProperty
	private File filedata;
	
	@JsonProperty
	public File getFiledata() {
		return this.filedata;
	}
	
	public void setFiledata(File filedata) {
		this.filedata = filedata;
	}
	
	public NodeBodyCreateFile filedata(File filedata) {
		this.filedata = filedata;
		return this;
	}

}
+26 −27
Original line number Diff line number Diff line
package com.inteligr8.alfresco.acs.api;
package com.inteligr8.alfresco.acs.model;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inteligr8.alfresco.acs.model.NodeBodyCreate;

public class NodeBodyCreateMultipart extends MultipartBody {
public class NodeBodyCreateMultipartCxf extends MultipartBody {
	
	private static final Logger logger = LoggerFactory.getLogger(NodeBodyCreateMultipartCxf.class);
	private static final ObjectMapper om = new ObjectMapper();
	
	public static NodeBodyCreateMultipart from(
	public static NodeBodyCreateMultipartCxf from(
			NodeBodyCreate nodeBody, String filename, InputStream istream,
			Boolean autoRename, Boolean majorVersion, Boolean versioningEnabled) throws IOException {
		List<Attachment> atts = new LinkedList<>();
@@ -34,14 +35,14 @@ public class NodeBodyCreateMultipart extends MultipartBody {
		if (versioningEnabled != null)
			atts.add(toAttachment("versioningEnabled", String.valueOf(versioningEnabled)));
		atts.add(toAttachment(filename, istream));
		return new NodeBodyCreateMultipart(atts, true);
		return new NodeBodyCreateMultipartCxf(atts, true);
	}
 	
	public NodeBodyCreateMultipart(List<Attachment> atts) throws IOException {
	public NodeBodyCreateMultipartCxf(List<Attachment> atts) throws IOException {
		super(atts);
	}
 	
	public NodeBodyCreateMultipart(List<Attachment> atts, boolean outbound) throws IOException {
	public NodeBodyCreateMultipartCxf(List<Attachment> atts, boolean outbound) throws IOException {
		super(atts, outbound);
	}
	
@@ -63,37 +64,35 @@ public class NodeBodyCreateMultipart extends MultipartBody {
	
	
	
	@SuppressWarnings("unchecked")
	private static List<Attachment> toAttachments(NodeBodyCreate nodeBody) throws IOException {
		List<Attachment> atts = new LinkedList<>();
		atts.add(toAttachment("name", nodeBody.getName()));
		atts.add(toAttachment("nodeType", nodeBody.getNodeType()));
		if (nodeBody.getAspectNames() != null)
			atts.add(toAttachment("aspectNames", nodeBody.getAspectNames()));
		if (nodeBody.getProperties() != null)
			atts.add(toAttachment("properties", (Map<String, Object>)nodeBody.getProperties()));
		return atts;
		if (nodeBody.getAspectNames() != null && !nodeBody.getAspectNames().isEmpty())
			logger.warn("The ACS Public REST API does not support the explicit inclusion of aspects while creating content");
		if (nodeBody.getProperties() != null) {
			@SuppressWarnings("unchecked")
			Map<String, Object> props = (Map<String, Object>)nodeBody.getProperties();
			for (Entry<String, Object> prop : props.entrySet()) {
				if (prop.getValue() != null) {
					// FIXME convert dates as ACS would expect them to be formatted
					atts.add(toAttachment(prop.getKey(), prop.getValue().toString()));
				}
	
	private static Attachment toAttachment(String name, String value) {
		return new Attachment(name, new ByteArrayInputStream(value.getBytes()), new ContentDisposition("form-data; name=\"" + name + "\""));
			}
	
	private static Attachment toAttachment(String name, Collection<String> c) throws JsonProcessingException {
		return toJsonAttachment(name, c);
		}
	
	private static Attachment toAttachment(String name, Map<String, Object> map) throws JsonProcessingException {
		return toJsonAttachment(name, map);
		return atts;
	}
	
	private static Attachment toJsonAttachment(String name, Object obj) throws JsonProcessingException {
		String json = om.writeValueAsString(obj);
		return new Attachment(name, new ByteArrayInputStream(json.getBytes()), new ContentDisposition("form-data; name=\"" + name + "\""));
	private static Attachment toAttachment(String name, String value) {
		return new Attachment(name, new ByteArrayInputStream(value.getBytes()), new ContentDisposition("form-data; name=\"" + name + "\""));
	}

	private static Attachment toAttachment(String filename, InputStream istream) {
		if (filename == null) {
			return new Attachment("filedata", istream, new ContentDisposition("form-data; name=\"filedata\""));
		} else {
			return new Attachment("filedata", istream, new ContentDisposition("form-data; name=\"filedata\"; filename=\"" + filename + "\""));
		}
	}

}
Loading