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.util.Collections;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.web.resources.api.Processor;
029import org.nuxeo.ecm.web.resources.api.service.WebResourceManager;
030import org.nuxeo.runtime.api.Framework;
031
032import ro.isdc.wro.model.resource.locator.UriLocator;
033import ro.isdc.wro.model.resource.processor.ResourcePostProcessor;
034import ro.isdc.wro.model.resource.processor.ResourcePreProcessor;
035import ro.isdc.wro.util.provider.ConfigurableProviderSupport;
036
037/**
038 * Nuxeo contributor to wro processors and locators, registered as a service.
039 *
040 * @since 7.3
041 */
042public class NuxeoConfigurableProvider extends ConfigurableProviderSupport {
043
044    private static final Log log = LogFactory.getLog(NuxeoConfigurableProvider.class);
045
046    public static final String PRE_TYPE = "wroPre";
047
048    public static final String POST_TYPE = "wroPost";
049
050    @Override
051    public Map<String, UriLocator> provideLocators() {
052        Map<String, UriLocator> map = new HashMap<>();
053        map.put(NuxeoUriLocator.ALIAS, new NuxeoUriLocator());
054        return map;
055    }
056
057    @Override
058    public Map<String, ResourcePreProcessor> providePreProcessors() {
059        Map<String, ResourcePreProcessor> map = new HashMap<>();
060        // extend with runtime service processors registration
061        List<? extends Processor> processors = resolveProcessors(PRE_TYPE);
062        for (Processor p : processors) {
063            Class<?> klass = p.getTargetProcessorClass();
064            if (klass == null) {
065                // assume alias references a native wro processor
066                continue;
067            }
068            ResourcePreProcessor proc;
069            try {
070                proc = (ResourcePreProcessor) klass.getDeclaredConstructor().newInstance();
071                map.put(p.getName(), proc);
072            } catch (ReflectiveOperationException e) {
073                log.error("Caught error when instanciating resource pre processor", e);
074                continue;
075            }
076        }
077        return map;
078    }
079
080    @Override
081    public Map<String, ResourcePostProcessor> providePostProcessors() {
082        Map<String, ResourcePostProcessor> map = new HashMap<>();
083        // extend with runtime service processors registration
084        List<Processor> processors = resolveProcessors(POST_TYPE);
085        for (Processor p : processors) {
086            Class<?> klass = p.getTargetProcessorClass();
087            if (klass == null) {
088                // assume alias references a native wro processor
089                continue;
090            }
091            ResourcePostProcessor proc;
092            try {
093                proc = (ResourcePostProcessor) klass.getDeclaredConstructor().newInstance();
094                map.put(p.getName(), proc);
095            } catch (ReflectiveOperationException e) {
096                log.error("Caught error when instanciating resource post processor", e);
097                continue;
098            }
099        }
100        return map;
101    }
102
103    public static List<Processor> resolveProcessors(String type) {
104        WebResourceManager service = Framework.getService(WebResourceManager.class);
105        List<Processor> processors = service.getProcessors(type);
106        if (processors != null) {
107            return processors;
108        }
109        return Collections.emptyList();
110    }
111
112}