001/*
002 * (C) Copyright 2015 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.restapi.server.jaxrs.conversion;
021
022import java.net.URI;
023import java.net.URISyntaxException;
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}