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 blob(Blob blob) {
073        return blob(blob, HttpServletResponse.SC_OK);
074    }
075
076    public static Response blobs(List<Blob> blobs) throws MessagingException, IOException {
077        return blobs(blobs, HttpServletResponse.SC_OK);
078    }
079
080    public static Response blob(Blob blob, int httpStatus) {
081        String type = blob.getMimeType();
082        if (type == null || "???".equals(type)) {
083            type = MediaType.APPLICATION_OCTET_STREAM;
084        }
085        if (blob.getEncoding() != null) {
086            type += "; charset=" + blob.getEncoding();
087        }
088        return Response.status(httpStatus)
089                       .entity(blob)
090                       .type(type)
091                       .header("Content-Disposition", "attachment; filename=" + blob.getFilename())
092                       .build();
093    }
094
095    public static Response blobs(List<Blob> blobs, int httpStatus) throws MessagingException, IOException {
096        if (blobs.isEmpty()) {
097            return emptyBlobs();
098        }
099        MultipartBlobs multipartBlobs = new MultipartBlobs(blobs);
100        return Response.status(httpStatus)
101                       .entity(multipartBlobs)
102                       .type(new BoundaryMediaType(multipartBlobs.getContentType()))
103                       .build();
104    }
105
106    /**
107     * @since 5.7.2
108     */
109    public static Object getResponse(Object result, HttpServletRequest request) throws MessagingException, IOException {
110        return getResponse(result, request, HttpServletResponse.SC_OK);
111    }
112
113    /**
114     * Handle custom http status.
115     *
116     * @since 7.1
117     */
118    public static Object getResponse(Object result, HttpServletRequest request, int httpStatus)
119            throws IOException, MessagingException {
120        if (result == null || "true".equals(request.getHeader("X-NXVoidOperation"))) {
121            return emptyContent();
122        }
123        if (result instanceof Blob) {
124            return blob((Blob) result);
125        } else if (result instanceof BlobList) {
126            return blobs((BlobList) result);
127        } else if (result instanceof DocumentRef) {
128            CoreSession session = SessionFactory.getSession(request);
129            return Response.status(httpStatus).entity(session.getDocument((DocumentRef) result)).build();
130        } else if (result instanceof DocumentRefList) {
131            CoreSession session = SessionFactory.getSession(request);
132            return Response.status(httpStatus).entity(((DocumentRefList) result).stream().map(session::getDocument)
133                    .collect(Collectors.toCollection(DocumentModelListImpl::new))).build();
134        } else if (result instanceof DocumentModel || result instanceof DocumentModelList
135                || result instanceof JsonAdapter || result instanceof RecordSet || result instanceof Paginable<?>) {
136            return Response.status(httpStatus).entity(result).build();
137        } else { // try to adapt to JSON
138            return Response.status(httpStatus).entity(new DefaultJsonAdapter(result)).build();
139        }
140    }
141
142    /**
143     * @since 7.1
144     */
145    public static class BoundaryMediaType extends MediaType {
146        private final String ctype;
147
148        BoundaryMediaType(String ctype) {
149            super("multipart", "mixed");
150            this.ctype = ctype;
151        }
152
153        @Override
154        public String toString() {
155            return ctype;
156        }
157    }
158}