001/* 002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Bogdan Stefanescu 016 * Antoine Taillefer 017 */ 018package org.nuxeo.ecm.automation.server.jaxrs; 019 020import java.io.IOException; 021import java.util.List; 022 023import javax.mail.MessagingException; 024import javax.servlet.http.HttpServletRequest; 025import javax.servlet.http.HttpServletResponse; 026import javax.ws.rs.core.MediaType; 027import javax.ws.rs.core.Response; 028 029import org.nuxeo.ecm.automation.core.util.BlobList; 030import org.nuxeo.ecm.automation.core.util.Paginable; 031import org.nuxeo.ecm.automation.core.util.RecordSet; 032import org.nuxeo.ecm.automation.jaxrs.DefaultJsonAdapter; 033import org.nuxeo.ecm.automation.jaxrs.JsonAdapter; 034import org.nuxeo.ecm.automation.jaxrs.io.documents.MultipartBlobs; 035import org.nuxeo.ecm.core.api.Blob; 036import org.nuxeo.ecm.core.api.CoreSession; 037import org.nuxeo.ecm.core.api.DocumentModel; 038import org.nuxeo.ecm.core.api.DocumentModelList; 039import org.nuxeo.ecm.core.api.DocumentRef; 040import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory; 041 042/** 043 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 044 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a> 045 */ 046public class ResponseHelper { 047 048 private ResponseHelper() { 049 } 050 051 public static Response notFound() { 052 return Response.status(404).build(); 053 } 054 055 public static Response emptyContent() { 056 return Response.status(204).build(); 057 } 058 059 public static Response notAllowed() { 060 return Response.status(401).build(); 061 } 062 063 public static Response blob(Blob blob) { 064 return blob(blob, HttpServletResponse.SC_OK); 065 } 066 067 public static Response blobs(List<Blob> blobs) throws MessagingException, IOException { 068 return blobs(blobs, HttpServletResponse.SC_OK); 069 } 070 071 public static Response blob(Blob blob, int httpStatus) { 072 String type = blob.getMimeType(); 073 if (type == null || "???".equals(type)) { 074 type = MediaType.APPLICATION_OCTET_STREAM; 075 } 076 return Response.status(httpStatus).entity(blob).type(type).header("Content-Disposition", 077 "attachment; filename=" + blob.getFilename()).build(); 078 } 079 080 public static Response blobs(List<Blob> blobs, int httpStatus) throws MessagingException, IOException { 081 MultipartBlobs multipartBlobs = new MultipartBlobs(blobs); 082 return Response.status(httpStatus).entity(multipartBlobs).type( 083 new BoundaryMediaType(multipartBlobs.getContentType())).build(); 084 } 085 086 /** 087 * @since 5.7.2 088 */ 089 public static Object getResponse(Object result, HttpServletRequest request) throws MessagingException, IOException { 090 return getResponse(result, request, HttpServletResponse.SC_OK); 091 } 092 093 /** 094 * Handle custom http status. 095 * 096 * @since 7.1 097 */ 098 public static Object getResponse(Object result, HttpServletRequest request, int httpStatus) throws IOException, 099 MessagingException { 100 if (result == null || "true".equals(request.getHeader("X-NXVoidOperation"))) { 101 return emptyContent(); 102 } 103 if (result instanceof Blob) { 104 return blob((Blob) result); 105 } else if (result instanceof BlobList) { 106 return blobs((BlobList) result); 107 } else if (result instanceof DocumentRef) { 108 CoreSession session = SessionFactory.getSession(request); 109 return Response.status(httpStatus).entity(session.getDocument((DocumentRef) result)).build(); 110 } else if (result instanceof DocumentModel || result instanceof DocumentModelList 111 || result instanceof JsonAdapter || result instanceof RecordSet || result instanceof Paginable<?>) { 112 return Response.status(httpStatus).entity(result).build(); 113 } else { // try to adapt to JSON 114 return Response.status(httpStatus).entity(new DefaultJsonAdapter(result)).build(); 115 } 116 } 117 118 /** 119 * @since 7.1 120 */ 121 public static class BoundaryMediaType extends MediaType { 122 private final String ctype; 123 124 BoundaryMediaType(String ctype) { 125 super("multipart", "mixed"); 126 this.ctype = ctype; 127 } 128 129 @Override 130 public String toString() { 131 return ctype; 132 } 133 } 134}