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