001/*
002 * (C) Copyright 2014 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 notAllowed() {
062        return Response.status(401).build();
063    }
064
065    public static Response blob(Blob blob) {
066        return blob(blob, HttpServletResponse.SC_OK);
067    }
068
069    public static Response blobs(List<Blob> blobs) throws MessagingException, IOException {
070        return blobs(blobs, HttpServletResponse.SC_OK);
071    }
072
073    public static Response blob(Blob blob, int httpStatus) {
074        String type = blob.getMimeType();
075        if (type == null || "???".equals(type)) {
076            type = MediaType.APPLICATION_OCTET_STREAM;
077        }
078        return Response.status(httpStatus).entity(blob).type(type).header("Content-Disposition",
079                "attachment; filename=" + blob.getFilename()).build();
080    }
081
082    public static Response blobs(List<Blob> blobs, int httpStatus) throws MessagingException, IOException {
083        MultipartBlobs multipartBlobs = new MultipartBlobs(blobs);
084        return Response.status(httpStatus).entity(multipartBlobs).type(
085                new BoundaryMediaType(multipartBlobs.getContentType())).build();
086    }
087
088    /**
089     * @since 5.7.2
090     */
091    public static Object getResponse(Object result, HttpServletRequest request) throws MessagingException, IOException {
092        return getResponse(result, request, HttpServletResponse.SC_OK);
093    }
094
095    /**
096     * Handle custom http status.
097     *
098     * @since 7.1
099     */
100    public static Object getResponse(Object result, HttpServletRequest request, int httpStatus) throws IOException,
101            MessagingException {
102        if (result == null || "true".equals(request.getHeader("X-NXVoidOperation"))) {
103            return emptyContent();
104        }
105        if (result instanceof Blob) {
106            return blob((Blob) result);
107        } else if (result instanceof BlobList) {
108            return blobs((BlobList) result);
109        } else if (result instanceof DocumentRef) {
110            CoreSession session = SessionFactory.getSession(request);
111            return Response.status(httpStatus).entity(session.getDocument((DocumentRef) result)).build();
112        } else if (result instanceof DocumentModel || result instanceof DocumentModelList
113                || result instanceof JsonAdapter || result instanceof RecordSet || result instanceof Paginable<?>) {
114            return Response.status(httpStatus).entity(result).build();
115        } else { // try to adapt to JSON
116            return Response.status(httpStatus).entity(new DefaultJsonAdapter(result)).build();
117        }
118    }
119
120    /**
121     * @since 7.1
122     */
123    public static class BoundaryMediaType extends MediaType {
124        private final String ctype;
125
126        BoundaryMediaType(String ctype) {
127            super("multipart", "mixed");
128            this.ctype = ctype;
129        }
130
131        @Override
132        public String toString() {
133            return ctype;
134        }
135    }
136}