001/* 002 * (C) Copyright 2006-2011 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 * Nuxeo - initial API and implementation 018 * 019 */ 020 021package org.nuxeo.ecm.automation.server.jaxrs.batch; 022 023import javax.servlet.http.HttpServletRequest; 024 025import org.nuxeo.ecm.automation.core.util.JSONBlobDecoder; 026import org.nuxeo.ecm.core.api.Blob; 027import org.nuxeo.ecm.webengine.jaxrs.context.RequestCleanupHandler; 028import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 029import org.nuxeo.runtime.api.Framework; 030 031import com.fasterxml.jackson.databind.node.ObjectNode; 032 033/** 034 * Uses a JSON definition to retrive a Blob uploaded in a batch 035 * 036 * @author Tiry (tdelprat@nuxeo.com) 037 */ 038public class JSONBatchBlobDecoder implements JSONBlobDecoder { 039 040 @Override 041 public Blob getBlobFromJSON(ObjectNode jsonObject) { 042 043 Blob blob = null; 044 045 if (!jsonObject.has("upload-batch")) { 046 return null; 047 } 048 049 final String batchId = jsonObject.get("upload-batch").textValue(); 050 String fileId = null; 051 if (jsonObject.has("upload-fileId")) { 052 fileId = jsonObject.get("upload-fileId").textValue(); 053 } 054 if (fileId != null) { 055 BatchManager bm = Framework.getService(BatchManager.class); 056 Batch batch = bm.getBatch(batchId); 057 if (batch == null) { 058 return null; 059 } 060 blob = batch.getBlob(fileId); 061 062 if (RequestContext.getActiveContext() != null) { 063 final boolean drop = !Boolean.parseBoolean( 064 RequestContext.getActiveContext().getRequest().getHeader(BatchManagerConstants.NO_DROP_FLAG)); 065 if (drop) { 066 RequestContext.getActiveContext().addRequestCleanupHandler(new RequestCleanupHandler() { 067 @Override 068 public void cleanup(HttpServletRequest request) { 069 BatchManager bm = Framework.getService(BatchManager.class); 070 bm.clean(batchId); 071 } 072 }); 073 } 074 } 075 } 076 return blob; 077 } 078 079}