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