001/* 002 * (C) Copyright 2016 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 */ 020 021package org.nuxeo.ecm.restapi.server.jaxrs.rendition; 022 023import javax.ws.rs.GET; 024import javax.ws.rs.core.Context; 025import javax.ws.rs.core.Request; 026 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.DocumentModel; 029import org.nuxeo.ecm.platform.rendition.Rendition; 030import org.nuxeo.ecm.platform.rendition.service.RenditionService; 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 8.2 038 */ 039@WebObject(type = "rendition") 040public class RenditionObject extends DefaultObject { 041 042 protected String renditionName; 043 044 protected DocumentModel doc; 045 046 @Override 047 protected void initialize(Object... args) { 048 assert args != null && args.length == 2; 049 doc = (DocumentModel) args[0]; 050 renditionName = (String) args[1]; 051 } 052 053 @Override 054 public <A> A getAdapter(Class<A> adapter) { 055 if (adapter.isAssignableFrom(Blob.class)) { 056 return adapter.cast(getRenditionBlob()); 057 } 058 return super.getAdapter(adapter); 059 } 060 061 protected Blob getRenditionBlob() { 062 RenditionService renditionService = Framework.getService(RenditionService.class); 063 Rendition rendition = renditionService.getRendition(doc, renditionName); 064 if (rendition == null) { 065 throw new WebResourceNotFoundException(String.format("No rendition '%s' was found", renditionName)); 066 } 067 return rendition.getBlob(); 068 } 069 070 @GET 071 public Object doGet(@Context Request request) { 072 Blob blob = getRenditionBlob(); 073 if (blob == null) { 074 throw new WebResourceNotFoundException( 075 String.format("No Blob was found for rendition '%s'", renditionName)); 076 } 077 return blob; 078 } 079}