001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Nuxeo - initial API and implementation 011 * 012 */ 013 014package org.nuxeo.ecm.automation.server.jaxrs.batch; 015 016import javax.servlet.http.HttpServletRequest; 017 018import org.codehaus.jackson.node.ObjectNode; 019import org.nuxeo.ecm.automation.core.util.JSONBlobDecoder; 020import org.nuxeo.ecm.core.api.Blob; 021import org.nuxeo.ecm.webengine.jaxrs.context.RequestCleanupHandler; 022import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 023import org.nuxeo.runtime.api.Framework; 024 025/** 026 * Uses a JSON definition to retrive a Blob uploaded in a batch 027 * 028 * @author Tiry (tdelprat@nuxeo.com) 029 */ 030public class JSONBatchBlobDecoder implements JSONBlobDecoder { 031 032 @Override 033 public Blob getBlobFromJSON(ObjectNode jsonObject) { 034 035 Blob blob = null; 036 037 if (!jsonObject.has("upload-batch")) { 038 return null; 039 } 040 041 final String batchId = jsonObject.get("upload-batch").getTextValue(); 042 String fileId = null; 043 if (jsonObject.has("upload-fileId")) { 044 fileId = jsonObject.get("upload-fileId").getTextValue(); 045 } 046 if (fileId != null) { 047 BatchManager bm = Framework.getLocalService(BatchManager.class); 048 blob = bm.getBlob(batchId, fileId); 049 050 if (RequestContext.getActiveContext() != null) { 051 RequestContext.getActiveContext().addRequestCleanupHandler(new RequestCleanupHandler() { 052 @Override 053 public void cleanup(HttpServletRequest request) { 054 BatchManager bm = Framework.getLocalService(BatchManager.class); 055 bm.clean(batchId); 056 } 057 }); 058 } 059 060 } 061 return blob; 062 } 063 064}