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.factory; 020 021import java.util.List; 022 023import org.apache.commons.lang.StringUtils; 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.ResourceBundle; 028import org.nuxeo.ecm.web.resources.api.ResourceContextImpl; 029import org.nuxeo.ecm.web.resources.api.ResourceType; 030import org.nuxeo.ecm.web.resources.api.service.WebResourceManager; 031import org.nuxeo.ecm.web.resources.wro.provider.NuxeoUriLocator; 032import org.nuxeo.runtime.api.Framework; 033 034import ro.isdc.wro.model.WroModel; 035import ro.isdc.wro.model.factory.WroModelFactory; 036import ro.isdc.wro.model.group.Group; 037 038/** 039 * Generates a {@link WroModel} using contributions to the {@link WebResourceManager} service. 040 * <p> 041 * Maps {@link ResourceBundle} elements to wro groups, and map their ordered resources (with resolved dependencies) to 042 * wro resources. 043 * 044 * @since 7.3 045 */ 046public class NuxeoWroModelFactory implements WroModelFactory { 047 048 private static final Log log = LogFactory.getLog(NuxeoWroModelFactory.class); 049 050 @Override 051 public WroModel create() { 052 WroModel model = new WroModel(); 053 WebResourceManager service = Framework.getService(WebResourceManager.class); 054 ResourceContextImpl rcontext = new ResourceContextImpl(); 055 List<ResourceBundle> bundles = service.getResourceBundles(); 056 for (ResourceBundle bundle : bundles) { 057 String groupName = bundle.getName(); 058 Group group = new Group(groupName); 059 List<Resource> resources = service.getResources(rcontext, groupName, ResourceType.any.name()); 060 if (resources != null) { 061 for (Resource resource : resources) { 062 ro.isdc.wro.model.resource.Resource wr = toWroResource(groupName, resource); 063 if (wr != null) { 064 group.addResource(wr); 065 } 066 } 067 } 068 model.addGroup(group); 069 } 070 return model; 071 } 072 073 protected ro.isdc.wro.model.resource.Resource toWroResource(String bundle, Resource resource) { 074 ro.isdc.wro.model.resource.ResourceType type = toWroResourceType(resource.getType()); 075 if (type == null) { 076 if (log.isDebugEnabled()) { 077 log.debug("Cannot handle resource type '" + resource.getType() + "' for resource '" + resource.getName() 078 + "'"); 079 } 080 return null; 081 } 082 String uri = NuxeoUriLocator.getUri(resource); 083 if (uri == null) { 084 log.error( 085 "Cannot handle resource '" + resource.getName() + "' for bundle '" + bundle + "': no uri resolved"); 086 return null; 087 } 088 ro.isdc.wro.model.resource.Resource res = ro.isdc.wro.model.resource.Resource.create(uri, type); 089 res.setMinimize(resource.isShrinkable()); 090 return res; 091 } 092 093 protected ro.isdc.wro.model.resource.ResourceType toWroResourceType(String type) { 094 if (StringUtils.isBlank(type)) { 095 return null; 096 } 097 if (ResourceType.js.name().equals(type.toLowerCase())) { 098 return ro.isdc.wro.model.resource.ResourceType.JS; 099 } 100 if (ResourceType.css.name().equals(type.toLowerCase())) { 101 return ro.isdc.wro.model.resource.ResourceType.CSS; 102 } 103 return null; 104 } 105 106 public void destroy() { 107 } 108 109}