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.rendition;
019
020import javax.ws.rs.GET;
021import javax.ws.rs.Path;
022import javax.ws.rs.PathParam;
023import javax.ws.rs.core.Context;
024import javax.ws.rs.core.Request;
025
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.platform.rendition.Rendition;
029import org.nuxeo.ecm.platform.rendition.service.RenditionService;
030import org.nuxeo.ecm.webengine.model.WebAdapter;
031import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
032import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @since 7.2
037 */
038@WebAdapter(name = RenditionAdapter.NAME, type = "renditionAdapter")
039public class RenditionAdapter extends DefaultAdapter {
040
041    public static final String NAME = "rendition";
042
043    @GET
044    @Path("{renditionName}")
045    public Object doGetRendition(@Context Request request, @PathParam("renditionName") String renditionName) {
046        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
047        RenditionService renditionService = Framework.getService(RenditionService.class);
048
049        Rendition rendition = renditionService.getRendition(doc, renditionName);
050        if (rendition == null) {
051            throw new WebResourceNotFoundException(String.format("No rendition '%s' was found", renditionName));
052        }
053
054        Blob blob = rendition.getBlob();
055        if (blob == null) {
056            throw new WebResourceNotFoundException(String.format("No Blob was found for rendition '%s'", renditionName));
057        }
058
059        return blob;
060    }
061}