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