1 17 18 19 20 package org.apache.lenya.cms.ac; 21 22 import java.io.File ; 23 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.configuration.Configurable; 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 28 import org.apache.excalibur.source.Source; 29 import org.apache.excalibur.source.SourceResolver; 30 import org.apache.excalibur.source.SourceUtil; 31 import org.apache.lenya.ac.AccessControlException; 32 import org.apache.lenya.ac.AccessController; 33 import org.apache.lenya.ac.impl.AbstractAccessControllerResolver; 34 import org.apache.lenya.cms.publication.Publication; 35 import org.apache.lenya.cms.publication.PublicationException; 36 import org.apache.lenya.cms.publication.PublicationFactory; 37 import org.apache.lenya.cms.publication.URLInformation; 38 39 42 public class PublicationAccessControllerResolver 43 extends AbstractAccessControllerResolver 44 implements Initializable { 45 46 protected static final String CONFIGURATION_FILE = 47 "config/ac/ac.xconf".replace('/', File.separatorChar); 48 protected static final String TYPE_ATTRIBUTE = "type"; 49 50 55 protected Object generateCacheKey(String webappUrl, SourceResolver resolver) 56 throws AccessControlException { 57 58 URLInformation info = new URLInformation(webappUrl); 59 60 String publicationId = info.getPublicationId(); 61 if (getLogger().isDebugEnabled()) { 62 getLogger().debug( 63 "Using first URL step (might be publication ID) as cache key: [" + publicationId + "]"); 64 } 65 66 return super.generateCacheKey(publicationId, resolver); 67 } 68 69 72 public AccessController doResolveAccessController(String webappUrl) 73 throws AccessControlException { 74 getLogger().debug("Resolving controller for URL [" + webappUrl + "]"); 75 76 AccessController controller = null; 77 Publication publication = getPublication(webappUrl); 78 79 if (publication != null) { 80 String publicationUrl = webappUrl.substring(("/" + publication.getId()).length()); 81 controller = resolveAccessController(publication, publicationUrl); 82 } 83 return controller; 84 } 85 86 93 protected Publication getPublication(String webappUrl) throws AccessControlException { 94 Publication publication = null; 95 96 assert webappUrl.startsWith("/"); 97 String url = webappUrl.substring(1); 99 100 if (url.length() > 0) { 101 102 URLInformation info = new URLInformation(webappUrl); 103 String publicationId = info.getPublicationId(); 104 105 File contextDir = getContext(); 106 if (PublicationFactory 107 .existsPublication(publicationId, contextDir.getAbsolutePath())) { 108 109 getLogger().debug("Publication [" + publicationId + "] exists."); 110 try { 111 publication = 112 PublicationFactory.getPublication( 113 publicationId, 114 contextDir.getAbsolutePath()); 115 } catch (PublicationException e) { 116 throw new AccessControlException(e); 117 } 118 119 } else { 120 getLogger().debug("Publication [" + publicationId + "] does not exist."); 121 } 122 } 123 return publication; 124 } 125 126 131 protected File getContext() throws AccessControlException { 132 return context; 133 } 134 135 private File context; 136 137 144 public AccessController resolveAccessController(Publication publication, String url) 145 throws AccessControlException { 146 147 assert publication != null; 148 149 AccessController accessController = null; 150 File configurationFile = new File (publication.getDirectory(), CONFIGURATION_FILE); 151 152 if (configurationFile.isFile()) { 153 try { 154 Configuration configuration = 155 new DefaultConfigurationBuilder().buildFromFile(configurationFile); 156 String type = configuration.getAttribute(TYPE_ATTRIBUTE); 157 158 accessController = 159 (AccessController) getManager().lookup(AccessController.ROLE + "/" + type); 160 161 if (accessController instanceof Configurable) { 162 ((Configurable) accessController).configure(configuration); 163 } 164 165 } catch (Exception e) { 166 throw new AccessControlException(e); 167 } 168 } 169 170 return accessController; 171 } 172 173 176 public void initialize() throws Exception { 177 SourceResolver resolver = null; 178 Source contextSource = null; 179 File contextDir; 180 try { 181 resolver = (SourceResolver) getManager().lookup(SourceResolver.ROLE); 182 contextSource = resolver.resolveURI("context:///"); 183 contextDir = SourceUtil.getFile(contextSource); 184 185 if (contextDir == null || !contextDir.isDirectory()) { 186 throw new AccessControlException("The servlet context is not a directory!"); 187 } 188 189 } finally { 190 if (resolver != null) { 191 if (contextSource != null) { 192 resolver.release(contextSource); 193 } 194 getManager().release(resolver); 195 } 196 } 197 this.context = contextDir; 198 } 199 200 } 201 | Popular Tags |