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 javax.ws.rs.GET;
023import javax.ws.rs.Path;
024import javax.ws.rs.PathParam;
025import javax.ws.rs.core.Response;
026
027import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
028import org.nuxeo.ecm.core.convert.api.ConversionService;
029import org.nuxeo.ecm.core.convert.api.ConversionStatus;
030import org.nuxeo.ecm.restapi.jaxrs.io.conversion.ConversionStatusWithResult;
031import org.nuxeo.ecm.webengine.model.WebObject;
032import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @since 7.4
038 */
039@WebObject(type = "conversions")
040public class ConversionRootObject extends DefaultObject {
041
042    @GET
043    @Path("{id}/poll")
044    public Object doGetConversionStatus(@PathParam("id") String id) {
045        ConversionService conversionService = Framework.getService(ConversionService.class);
046        ConversionStatus conversionStatus = conversionService.getConversionStatus(id);
047        if (conversionStatus == null) {
048            throw new WebResourceNotFoundException("No conversion job for id: " + id);
049        }
050
051        String serverURL = ctx.getServerURL().toString().replaceAll("/$", "");
052        String resultURL = String.format("%s%s/conversions/%s/result", serverURL, ctx.getModulePath(), conversionStatus.id);
053        ConversionStatusWithResult conversionStatusWithResult = new ConversionStatusWithResult(conversionStatus, resultURL);
054        return Response.ok(conversionStatusWithResult).build();
055    }
056
057    @GET
058    @Path("{id}/result")
059    public Object doGetConversionResult(@PathParam("id") String id) {
060        ConversionService conversionService = Framework.getService(ConversionService.class);
061        BlobHolder result = conversionService.getConversionResult(id, false);
062        if (result == null || result.getBlob() == null) {
063            throw new WebResourceNotFoundException("No conversion result for id: " + id);
064        }
065
066        return result.getBlob();
067    }
068
069}