Commit 938ac213 authored by Brian Long's avatar Brian Long
Browse files

fixed multipart (upload) support

parent 285506b7
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);

}
+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 + "\""));
		}
	}

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

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
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.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;

public class NodeBodyCreateMultipartJersey extends FormDataMultiPart {

	private static final Logger logger = LoggerFactory.getLogger(NodeBodyCreateMultipartJersey.class);
	private static final ObjectMapper om = new ObjectMapper();
	
	public static NodeBodyCreateMultipartJersey from(
			NodeBodyCreate nodeBody, String filename, InputStream istream,
			Boolean autoRename, Boolean majorVersion, Boolean versioningEnabled) throws IOException, ParseException {
		NodeBodyCreateMultipartJersey multipart = new NodeBodyCreateMultipartJersey();
		multipart.field("autoRename", String.valueOf(autoRename))
				.field("majorVersion", String.valueOf(majorVersion))
				.field("versioningEnabled", String.valueOf(versioningEnabled))
				.bodyPart(toBodyPart(filename, istream))
				.getBodyParts().addAll(toFields(nodeBody));
		return multipart;
	}
 	
	private NodeBodyCreateMultipartJersey() throws IOException {
	}
	
	public NodeBodyCreate getBody() throws IOException {
		BodyPart bodyPart = this.getField("");
		if (bodyPart == null)
			throw new IllegalStateException();
		if (!MediaType.APPLICATION_JSON_TYPE.equals(bodyPart.getMediaType()))
			throw new IllegalStateException();
		
		InputStream istream = bodyPart.getEntityAs(InputStream.class);
		try {
			return om.readValue(istream, NodeBodyCreate.class);
		} finally {
			istream.close();
		}
	}
	
	public FormDataBodyPart getFiledataAttachment() {
		return this.getField("filedata");
	}
	
	
	
	private static List<FormDataBodyPart> toFields(NodeBodyCreate nodeBody) throws IOException {
		List<FormDataBodyPart> fields = new LinkedList<>();
		fields.add(new FormDataBodyPart("name", nodeBody.getName()));
		fields.add(new FormDataBodyPart("nodeType", nodeBody.getNodeType()));
		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
					fields.add(new FormDataBodyPart(prop.getKey(), prop.getValue().toString()));
				}
			}
		}
		
		return fields;
	}

	private static BodyPart toBodyPart(String filename, InputStream istream) throws ParseException {
		if (filename == null) {
			return new FormDataBodyPart()
					.contentDisposition(new FormDataContentDisposition("form-data; name=\"filedata\""))
					.entity(istream);
		} else {
			return new FormDataBodyPart()
					.contentDisposition(new FormDataContentDisposition("form-data; name=\"filedata\"; filename=\"" + filename + "\""))
					.entity(istream);
		}
	}

}
Loading