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