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