001/* 002 * (C) Copyright 2014-2016 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 * Bogdan Stefanescu 018 * Antoine Taillefer 019 */ 020package org.nuxeo.ecm.automation.server.jaxrs; 021 022import java.io.IOException; 023import java.util.List; 024import java.util.stream.Collectors; 025 026import javax.mail.MessagingException; 027import javax.servlet.http.HttpServletRequest; 028import javax.servlet.http.HttpServletResponse; 029import javax.ws.rs.core.MediaType; 030import javax.ws.rs.core.Response; 031 032import org.nuxeo.ecm.automation.core.util.BlobList; 033import org.nuxeo.ecm.automation.core.util.Paginable; 034import org.nuxeo.ecm.automation.core.util.RecordSet; 035import org.nuxeo.ecm.automation.jaxrs.DefaultJsonAdapter; 036import org.nuxeo.ecm.automation.jaxrs.JsonAdapter; 037import org.nuxeo.ecm.automation.jaxrs.io.documents.MultipartBlobs; 038import org.nuxeo.ecm.core.api.Blob; 039import org.nuxeo.ecm.core.api.CoreSession; 040import org.nuxeo.ecm.core.api.DocumentModel; 041import org.nuxeo.ecm.core.api.DocumentModelList; 042import org.nuxeo.ecm.core.api.DocumentRef; 043import org.nuxeo.ecm.core.api.DocumentRefList; 044import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 045import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory; 046 047/** 048 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 049 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 050 */ 051public class ResponseHelper { 052 053 private ResponseHelper() { 054 } 055 056 public static Response notFound() { 057 return Response.status(404).build(); 058 } 059 060 public static Response emptyContent() { 061 return Response.status(204).build(); 062 } 063 064 public static Response emptyBlobs() { 065 return Response.status(204).type("application/nuxeo-empty-list").build(); 066 } 067 068 public static Response notAllowed() { 069 return Response.status(401).build(); 070 } 071 072 public static Response blobs(List<Blob> blobs) throws MessagingException, IOException { 073 return blobs(blobs, HttpServletResponse.SC_OK); 074 } 075 076 public static Response blobs(List<Blob> blobs, int httpStatus) throws MessagingException, IOException { 077 if (blobs.isEmpty()) { 078 return emptyBlobs(); 079 } 080 MultipartBlobs multipartBlobs = new MultipartBlobs(blobs); 081 return Response.status(httpStatus) 082 .entity(multipartBlobs) 083 .type(new BoundaryMediaType(multipartBlobs.getContentType())) 084 .build(); 085 } 086 087 /** 088 * @since 5.7.2 089 */ 090 public static Object getResponse(Object result, HttpServletRequest request) throws MessagingException, IOException { 091 return getResponse(result, request, HttpServletResponse.SC_OK); 092 } 093 094 /** 095 * Handle custom http status. 096 * 097 * @since 7.1 098 */ 099 public static Object getResponse(Object result, HttpServletRequest request, int httpStatus) 100 throws IOException, MessagingException { 101 if (result == null || "true".equals(request.getHeader("X-NXVoidOperation"))) { 102 return emptyContent(); 103 } 104 if (result instanceof Blob) { 105 return result; // BlobWriter will do all the processing and call the DownloadService 106 } else if (result instanceof BlobList) { 107 return blobs((BlobList) result); 108 } else if (result instanceof DocumentRef) { 109 CoreSession session = SessionFactory.getSession(request); 110 return Response.status(httpStatus).entity(session.getDocument((DocumentRef) result)).build(); 111 } else if (result instanceof DocumentRefList) { 112 CoreSession session = SessionFactory.getSession(request); 113 return Response.status(httpStatus).entity(((DocumentRefList) result).stream().map(session::getDocument) 114 .collect(Collectors.toCollection(DocumentModelListImpl::new))).build(); 115 } else if (result instanceof DocumentModel || result instanceof DocumentModelList 116 || result instanceof JsonAdapter || result instanceof RecordSet || result instanceof Paginable<?>) { 117 return Response.status(httpStatus).entity(result).build(); 118 } else { // try to adapt to JSON 119 return Response.status(httpStatus).entity(new DefaultJsonAdapter(result)).build(); 120 } 121 } 122 123 /** 124 * @since 7.1 125 */ 126 public static class BoundaryMediaType extends MediaType { 127 private final String ctype; 128 129 BoundaryMediaType(String ctype) { 130 super("multipart", "mixed"); 131 this.ctype = ctype; 132 } 133 134 @Override 135 public String toString() { 136 return ctype; 137 } 138 } 139}