/** * This file Copyright (c) 2014 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.dam.core.util; import com.google.common.net.MediaType; import info.magnolia.dam.api.Asset; import info.magnolia.dam.api.AssetRenderer; import info.magnolia.dam.api.AssetRendition; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * An {@link AssetRenderer} which does nothing to the Asset (i.e returns an {@link AssetRendition} which just delegates * to the original asset, but can configured to support only certain mimetypes. * It can thus be configured to "bypass" other renderers for certain types. */ public class NoOpAssetRenderer implements AssetRenderer { private List mediaTypes = new ArrayList(); private List outputMediaTypes = new ArrayList(); @Override public boolean supports(MediaType from, MediaType to) { return mediaTypes.contains(from) || outputMediaTypes.contains(to); } @Override public boolean canRender(Asset asset, MediaType to) { return supports(MediaType.parse(asset.getMimeType()), to); } @Override public AssetRendition render(Asset asset, MediaType to, String renditionName) { return new NoOpAssetRendition(asset); } // ----- Configuration methods ------ public List getMediaTypes() { return mediaTypes; } public void setMediaTypes(List mediaTypes) { this.mediaTypes = mediaTypes; } public List getOutputMediaTypes() { return outputMediaTypes; } public void setOutputMediaTypes(List outputMediaTypes) { this.outputMediaTypes = outputMediaTypes; } private static class NoOpAssetRendition implements AssetRendition { private final Asset asset; public NoOpAssetRendition(Asset asset) { this.asset = asset; } @Override public String getLink() { return asset.getLink(); } @Override public String getRenditionName() { return "no-op"; } @Override public String getMimeType() { return asset.getMimeType(); } @Override public InputStream getStream() { return asset.getContentStream(); } @Override public Asset getAsset() { return asset; } } }