001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.restapi.server.jaxrs.conversion;
019
020import java.net.URI;
021import java.net.URISyntaxException;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.core.Response;
029
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
032import org.nuxeo.ecm.core.convert.api.ConversionService;
033import org.nuxeo.ecm.core.convert.api.ConversionStatus;
034import org.nuxeo.ecm.webengine.model.WebObject;
035import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
036import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
037import org.nuxeo.runtime.api.Framework;
038
039/**
040 * @since 7.4
041 */
042@WebObject(type = "conversions")
043public class ConversionRootObject extends DefaultObject {
044
045    @GET
046    @Path("{id}/poll")
047    public Object doGetConversionStatus(@PathParam("id") String id) {
048        ConversionService conversionService = Framework.getService(ConversionService.class);
049        ConversionStatus conversionStatus = conversionService.getConversionStatus(id);
050        if (conversionStatus == null) {
051            throw new WebResourceNotFoundException("No conversion job for id: " + id);
052        }
053
054        // conversion finished
055        if (conversionStatus.status == ConversionStatus.Status.COMPLETED) {
056            String serverURL = ctx.getServerURL().toString();
057            if (serverURL.endsWith("/")) {
058                serverURL = serverURL.substring(0, serverURL.length() - 1);
059            }
060            String url = String.format("%s%s/conversions/%s/result", serverURL, ctx.getModulePath(), id);
061            try {
062                return Response.seeOther(new URI(url)).build();
063            } catch (URISyntaxException e) {
064                throw new NuxeoException(e);
065            }
066        }
067
068        return Response.ok(conversionStatus).build();
069    }
070
071    @GET
072    @Path("{id}/result")
073    public Object doGetConversionResult(@PathParam("id") String id) {
074        ConversionService conversionService = Framework.getService(ConversionService.class);
075        BlobHolder result = conversionService.getConversionResult(id, false);
076        if (result == null || result.getBlob() == null) {
077            throw new WebResourceNotFoundException("No conversion result for id: " + id);
078        }
079
080        return result.getBlob();
081    }
082
083}