001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * vpasquier <vpasquier@nuxeo.com> 016 */ 017package org.nuxeo.box.api.file; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.Serializable; 022import java.lang.reflect.InvocationTargetException; 023import java.text.ParseException; 024 025import javax.ws.rs.Consumes; 026import javax.ws.rs.DELETE; 027import javax.ws.rs.GET; 028import javax.ws.rs.POST; 029import javax.ws.rs.PUT; 030import javax.ws.rs.Path; 031import javax.ws.rs.PathParam; 032import javax.ws.rs.Produces; 033import javax.ws.rs.core.MediaType; 034 035import org.nuxeo.box.api.adapter.BoxAdapter; 036import org.nuxeo.box.api.file.adapter.BoxFileAdapter; 037import org.nuxeo.box.api.marshalling.dao.BoxFile; 038import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException; 039import org.nuxeo.box.api.service.BoxService; 040import org.nuxeo.ecm.core.api.Blobs; 041import org.nuxeo.ecm.core.api.CoreSession; 042import org.nuxeo.ecm.core.api.DocumentModel; 043import org.nuxeo.ecm.core.api.DocumentNotFoundException; 044import org.nuxeo.ecm.core.api.IdRef; 045import org.nuxeo.ecm.webengine.model.WebObject; 046import org.nuxeo.ecm.webengine.model.impl.AbstractResource; 047import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl; 048import org.nuxeo.runtime.api.Framework; 049 050import com.sun.jersey.multipart.FormDataParam; 051 052/** 053 * WebObject for a Box File 054 * 055 * @since 5.9.2 056 */ 057@WebObject(type = "file") 058@Produces({ MediaType.APPLICATION_JSON }) 059public class BoxFileObject extends AbstractResource<ResourceTypeImpl> { 060 061 BoxService boxService; 062 063 @Override 064 public void initialize(Object... args) { 065 boxService = Framework.getLocalService(BoxService.class); 066 } 067 068 @GET 069 public Object doGet() { 070 return getView("index"); 071 } 072 073 @GET 074 @Path("{fileId}") 075 public String doGetFile(@PathParam("fileId") final String fileId) throws DocumentNotFoundException, 076 BoxJSONException { 077 final CoreSession session = ctx.getCoreSession(); 078 final DocumentModel file = session.getDocument(new IdRef(fileId)); 079 // Adapt nx document to box folder adapter 080 final BoxFileAdapter fileAdapter = (BoxFileAdapter) file.getAdapter(BoxAdapter.class); 081 return boxService.toJSONString(fileAdapter.getBoxItem()); 082 } 083 084 @DELETE 085 @Path("{fileId}") 086 public void doDeleteFile(@PathParam("fileId") String fileId) { 087 final CoreSession session = ctx.getCoreSession(); 088 session.removeDocument(new IdRef(fileId)); 089 session.save(); 090 } 091 092 @PUT 093 @Path("{fileId}") 094 public String doUpdateFile(@PathParam("fileId") String fileId, String jsonBoxFile) throws 095 BoxJSONException, ParseException, IllegalAccessException, InvocationTargetException { 096 final CoreSession session = ctx.getCoreSession(); 097 // Fetch the nx document with given id 098 final DocumentModel nxDocument = session.getDocument(new IdRef(fileId)); 099 // Create box File from json payload 100 BoxFile boxFileUpdated = boxService.getBoxFile(jsonBoxFile); 101 // Adapt nx document to box File adapter 102 final BoxFileAdapter nxDocumentAdapter = (BoxFileAdapter) nxDocument.getAdapter(BoxAdapter.class); 103 // Update both nx document and box File adapter 104 nxDocumentAdapter.setBoxItem(boxFileUpdated); 105 nxDocumentAdapter.save(session); 106 // Return the new box File json 107 return boxService.toJSONString(nxDocumentAdapter.getBoxItem()); 108 } 109 110 @POST 111 @Path("content") 112 @Consumes(MediaType.MULTIPART_FORM_DATA) 113 public String doPostFile(@FormDataParam("file") InputStream uploadedInputStream, 114 @FormDataParam("filename") String fileName, @FormDataParam("parent_id") String parentId) 115 throws BoxJSONException, IOException { 116 // Fetching its parent to get parent id 117 final CoreSession session = ctx.getCoreSession(); 118 DocumentModel documentParent; 119 if ("0".equals(parentId)) { 120 documentParent = session.getRootDocument(); 121 } else { 122 documentParent = session.getDocument(new IdRef(parentId)); 123 } 124 // Create the nx document from box item information 125 DocumentModel newFile = session.createDocumentModel(documentParent.getPathAsString(), fileName, "File"); 126 newFile.setPropertyValue("file:content", (Serializable) Blobs.createBlob(uploadedInputStream)); 127 newFile = session.createDocument(newFile); 128 // Adapt nx document to box folder adapter 129 final BoxFileAdapter fileAdapter = (BoxFileAdapter) newFile.getAdapter(BoxAdapter.class); 130 // Return the new box folder json 131 return boxService.toJSONString(fileAdapter.getBoxItem()); 132 } 133 134 @Path("{fileId}/comments") 135 public Object doGetComments(@PathParam("fileId") String fileId) { 136 return newObject("comment", fileId); 137 } 138 139}