1 17 package org.apache.lenya.cms.cocoon.components.modules.input; 18 19 import java.util.Collections ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.configuration.Configuration; 24 import org.apache.avalon.framework.configuration.ConfigurationException; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.ServiceSelector; 29 import org.apache.cocoon.components.modules.input.AbstractInputModule; 30 import org.apache.cocoon.environment.ObjectModelHelper; 31 import org.apache.cocoon.environment.Request; 32 import org.apache.lenya.ac.AccessController; 33 import org.apache.lenya.ac.AccessControllerResolver; 34 import org.apache.lenya.ac.AccreditableManager; 35 import org.apache.lenya.ac.Authorizer; 36 import org.apache.lenya.ac.Policy; 37 import org.apache.lenya.ac.PolicyManager; 38 import org.apache.lenya.ac.impl.DefaultAccessController; 39 import org.apache.lenya.ac.impl.PolicyAuthorizer; 40 import org.apache.lenya.cms.publication.Document; 41 import org.apache.lenya.cms.publication.DocumentBuilder; 42 import org.apache.lenya.cms.publication.PageEnvelope; 43 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 44 import org.apache.lenya.cms.publication.Proxy; 45 import org.apache.lenya.cms.publication.Publication; 46 47 61 public class ProxyUrlModule extends AbstractInputModule implements Serviceable { 62 63 private ServiceManager manager; 64 65 69 public Object getAttribute(String name, Configuration modeConf, Map objectModel) 70 throws ConfigurationException { 71 72 ServiceSelector serviceSelector = null; 73 PolicyManager policyManager = null; 74 AccessControllerResolver acResolver = null; 75 AccreditableManager accreditableManager = null; 76 77 final String [] attributes = name.split(":"); 79 80 if (attributes.length < 3) { 81 throw new ConfigurationException("Invalid number of parameters: " + attributes.length 82 + ". Expected area, document-id, language."); 83 } 84 85 final String area = attributes[0]; 86 final String documentId = attributes[1]; 87 final String language = attributes[2]; 88 89 String value = null; 90 try { 91 PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 92 Publication publication = envelope.getPublication(); 93 94 DocumentBuilder builder = publication.getDocumentBuilder(); 95 96 String canonicalUrl = builder 98 .buildCanonicalUrl(publication, area, documentId, language); 99 100 if (getLogger().isDebugEnabled()) { 101 getLogger().debug("Created canonicalURL: " + canonicalUrl); 102 } 103 104 serviceSelector = (ServiceSelector) this.manager.lookup(AccessControllerResolver.ROLE 106 + "Selector"); 107 acResolver = (AccessControllerResolver) serviceSelector 108 .select(AccessControllerResolver.DEFAULT_RESOLVER); 109 110 AccessController accessController = acResolver.resolveAccessController(canonicalUrl); 111 if (accessController instanceof DefaultAccessController) { 112 DefaultAccessController defaultAccessController = (DefaultAccessController) accessController; 113 accreditableManager = defaultAccessController.getAccreditableManager(); 114 Authorizer[] authorizers = defaultAccessController.getAuthorizers(); 115 for (int i = 0; i < authorizers.length; i++) { 116 if (authorizers[i] instanceof PolicyAuthorizer) { 117 PolicyAuthorizer policyAuthorizer = (PolicyAuthorizer) authorizers[i]; 118 policyManager = policyAuthorizer.getPolicyManager(); 119 } 120 } 121 } 122 123 Policy policy = policyManager.getPolicy(accreditableManager, canonicalUrl); 124 125 Document doc = builder.buildDocument(publication, canonicalUrl); 126 127 Proxy proxy = doc.getPublication().getProxy(doc, policy.isSSLProtected()); 128 129 if (proxy != null) { 130 value = proxy.getURL(doc); 131 } else { 132 Request request = ObjectModelHelper.getRequest(objectModel); 134 value = "http://" + request.getServerName() + ":" + request.getServerPort() 135 + request.getContextPath() + doc.getCompleteURL(); 136 } 137 138 } catch (Exception e) { 139 throw new ConfigurationException("Obtaining value for [" + name + "] failed: ", e); 140 } 141 return value; 142 } 143 144 148 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) 149 throws ConfigurationException { 150 return Collections.EMPTY_SET.iterator(); 151 } 152 153 157 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) 158 throws ConfigurationException { 159 Object [] objects = { getAttribute(name, modeConf, objectModel) }; 160 return objects; 161 } 162 163 166 public void service(ServiceManager manager) throws ServiceException { 167 this.manager = manager; 168 } 169 } | Popular Tags |