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 static org.nuxeo.ecm.core.io.download.DownloadService.REQUEST_ATTR_DOWNLOAD_REASON; 024import static org.nuxeo.ecm.core.io.download.DownloadService.REQUEST_ATTR_DOWNLOAD_RENDITION; 025 026import javax.servlet.http.HttpServletRequest; 027import javax.ws.rs.GET; 028import javax.ws.rs.core.Context; 029import javax.ws.rs.core.Request; 030 031import org.nuxeo.ecm.core.api.Blob; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.platform.rendition.Rendition; 034import org.nuxeo.ecm.platform.rendition.service.RenditionService; 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 8.2 042 */ 043@WebObject(type = "rendition") 044public class RenditionObject extends DefaultObject { 045 046 @Context 047 protected HttpServletRequest servletRequest; 048 049 protected String renditionName; 050 051 protected DocumentModel doc; 052 053 @Override 054 protected void initialize(Object... args) { 055 assert args != null && args.length == 2; 056 doc = (DocumentModel) args[0]; 057 renditionName = (String) args[1]; 058 } 059 060 @Override 061 public <A> A getAdapter(Class<A> adapter) { 062 if (adapter.isAssignableFrom(Blob.class)) { 063 return adapter.cast(getRenditionBlob()); 064 } 065 return super.getAdapter(adapter); 066 } 067 068 protected Blob getRenditionBlob() { 069 RenditionService renditionService = Framework.getService(RenditionService.class); 070 Rendition rendition = renditionService.getRendition(doc, renditionName); 071 if (rendition == null) { 072 throw new WebResourceNotFoundException(String.format("No rendition '%s' was found", renditionName)); 073 } 074 return rendition.getBlob(); 075 } 076 077 @GET 078 public Object doGet(@Context Request request) { 079 Blob blob = getRenditionBlob(); 080 if (blob == null) { 081 throw new WebResourceNotFoundException( 082 String.format("No Blob was found for rendition '%s'", renditionName)); 083 } 084 servletRequest.setAttribute(REQUEST_ATTR_DOWNLOAD_REASON, "rendition"); 085 servletRequest.setAttribute(REQUEST_ATTR_DOWNLOAD_RENDITION, renditionName); 086 return blob; 087 } 088}