001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * vpasquier <vpasquier@nuxeo.com> 018 */ 019package org.nuxeo.box.api.file; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.io.Serializable; 024import java.lang.reflect.InvocationTargetException; 025import java.text.ParseException; 026 027import javax.ws.rs.Consumes; 028import javax.ws.rs.DELETE; 029import javax.ws.rs.GET; 030import javax.ws.rs.POST; 031import javax.ws.rs.PUT; 032import javax.ws.rs.Path; 033import javax.ws.rs.PathParam; 034import javax.ws.rs.Produces; 035import javax.ws.rs.core.MediaType; 036 037import org.nuxeo.box.api.adapter.BoxAdapter; 038import org.nuxeo.box.api.file.adapter.BoxFileAdapter; 039import org.nuxeo.box.api.marshalling.dao.BoxFile; 040import org.nuxeo.box.api.marshalling.exceptions.BoxJSONException; 041import org.nuxeo.box.api.service.BoxService; 042import org.nuxeo.ecm.core.api.Blobs; 043import org.nuxeo.ecm.core.api.CoreSession; 044import org.nuxeo.ecm.core.api.DocumentModel; 045import org.nuxeo.ecm.core.api.DocumentNotFoundException; 046import org.nuxeo.ecm.core.api.IdRef; 047import org.nuxeo.ecm.webengine.model.WebObject; 048import org.nuxeo.ecm.webengine.model.impl.AbstractResource; 049import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl; 050import org.nuxeo.runtime.api.Framework; 051 052import com.sun.jersey.multipart.FormDataParam; 053 054/** 055 * WebObject for a Box File 056 * 057 * @since 5.9.2 058 */ 059@WebObject(type = "file") 060@Produces({ MediaType.APPLICATION_JSON }) 061public class BoxFileObject extends AbstractResource<ResourceTypeImpl> { 062 063 BoxService boxService; 064 065 @Override 066 public void initialize(Object... args) { 067 boxService = Framework.getLocalService(BoxService.class); 068 } 069 070 @GET 071 public Object doGet() { 072 return getView("index"); 073 } 074 075 @GET 076 @Path("{fileId}") 077 public String doGetFile(@PathParam("fileId") final String fileId) throws DocumentNotFoundException, 078 BoxJSONException { 079 final CoreSession session = ctx.getCoreSession(); 080 final DocumentModel file = session.getDocument(new IdRef(fileId)); 081 // Adapt nx document to box folder adapter 082 final BoxFileAdapter fileAdapter = (BoxFileAdapter) file.getAdapter(BoxAdapter.class); 083 return boxService.toJSONString(fileAdapter.getBoxItem()); 084 } 085 086 @DELETE 087 @Path("{fileId}") 088 public void doDeleteFile(@PathParam("fileId") String fileId) { 089 final CoreSession session = ctx.getCoreSession(); 090 session.removeDocument(new IdRef(fileId)); 091 session.save(); 092 } 093 094 @PUT 095 @Path("{fileId}") 096 public String doUpdateFile(@PathParam("fileId") String fileId, String jsonBoxFile) throws 097 BoxJSONException, ParseException, IllegalAccessException, InvocationTargetException { 098 final CoreSession session = ctx.getCoreSession(); 099 // Fetch the nx document with given id 100 final DocumentModel nxDocument = session.getDocument(new IdRef(fileId)); 101 // Create box File from json payload 102 BoxFile boxFileUpdated = boxService.getBoxFile(jsonBoxFile); 103 // Adapt nx document to box File adapter 104 final BoxFileAdapter nxDocumentAdapter = (BoxFileAdapter) nxDocument.getAdapter(BoxAdapter.class); 105 // Update both nx document and box File adapter 106 nxDocumentAdapter.setBoxItem(boxFileUpdated); 107 nxDocumentAdapter.save(session); 108 // Return the new box File json 109 return boxService.toJSONString(nxDocumentAdapter.getBoxItem()); 110 } 111 112 @POST 113 @Path("content") 114 @Consumes(MediaType.MULTIPART_FORM_DATA) 115 public String doPostFile(@FormDataParam("file") InputStream uploadedInputStream, 116 @FormDataParam("filename") String fileName, @FormDataParam("parent_id") String parentId) 117 throws BoxJSONException, IOException { 118 // Fetching its parent to get parent id 119 final CoreSession session = ctx.getCoreSession(); 120 DocumentModel documentParent; 121 if ("0".equals(parentId)) { 122 documentParent = session.getRootDocument(); 123 } else { 124 documentParent = session.getDocument(new IdRef(parentId)); 125 } 126 // Create the nx document from box item information 127 DocumentModel newFile = session.createDocumentModel(documentParent.getPathAsString(), fileName, "File"); 128 newFile.setPropertyValue("file:content", (Serializable) Blobs.createBlob(uploadedInputStream)); 129 newFile = session.createDocument(newFile); 130 // Adapt nx document to box folder adapter 131 final BoxFileAdapter fileAdapter = (BoxFileAdapter) newFile.getAdapter(BoxAdapter.class); 132 // Return the new box folder json 133 return boxService.toJSONString(fileAdapter.getBoxItem()); 134 } 135 136 @Path("{fileId}/comments") 137 public Object doGetComments(@PathParam("fileId") String fileId) { 138 return newObject("comment", fileId); 139 } 140 141}