Line 0
Link Here
|
|
|
1 |
package org.apache.lenya.cms.cocoon.transformation; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.Arrays; |
5 |
import java.util.Map; |
6 |
|
7 |
import org.apache.avalon.framework.parameters.Parameters; |
8 |
import org.apache.avalon.framework.service.ServiceSelector; |
9 |
import org.apache.cocoon.ProcessingException; |
10 |
import org.apache.cocoon.environment.ObjectModelHelper; |
11 |
import org.apache.cocoon.environment.Request; |
12 |
import org.apache.cocoon.environment.SourceResolver; |
13 |
import org.apache.cocoon.transformation.AbstractSAXTransformer; |
14 |
import org.apache.lenya.ac.AccessControlException; |
15 |
import org.apache.lenya.ac.AccessController; |
16 |
import org.apache.lenya.ac.AccessControllerResolver; |
17 |
import org.apache.lenya.ac.AccreditableManager; |
18 |
import org.apache.lenya.ac.Policy; |
19 |
import org.apache.lenya.ac.PolicyManager; |
20 |
import org.apache.lenya.cms.publication.DocumentBuildException; |
21 |
import org.apache.lenya.cms.publication.DocumentFactory; |
22 |
import org.apache.lenya.cms.publication.DocumentUtil; |
23 |
import org.apache.lenya.cms.publication.Proxy; |
24 |
import org.apache.lenya.cms.publication.Publication; |
25 |
import org.apache.lenya.cms.publication.PublicationUtil; |
26 |
import org.apache.lenya.cms.repository.RepositoryUtil; |
27 |
import org.apache.lenya.cms.repository.Session; |
28 |
import org.apache.lenya.util.ServletHelper; |
29 |
import org.xml.sax.Attributes; |
30 |
import org.xml.sax.SAXException; |
31 |
import org.xml.sax.helpers.AttributesImpl; |
32 |
|
33 |
public class ProxyTransformer extends AbstractSAXTransformer { |
34 |
protected static final String[] elementNames = { "a", "object", "img", |
35 |
"link", "form" }; |
36 |
|
37 |
protected static final String[] attributeNames = { "href", "src", "data", |
38 |
"action" }; |
39 |
|
40 |
private boolean ignoreLinkElement = false; |
41 |
|
42 |
private String indent = ""; |
43 |
|
44 |
private DocumentFactory factory; |
45 |
|
46 |
private Publication publication; |
47 |
|
48 |
private String url; |
49 |
|
50 |
private ServiceSelector serviceSelector; |
51 |
|
52 |
private AccessControllerResolver acResolver; |
53 |
|
54 |
private AccreditableManager accreditableManager; |
55 |
|
56 |
private PolicyManager policyManager; |
57 |
|
58 |
protected static final String PARAMETER_FACTORY = "private.factory"; |
59 |
|
60 |
public void setup(SourceResolver _resolver, Map _objectModel, String _source, |
61 |
Parameters _parameters) throws ProcessingException, SAXException, |
62 |
IOException { |
63 |
super.setup(_resolver, _objectModel, _source, _parameters); |
64 |
Request _request = ObjectModelHelper.getRequest(_objectModel); |
65 |
|
66 |
try { |
67 |
Session session = RepositoryUtil.getSession(this.manager, _request); |
68 |
this.factory = DocumentUtil.createDocumentFactory(this.manager, session); |
69 |
this.url = ServletHelper.getWebappURI(_request); |
70 |
this.publication = PublicationUtil.getPublicationFromUrl(this.manager, |
71 |
factory, url); |
72 |
} catch (final Exception e) { |
73 |
throw new RuntimeException(e); |
74 |
} |
75 |
this.serviceSelector = null; |
76 |
try { |
77 |
this.serviceSelector = (ServiceSelector) this.manager |
78 |
.lookup(AccessControllerResolver.ROLE + "Selector"); |
79 |
this.acResolver = (AccessControllerResolver) this.serviceSelector |
80 |
.select(AccessControllerResolver.DEFAULT_RESOLVER); |
81 |
AccessController accessController = this.acResolver |
82 |
.resolveAccessController(url); |
83 |
this.accreditableManager = accessController.getAccreditableManager(); |
84 |
this.policyManager = accessController.getPolicyManager(); |
85 |
} catch (final Exception e) { |
86 |
throw new RuntimeException(e); |
87 |
} |
88 |
|
89 |
} |
90 |
|
91 |
public void startElement(String uri, String name, String qname, |
92 |
Attributes attrs) throws SAXException { |
93 |
if (getLogger().isDebugEnabled()) { |
94 |
getLogger().debug( |
95 |
this.indent + "<" + qname + "> (ignoreAElement = " |
96 |
+ this.ignoreLinkElement + ")"); |
97 |
this.indent += " "; |
98 |
} |
99 |
AttributesImpl newAttrs = null; |
100 |
if (lookingAtLinkElement(name)) { |
101 |
|
102 |
for (int i = 0; i < attributeNames.length; i++) { |
103 |
String linkUrl = attrs.getValue(attributeNames[i]); |
104 |
if (linkUrl != null) { |
105 |
if (linkUrl.startsWith("/")) { |
106 |
try { |
107 |
newAttrs = new AttributesImpl(attrs); |
108 |
rewriteLink(newAttrs, attributeNames[i], linkUrl); |
109 |
if (getLogger().isDebugEnabled()) { |
110 |
getLogger().debug(this.indent + "link URL: [" + linkUrl + "]"); |
111 |
} |
112 |
} catch (final Exception e) { |
113 |
getLogger().error("startElement failed: ", e); |
114 |
throw new SAXException(e); |
115 |
} |
116 |
} |
117 |
} |
118 |
} |
119 |
if (newAttrs == null) |
120 |
super.startElement(uri, name, qname, attrs); |
121 |
else |
122 |
super.startElement(uri, name, qname, newAttrs); |
123 |
} else |
124 |
super.startElement(uri, name, qname, attrs); |
125 |
|
126 |
} |
127 |
|
128 |
public void endElement(String uri, String name, String qname) |
129 |
throws SAXException { |
130 |
if (getLogger().isDebugEnabled()) { |
131 |
this.indent = this.indent.substring(2); |
132 |
getLogger().debug(this.indent + "</" + qname + ">"); |
133 |
} |
134 |
if (getLogger().isDebugEnabled()) { |
135 |
getLogger().debug(this.indent + "</" + qname + "> sent"); |
136 |
} |
137 |
super.endElement(uri, name, qname); |
138 |
} |
139 |
|
140 |
private void rewriteLink(AttributesImpl newAttrs, String attributeName, |
141 |
String linkUrl) throws AccessControlException, DocumentBuildException { |
142 |
String rewrittenURL = ""; |
143 |
Policy policy; |
144 |
policy = this.policyManager.getPolicy(this.accreditableManager, linkUrl); |
145 |
String area = ""; |
146 |
try { |
147 |
area = factory.getFromURL(linkUrl).getArea(); |
148 |
} catch (RuntimeException e) { |
149 |
getLogger().warn("global link"); |
150 |
} |
151 |
if (PublicationUtil.isValidArea(area)) { |
152 |
Proxy proxy = this.publication.getProxy(area, policy.isSSLProtected()); |
153 |
if (proxy == null) { |
154 |
rewrittenURL = this.request.getContextPath() + linkUrl; |
155 |
} else { |
156 |
String prefix = "/" + publication.getId() + "/" + area; |
157 |
if (linkUrl.startsWith(prefix)) |
158 |
rewrittenURL = proxy.getUrl() + linkUrl.substring(prefix.length()); |
159 |
else |
160 |
rewrittenURL = proxy.getUrl() + linkUrl; |
161 |
} |
162 |
if (getLogger().isDebugEnabled()) { |
163 |
getLogger().debug( |
164 |
this.indent + "SSL protection: [" + policy.isSSLProtected() + "]"); |
165 |
getLogger().debug(this.indent + "Resolved proxy: [" + proxy + "]"); |
166 |
} |
167 |
|
168 |
if (getLogger().isDebugEnabled()) { |
169 |
getLogger().debug( |
170 |
this.indent + "Rewriting URL to: [" + rewrittenURL + "]"); |
171 |
} |
172 |
} else { |
173 |
rewrittenURL = linkUrl; |
174 |
getLogger().warn("URL [" + rewrittenURL + "] could not be rewritten."); |
175 |
|
176 |
} |
177 |
setAttribute(newAttrs, attributeName, rewrittenURL); |
178 |
} |
179 |
|
180 |
private boolean lookingAtLinkElement(String name) { |
181 |
return Arrays.asList(elementNames).contains(name); |
182 |
} |
183 |
|
184 |
/** |
185 |
* Sets the value of the href attribute. |
186 |
* |
187 |
* @param attr |
188 |
* The attributes. |
189 |
* @param name |
190 |
* The attribute name. |
191 |
* @param value |
192 |
* The value. |
193 |
* @throws IllegalArgumentException |
194 |
* if the href attribute is not contained in this attributes. |
195 |
*/ |
196 |
protected void setAttribute(AttributesImpl attr, String name, String value) { |
197 |
int position = attr.getIndex(name); |
198 |
if (position == -1) { |
199 |
throw new IllegalArgumentException("The href attribute is not available!"); |
200 |
} |
201 |
attr.setValue(position, value); |
202 |
} |
203 |
} |