KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > components > modules > input > ProxyUrlModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.lenya.cms.cocoon.components.modules.input;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
48  * Input module for getting the proxied URL of a document.
49  *
50  * <p>
51  * Usage: <code>{proxy-url:{area}:{document-id}:{language}}</code>
52  * </p>
53  *
54  * <p>
55  * If there are no proxy settings in the file conf/publication.xconf, the values of the request
56  * parameters 'server name' and 'port' will be used to construct the URL.
57  * </p>
58  *
59  * @version $Id:$
60  */

61 public class ProxyUrlModule extends AbstractInputModule implements Serviceable {
62
63     private ServiceManager manager;
64
65     /**
66      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
67      * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
68      */

69     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
70             throws ConfigurationException {
71
72         ServiceSelector serviceSelector = null;
73         PolicyManager policyManager = null;
74         AccessControllerResolver acResolver = null;
75         AccreditableManager accreditableManager = null;
76
77         // Get parameters
78
final String JavaDoc[] 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 JavaDoc area = attributes[0];
86         final String JavaDoc documentId = attributes[1];
87         final String JavaDoc language = attributes[2];
88
89         String JavaDoc value = null;
90         try {
91             PageEnvelope envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
92             Publication publication = envelope.getPublication();
93
94             DocumentBuilder builder = publication.getDocumentBuilder();
95
96             // Create canonical URL
97
String JavaDoc canonicalUrl = builder
98                     .buildCanonicalUrl(publication, area, documentId, language);
99
100             if (getLogger().isDebugEnabled()) {
101                 getLogger().debug("Created canonicalURL: " + canonicalUrl);
102             }
103
104             // Get proxy for document
105
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                 // Take server name and port from request.
133
Request request = ObjectModelHelper.getRequest(objectModel);
134                 value = "http://" + request.getServerName() + ":" + request.getServerPort()
135                         + request.getContextPath() + doc.getCompleteURL();
136             }
137
138         } catch (Exception JavaDoc e) {
139             throw new ConfigurationException("Obtaining value for [" + name + "] failed: ", e);
140         }
141         return value;
142     }
143
144     /**
145      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
146      * java.util.Map)
147      */

148     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
149             throws ConfigurationException {
150         return Collections.EMPTY_SET.iterator();
151     }
152
153     /**
154      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
155      * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
156      */

157     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
158             throws ConfigurationException {
159         Object JavaDoc[] objects = { getAttribute(name, modeConf, objectModel) };
160         return objects;
161     }
162
163     /**
164      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
165      */

166     public void service(ServiceManager manager) throws ServiceException {
167         this.manager = manager;
168     }
169 }
Popular Tags