001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.wro.provider;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.web.resources.api.Resource;
025import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
026import org.nuxeo.runtime.api.Framework;
027
028import ro.isdc.wro.model.group.Inject;
029import ro.isdc.wro.model.resource.locator.UriLocator;
030import ro.isdc.wro.model.resource.locator.factory.UriLocatorFactory;
031
032/**
033 * Nuxeo URI locator, made available to all wro resources thanks to {@link NuxeoConfigurableProvider}.
034 *
035 * @since 7.3
036 */
037public class NuxeoUriLocator implements UriLocator {
038
039    private static final Log log = LogFactory.getLog(NuxeoUriLocator.class);
040
041    public static final String ALIAS = "nuxeoUri";
042
043    @Inject
044    UriLocatorFactory uriLocatorFactory;
045
046    @Override
047    public boolean accept(String uri) {
048        return uri != null && uri.startsWith(Resource.PREFIX);
049    }
050
051    @Override
052    public InputStream locate(String uri) throws IOException {
053        Resource resource = getResource(uri);
054        if (resource != null) {
055            String ruri = resource.getURI();
056            if (ruri == null) {
057                log.error(String.format("Cannot handle resource '%s': no resolved uri", resource.getName()));
058                return null;
059            }
060            final UriLocator uriLocator = uriLocatorFactory.getInstance(ruri);
061            if (uriLocator != null) {
062                return uriLocator.locate(ruri);
063            }
064        }
065        return null;
066    }
067
068    public static Resource getResource(String uri) {
069        // resolve resource from Nuxeo service
070        String name = uri.substring(Resource.PREFIX.length());
071        WebResourceManager service = Framework.getService(WebResourceManager.class);
072        return service.getResource(name);
073    }
074
075    public static String getUri(Resource resource) {
076        return String.format("%s%s", Resource.PREFIX, resource.getName());
077    }
078
079    public static boolean isProcessorEnabled(String alias, String uri) {
080        Resource res = getResource(uri);
081        if (res != null) {
082            return res.getProcessors().contains(alias);
083        }
084        return false;
085    }
086
087}