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.GenericEntity;
030import javax.ws.rs.core.MediaType;
031import javax.ws.rs.core.Response;
032
033import org.apache.commons.lang3.reflect.TypeUtils;
034import org.nuxeo.ecm.automation.core.util.BlobList;
035import org.nuxeo.ecm.automation.core.util.Paginable;
036import org.nuxeo.ecm.automation.core.util.RecordSet;
037import org.nuxeo.ecm.automation.jaxrs.DefaultJsonAdapter;
038import org.nuxeo.ecm.automation.jaxrs.JsonAdapter;
039import org.nuxeo.ecm.automation.jaxrs.io.documents.MultipartBlobs;
040import org.nuxeo.ecm.core.api.Blob;
041import org.nuxeo.ecm.core.api.CoreSession;
042import org.nuxeo.ecm.core.api.DocumentModel;
043import org.nuxeo.ecm.core.api.DocumentModelList;
044import org.nuxeo.ecm.core.api.DocumentRef;
045import org.nuxeo.ecm.core.api.DocumentRefList;
046import org.nuxeo.ecm.core.api.NuxeoPrincipal;
047import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
048import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
049
050/**
051 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
052 * @author <a href="mailto:ataillefer@nuxeo.com">Antoine Taillefer</a>
053 */
054public class ResponseHelper {
055
056    private ResponseHelper() {
057    }
058
059    public static Response notFound() {
060        return Response.status(404).build();
061    }
062
063    public static Response emptyContent() {
064        return Response.status(204).build();
065    }
066
067    public static Response emptyBlobs() {
068        return Response.status(204).type("application/nuxeo-empty-list").build();
069    }
070
071    public static Response notAllowed() {
072        return Response.status(401).build();
073    }
074
075    public static Response blobs(List<Blob> blobs) throws MessagingException, IOException {
076        return blobs(blobs, HttpServletResponse.SC_OK);
077    }
078
079    public static Response blobs(List<Blob> blobs, int httpStatus) throws MessagingException, IOException {
080        if (blobs.isEmpty()) {
081            return emptyBlobs();
082        }
083        MultipartBlobs multipartBlobs = new MultipartBlobs(blobs);
084        return Response.status(httpStatus)
085                       .entity(multipartBlobs)
086                       .type(new BoundaryMediaType(multipartBlobs.getContentType()))
087                       .build();
088    }
089
090    /**
091     * @since 5.7.2
092     */
093    public static Object getResponse(Object result, HttpServletRequest request) throws MessagingException, IOException {
094        return getResponse(result, request, HttpServletResponse.SC_OK);
095    }
096
097    /**
098     * Handle custom http status.
099     *
100     * @since 7.1
101     */
102    public static Object getResponse(Object result, HttpServletRequest request, int httpStatus)
103            throws IOException, MessagingException {
104        if (result == null || "true".equals(request.getHeader("X-NXVoidOperation"))) {
105            return emptyContent();
106        }
107        if (result instanceof Blob) {
108            return result; // BlobWriter will do all the processing and call the DownloadService
109        } else if (result instanceof BlobList) {
110            return blobs((BlobList) result);
111        } else if (result instanceof DocumentRef) {
112            CoreSession session = SessionFactory.getSession(request);
113            return Response.status(httpStatus).entity(session.getDocument((DocumentRef) result)).build();
114        } else if (result instanceof DocumentRefList) {
115            CoreSession session = SessionFactory.getSession(request);
116            return Response.status(httpStatus).entity(((DocumentRefList) result).stream().map(session::getDocument)
117                    .collect(Collectors.toCollection(DocumentModelListImpl::new))).build();
118        } else if (result instanceof List && !((List<?>) result).isEmpty()
119                && ((List<?>) result).get(0) instanceof NuxeoPrincipal) {
120            return Response.status(httpStatus)
121                           .entity(new GenericEntity<>(result,
122                                   TypeUtils.parameterize(List.class, NuxeoPrincipal.class)))
123                           .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}