001/*
002 * (C) Copyright 2015 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 *     Antoine Taillefer <ataillefer@nuxeo.com>
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.UnsupportedEncodingException;
024import java.net.URLDecoder;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import javax.mail.MessagingException;
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033import javax.ws.rs.DELETE;
034import javax.ws.rs.GET;
035import javax.ws.rs.POST;
036import javax.ws.rs.Path;
037import javax.ws.rs.PathParam;
038import javax.ws.rs.Produces;
039import javax.ws.rs.core.Context;
040import javax.ws.rs.core.MediaType;
041import javax.ws.rs.core.Response;
042import javax.ws.rs.core.Response.Status;
043import javax.ws.rs.core.Response.Status.Family;
044import javax.ws.rs.core.Response.StatusType;
045
046import org.apache.commons.collections.CollectionUtils;
047import org.apache.commons.lang.StringUtils;
048import org.apache.commons.lang.math.NumberUtils;
049import org.apache.commons.logging.Log;
050import org.apache.commons.logging.LogFactory;
051import org.codehaus.jackson.map.ObjectMapper;
052import org.nuxeo.ecm.automation.OperationContext;
053import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest;
054import org.nuxeo.ecm.automation.server.jaxrs.ResponseHelper;
055import org.nuxeo.ecm.automation.server.jaxrs.batch.BatchFileEntry;
056import org.nuxeo.ecm.automation.server.jaxrs.batch.BatchManager;
057import org.nuxeo.ecm.automation.server.jaxrs.batch.BatchManagerConstants;
058import org.nuxeo.ecm.core.api.Blob;
059import org.nuxeo.ecm.core.api.CoreSession;
060import org.nuxeo.ecm.core.api.NuxeoException;
061import org.nuxeo.ecm.webengine.WebException;
062import org.nuxeo.ecm.webengine.forms.FormData;
063import org.nuxeo.ecm.webengine.jaxrs.context.RequestContext;
064import org.nuxeo.ecm.webengine.model.WebObject;
065import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
066import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
067import org.nuxeo.runtime.api.Framework;
068import org.nuxeo.runtime.transaction.TransactionHelper;
069
070/**
071 * Batch upload endpoint.
072 * <p>
073 * Replaces the deprecated endpoints listed below:
074 * <ul>
075 * <li>POST /batch/upload, see org.nuxeo.ecm.automation.server.jaxrs.batch.BatchResource#doPost(HttpServletRequest), use
076 * POST /upload/{batchId}/{fileIdx} instead, see {@link #upload(HttpServletRequest, String, String)}</li>
077 * <li>GET /batch/files/{batchId}, see org.nuxeo.ecm.automation.server.jaxrs.batch.BatchResource#getFilesBatch(String),
078 * use GET /upload/{batchId} instead, see {@link #getBatchInfo(String)} instead</li>
079 * <li>GET /batch/drop/{batchId}, see org.nuxeo.ecm.automation.server.jaxrs.batch.BatchResource#dropBatch(String), use
080 * DELETE /upload/{batchId} instead, see {@link #dropBatch(String)}</li>
081 * </ul>
082 * Also provides new endpoints:
083 * <ul>
084 * <li>POST /upload, see {@link #initBatch()}</li>
085 * <li>GET /upload/{batchId}/{fileIdx}, see {@link #getFileInfo(String, String)}</li>
086 * </ul>
087 * Largely inspired by the excellent Google Drive REST API documentation about <a
088 * href="https://developers.google.com/drive/web/manage-uploads#resumable">resumable upload</a>.
089 *
090 * @since 7.4
091 */
092@WebObject(type = "upload")
093public class BatchUploadObject extends AbstractResource<ResourceTypeImpl> {
094
095    protected static final Log log = LogFactory.getLog(BatchUploadObject.class);
096
097    protected static final String REQUEST_BATCH_ID = "batchId";
098
099    protected static final String REQUEST_FILE_IDX = "fileIdx";
100
101    protected static final String OPERATION_ID = "operationId";
102
103    public static final String UPLOAD_TYPE_NORMAL = "normal";
104
105    public static final String UPLOAD_TYPE_CHUNKED = "chunked";
106
107    @POST
108    public Response initBatch() throws IOException {
109        BatchManager bm = Framework.getService(BatchManager.class);
110        String batchId = bm.initBatch();
111        Map<String, String> result = new HashMap<String, String>();
112        result.put("batchId", batchId);
113        return buildResponse(Status.CREATED, result);
114    }
115
116    @POST
117    @Path("{batchId}/{fileIdx}")
118    public Response upload(@Context HttpServletRequest request, @PathParam(REQUEST_BATCH_ID) String batchId,
119            @PathParam(REQUEST_FILE_IDX) String fileIdx) throws IOException {
120        TransactionHelper.commitOrRollbackTransaction();
121        try {
122            return uploadNoTransaction(request, batchId, fileIdx);
123        } finally {
124            TransactionHelper.startTransaction();
125        }
126    }
127
128    protected Response uploadNoTransaction(@Context HttpServletRequest request,
129            @PathParam(REQUEST_BATCH_ID) String batchId, @PathParam(REQUEST_FILE_IDX) String fileIdx)
130            throws IOException {
131
132        BatchManager bm = Framework.getService(BatchManager.class);
133        if (!bm.hasBatch(batchId)) {
134            return buildEmptyResponse(Status.NOT_FOUND);
135        }
136
137        // Check file index parameter
138        if (!NumberUtils.isDigits(fileIdx)) {
139            return buildTextResponse(Status.BAD_REQUEST, "fileIdx request path parameter must be a number");
140        }
141
142        boolean isMultipart = false;
143
144        // Parameters are passed as request header, the request body is the stream
145        String contentType = request.getHeader("Content-Type");
146        String uploadType = request.getHeader("X-Upload-Type");
147        String contentLength = request.getHeader("Content-Length");
148        String uploadChunkIndex = request.getHeader("X-Upload-Chunk-Index");
149        String chunkCount = request.getHeader("X-Upload-Chunk-Count");
150        String fileName = request.getHeader("X-File-Name");
151        String fileSize = request.getHeader("X-File-Size");
152        String mimeType = request.getHeader("X-File-Type");
153        InputStream is = null;
154
155        BatchFileEntry fileEntry = null;
156        String uploadedSize = "0";
157        long contentLengthAsLong = -1;
158        if (contentLength != null) {
159            contentLengthAsLong = Long.valueOf(contentLength);
160        }
161        if (contentLengthAsLong > -1) {
162            uploadedSize = contentLength;
163            // Handle multipart case: mainly MSIE with jQueryFileupload
164            if (contentType != null && contentType.contains("multipart")) {
165                isMultipart = true;
166                FormData formData = new FormData(request);
167                Blob blob = formData.getFirstBlob();
168                if (blob != null) {
169                    is = blob.getStream();
170                    if (!UPLOAD_TYPE_CHUNKED.equals(uploadType)) {
171                        fileName = blob.getFilename();
172                    }
173                    mimeType = blob.getMimeType();
174                    uploadedSize = String.valueOf(blob.getLength());
175                }
176            } else {
177                if (fileName != null) {
178                    fileName = URLDecoder.decode(fileName, "UTF-8");
179                }
180                is = request.getInputStream();
181            }
182
183            if (UPLOAD_TYPE_CHUNKED.equals(uploadType)) {
184                try {
185                    log.debug(String.format("Uploading chunk [index=%s / total=%s] (%sb) for file %s",
186                            uploadChunkIndex, chunkCount, uploadedSize, fileName));
187                    bm.addStream(batchId, fileIdx, is, Integer.parseInt(chunkCount),
188                            Integer.parseInt(uploadChunkIndex), fileName, mimeType, Long.parseLong(fileSize));
189                } catch (NumberFormatException e) {
190                    return buildTextResponse(Status.BAD_REQUEST,
191                            "X-Upload-Chunk-Index, X-Upload-Chunk-Count and X-File-Size headers must be numbers");
192                }
193            } else {
194                // Use non chunked mode by default if X-Upload-Type header is not provided
195                uploadType = UPLOAD_TYPE_NORMAL;
196                log.debug(String.format("Uploading file %s (%sb)", fileName, uploadedSize));
197                bm.addStream(batchId, fileIdx, is, fileName, mimeType);
198            }
199        } else {
200            fileEntry = bm.getFileEntry(batchId, fileIdx);
201            if (fileEntry == null) {
202                return buildEmptyResponse(Status.NOT_FOUND);
203            }
204        }
205
206        if (fileEntry == null && UPLOAD_TYPE_CHUNKED.equals(uploadType)) {
207            fileEntry = bm.getFileEntry(batchId, fileIdx);
208        }
209
210        StatusType status = Status.CREATED;
211        Map<String, Object> result = new HashMap<>();
212        result.put("uploaded", "true");
213        result.put("batchId", batchId);
214        result.put("fileIdx", fileIdx);
215        result.put("uploadedSize", uploadedSize);
216        if (fileEntry != null && fileEntry.isChunked()) {
217            result.put("uploadType", UPLOAD_TYPE_CHUNKED);
218            result.put("uploadedChunkIds", fileEntry.getOrderedChunkIndexes());
219            result.put("chunkCount", fileEntry.getChunkCount());
220            if (!fileEntry.isChunksCompleted()) {
221                status = new ResumeIncompleteStatusType();
222            }
223        } else {
224            result.put("uploadType", UPLOAD_TYPE_NORMAL);
225        }
226        return buildResponse(status, result, isMultipart);
227    }
228
229    @GET
230    @Path("{batchId}")
231    public Response getBatchInfo(@PathParam(REQUEST_BATCH_ID) String batchId) throws IOException {
232        BatchManager bm = Framework.getService(BatchManager.class);
233        if (!bm.hasBatch(batchId)) {
234            return buildEmptyResponse(Status.NOT_FOUND);
235        }
236        List<BatchFileEntry> fileEntries = bm.getFileEntries(batchId);
237        if (CollectionUtils.isEmpty(fileEntries)) {
238            return buildEmptyResponse(Status.NO_CONTENT);
239        }
240        List<Map<String, Object>> result = new ArrayList<>();
241        for (BatchFileEntry fileEntry : fileEntries) {
242            result.add(getFileInfo(fileEntry));
243        }
244        return buildResponse(Status.OK, result);
245    }
246
247    @GET
248    @Path("{batchId}/{fileIdx}")
249    public Response getFileInfo(@PathParam(REQUEST_BATCH_ID) String batchId, @PathParam(REQUEST_FILE_IDX) String fileIdx)
250            throws IOException {
251        BatchManager bm = Framework.getService(BatchManager.class);
252        if (!bm.hasBatch(batchId)) {
253            return buildEmptyResponse(Status.NOT_FOUND);
254        }
255        BatchFileEntry fileEntry = bm.getFileEntry(batchId, fileIdx);
256        if (fileEntry == null) {
257            return buildEmptyResponse(Status.NOT_FOUND);
258        }
259        StatusType status = Status.OK;
260        if (fileEntry.isChunked() && !fileEntry.isChunksCompleted()) {
261            status = new ResumeIncompleteStatusType();
262        }
263        Map<String, Object> result = getFileInfo(fileEntry);
264        return buildResponse(status, result);
265    }
266
267    @DELETE
268    @Path("{batchId}")
269    public Response cancel(@PathParam(REQUEST_BATCH_ID) String batchId) throws IOException {
270        BatchManager bm = Framework.getLocalService(BatchManager.class);
271        if (!bm.hasBatch(batchId)) {
272            return buildEmptyResponse(Status.NOT_FOUND);
273        }
274        bm.clean(batchId);
275        return buildEmptyResponse(Status.NO_CONTENT);
276    }
277
278    /**
279     * @since 8.4
280     */
281    @DELETE
282    @Path("{batchId}/{fileIdx}")
283    public Response removeFile(@PathParam(REQUEST_BATCH_ID) String batchId, @PathParam(REQUEST_FILE_IDX) String fileIdx)
284        throws IOException {
285        BatchManager bm = Framework.getLocalService(BatchManager.class);
286        if (!bm.removeFileEntry(batchId, fileIdx)) {
287            return buildEmptyResponse(Status.NOT_FOUND);
288        }
289        return buildEmptyResponse(Status.NO_CONTENT);
290    }
291
292    @Context
293   protected  HttpServletRequest request;
294
295    @Context
296    protected HttpServletResponse response;
297
298    @POST
299    @Produces("application/json")
300    @Path("{batchId}/execute/{operationId}")
301    public Object execute(@PathParam(REQUEST_BATCH_ID) String batchId, @PathParam(OPERATION_ID) String operationId,
302            ExecutionRequest xreq) throws UnsupportedEncodingException {
303        return executeBatch(batchId, null, operationId, request, xreq);
304    }
305
306    @POST
307    @Produces("application/json")
308    @Path("{batchId}/{fileIdx}/execute/{operationId}")
309    public Object execute(@PathParam(REQUEST_BATCH_ID) String batchId, @PathParam(REQUEST_FILE_IDX) String fileIdx,
310            @PathParam(OPERATION_ID) String operationId, ExecutionRequest xreq)
311            throws UnsupportedEncodingException {
312        return executeBatch(batchId, fileIdx, operationId, request, xreq);
313    }
314
315    protected Object executeBatch(String batchId, String fileIdx, String operationId, HttpServletRequest request,
316            ExecutionRequest xreq) throws UnsupportedEncodingException {
317
318        if (!Boolean.parseBoolean(
319            RequestContext.getActiveContext(request).getRequest().getHeader(BatchManagerConstants.NO_DROP_FLAG))) {
320            RequestContext.getActiveContext(request).addRequestCleanupHandler(req -> {
321                BatchManager bm = Framework.getService(BatchManager.class);
322                bm.clean(batchId);
323            });
324        }
325
326        try {
327            CoreSession session = ctx.getCoreSession();
328            OperationContext ctx = xreq.createContext(request, response, session);
329            Map<String, Object> params = xreq.getParams();
330            BatchManager bm = Framework.getLocalService(BatchManager.class);
331            Object result;
332            if (StringUtils.isBlank(fileIdx)) {
333                result = bm.execute(batchId, operationId, session, ctx, params);
334            } else {
335                result = bm.execute(batchId, fileIdx, operationId, session, ctx, params);
336            }
337            return ResponseHelper.getResponse(result, request);
338        } catch (NuxeoException | MessagingException | IOException e) {
339            log.error("Error while executing automation batch ", e);
340            if (WebException.isSecurityError(e)) {
341                return buildJSONResponse(Status.FORBIDDEN, "{\"error\" : \"" + e.getMessage() + "\"}");
342            } else {
343                return buildJSONResponse(Status.INTERNAL_SERVER_ERROR, "{\"error\" : \"" + e.getMessage() + "\"}");
344            }
345        }
346    }
347
348    protected Response buildResponse(StatusType status, Object object) throws IOException {
349        return buildResponse(status, object, false);
350    }
351
352    protected Response buildResponse(StatusType status, Object object, boolean html) throws IOException {
353        ObjectMapper mapper = new ObjectMapper();
354        String result = mapper.writeValueAsString(object);
355        if (html) {
356            // For MSIE with iframe transport: we need to return HTML!
357            return buildHTMLResponse(status, result);
358        } else {
359            return buildJSONResponse(status, result);
360        }
361    }
362
363    protected Response buildJSONResponse(StatusType status, String message) throws UnsupportedEncodingException {
364        return buildResponse(status, MediaType.APPLICATION_JSON, message);
365    }
366
367    protected Response buildHTMLResponse(StatusType status, String message) throws UnsupportedEncodingException {
368        message = "<html>" + message + "</html>";
369        return buildResponse(status, MediaType.TEXT_HTML, message);
370    }
371
372    protected Response buildTextResponse(StatusType status, String message) throws UnsupportedEncodingException {
373        return buildResponse(status, MediaType.TEXT_PLAIN, message);
374    }
375
376    protected Response buildEmptyResponse(StatusType status) {
377        return Response.status(status).build();
378    }
379
380    protected Response buildResponse(StatusType status, String type, String message)
381            throws UnsupportedEncodingException {
382        return Response.status(status)
383                       .header("Content-Length", message.getBytes("UTF-8").length)
384                       .type(type + "; charset=UTF-8")
385                       .entity(message)
386                       .build();
387    }
388
389    protected Map<String, Object> getFileInfo(BatchFileEntry fileEntry) throws UnsupportedEncodingException {
390        Map<String, Object> info = new HashMap<>();
391        boolean chunked = fileEntry.isChunked();
392        String uploadType;
393        if (chunked) {
394            uploadType = UPLOAD_TYPE_CHUNKED;
395        } else {
396            uploadType = UPLOAD_TYPE_NORMAL;
397        }
398        info.put("name", fileEntry.getFileName());
399        info.put("size", fileEntry.getFileSize());
400        info.put("uploadType", uploadType);
401        if (chunked) {
402            info.put("uploadedChunkIds", fileEntry.getOrderedChunkIndexes());
403            info.put("chunkCount", fileEntry.getChunkCount());
404        }
405        return info;
406    }
407
408    public final class ResumeIncompleteStatusType implements StatusType {
409
410        @Override
411        public int getStatusCode() {
412            return 308;
413        }
414
415        @Override
416        public String getReasonPhrase() {
417            return "Resume Incomplete";
418        }
419
420        @Override
421        public Family getFamily() {
422            // Technically we don't use 308 Resume Incomplete as a redirection but it is the default family for 3xx
423            // status codes defined by Response$Status
424            return Family.REDIRECTION;
425        }
426    }
427
428}