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 org.nuxeo.ecm.automation.core.util.JSONBlobDecoder; 024import org.nuxeo.ecm.core.api.Blob; 025import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext; 026import org.nuxeo.runtime.api.Framework; 027 028import com.fasterxml.jackson.databind.node.ObjectNode; 029 030/** 031 * Uses a JSON definition to retrieve a Blob uploaded in a batch. 032 * <p> 033 * Format is: 034 * 035 * <pre> 036 * { 037 * "upload-batch": "BATCH_ID", <-- the batch id 038 * "upload-fileId": "FILE_ID" <-- the file id 039 * } 040 * </pre> 041 * 042 * @author Tiry ([email protected]) 043 */ 044public class JSONBatchBlobDecoder implements JSONBlobDecoder { 045 046 @Override 047 public Blob getBlobFromJSON(ObjectNode jsonObject) { 048 049 Blob blob = null; 050 051 if (!jsonObject.has("upload-batch")) { 052 return null; 053 } 054 055 final String batchId = jsonObject.get("upload-batch").textValue(); 056 String fileId = null; 057 if (jsonObject.has("upload-fileId")) { 058 fileId = jsonObject.get("upload-fileId").textValue(); 059 } 060 if (fileId != null) { 061 BatchManager bm = Framework.getService(BatchManager.class); 062 Batch batch = bm.getBatch(batchId); 063 if (batch == null) { 064 return null; 065 } 066 blob = batch.getBlob(fileId); 067 068 if (RequestContext.getActiveContext() != null) { 069 final boolean drop = !Boolean.parseBoolean( 070 RequestContext.getActiveContext().getRequest().getHeader(BatchManagerConstants.NO_DROP_FLAG)); 071 if (drop) { 072 RequestContext.getActiveContext().addRequestCleanupHandler(request -> { 073 BatchManager bm1 = Framework.getService(BatchManager.class); 074 bm1.clean(batchId); 075 }); 076 } 077 } 078 } 079 return blob; 080 } 081 082}