001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.lenya.cms.cocoon.transformation;
019:
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.xml.parsers.ParserConfigurationException;
027: import javax.xml.transform.TransformerConfigurationException;
028: import javax.xml.transform.TransformerException;
029:
030: import org.apache.avalon.framework.context.Context;
031: import org.apache.avalon.framework.parameters.Parameters;
032: import org.apache.avalon.framework.service.ServiceException;
033: import org.apache.avalon.framework.service.ServiceSelector;
034: import org.apache.cocoon.components.ContextHelper;
035: import org.apache.cocoon.components.serializers.XHTMLSerializer;
036: import org.apache.cocoon.components.source.SourceResolverAdapter;
037: import org.apache.excalibur.source.SourceNotFoundException;
038: import org.apache.excalibur.source.SourceResolver;
039: import org.apache.lenya.ac.impl.AbstractAccessControlTest;
040: import org.apache.lenya.cms.cocoon.source.SourceUtil;
041: import org.apache.lenya.cms.linking.GlobalProxies;
042: import org.apache.lenya.cms.linking.impl.GlobalProxiesImpl;
043: import org.apache.lenya.cms.publication.Proxy;
044: import org.apache.lenya.cms.publication.Publication;
045: import org.apache.lenya.cms.publication.PublicationException;
046: import org.apache.lenya.cms.publication.templating.Instantiator;
047: import org.apache.lenya.xml.NamespaceHelper;
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050: import org.xml.sax.SAXException;
051: import org.xml.sax.helpers.AttributesImpl;
052:
053: public class ProxyTransformerTest extends AbstractAccessControlTest {
054:
055: protected static final String PUBCONF_NAMESPACE = "http://apache.org/cocoon/lenya/publication/1.1";
056:
057: protected static final String NAMESPACE = XHTMLSerializer.XHTML1_NAMESPACE;
058: protected static final String ELEMENT = "a";
059: protected static final String ATTRIBUTE = "href";
060:
061: protected String getWebappUrl() {
062: return "/default/authoring/index.html";
063: }
064:
065: public void testProxyTransformer() throws Exception {
066:
067: ProxyTransformer transformer = new ProxyTransformer();
068: transformer.enableLogging(getLogger());
069: transformer.service(getManager());
070:
071: String pubId = "mock";
072: String area = "authoring";
073: String proxyUrl = "http://www.mock.org";
074:
075: createMockPublication(pubId, area, proxyUrl);
076:
077: Context context = this .context;
078: Map objectModel = (Map) context
079: .get(ContextHelper.CONTEXT_OBJECT_MODEL);
080:
081: SourceResolver resolver = null;
082: try {
083: resolver = (SourceResolver) getManager().lookup(
084: SourceResolver.ROLE);
085: transformer.setup(new SourceResolverAdapter(resolver),
086: objectModel, "", new Parameters());
087:
088: String documentUrl = "/index.html";
089: String linkUrl = "/" + pubId + "/" + area + documentUrl;
090: String targetUrl = proxyUrl + documentUrl;
091: rewriteLink(transformer, linkUrl, targetUrl);
092:
093: String cssUrl = "/lenya/foo.css";
094: rewriteLink(transformer, cssUrl, cssUrl);
095:
096: String moduleUrl = "/modules/foo/bar.html?x=y";
097: rewriteLink(transformer, moduleUrl, moduleUrl);
098:
099: } finally {
100: if (resolver != null) {
101: getManager().release(resolver);
102: }
103: }
104: }
105:
106: protected void rewriteLink(ProxyTransformer transformer,
107: String linkUrl, String targetUrl) throws Exception {
108: AttributesImpl attrs = new AttributesImpl();
109: attrs.addAttribute("", ATTRIBUTE, ATTRIBUTE, "string", linkUrl);
110:
111: AbstractLinkTransformer.AttributeConfiguration config = new AbstractLinkTransformer.AttributeConfiguration(
112: NAMESPACE, ELEMENT, ATTRIBUTE);
113:
114: transformer.handleLink(linkUrl, config, attrs);
115:
116: String rewrittenUrl = attrs.getValue(ATTRIBUTE);
117: assertEquals(rewrittenUrl, targetUrl);
118: }
119:
120: protected void createMockPublication(String pubId, String area,
121: String proxyUrl) throws PublicationException,
122: ServiceException, Exception {
123: if (!existsPublication(pubId)) {
124:
125: Publication defaultPub = getPublication("default");
126: Instantiator instantiator = null;
127: ServiceSelector selector = null;
128: try {
129: selector = (ServiceSelector) getManager().lookup(
130: Instantiator.ROLE + "Selector");
131: instantiator = (Instantiator) selector
132: .select(defaultPub.getInstantiatorHint());
133: instantiator.instantiate(defaultPub, pubId, "Mock");
134: configureProxy(area, proxyUrl);
135: } finally {
136: if (selector != null) {
137: if (instantiator != null) {
138: selector.release(instantiator);
139: }
140: getManager().release(selector);
141: }
142: }
143: }
144: }
145:
146: protected void configureProxy(String area, String proxyUrl)
147: throws ServiceException, SourceNotFoundException,
148: ParserConfigurationException, SAXException, IOException,
149: TransformerConfigurationException, TransformerException,
150: MalformedURLException {
151: String configUri = "context://lenya/pubs/mock/config/publication.xml";
152: Document dom = SourceUtil.readDOM(configUri, getManager());
153: NamespaceHelper helper = new NamespaceHelper(PUBCONF_NAMESPACE,
154: "", dom);
155:
156: Element proxies = helper.getFirstChild(
157: dom.getDocumentElement(), "proxies");
158: if (proxies == null) {
159: proxies = helper.createElement("proxies");
160: dom.getDocumentElement().appendChild(proxies);
161: }
162:
163: addProxyElement(helper, proxies, area, proxyUrl, false);
164: addProxyElement(helper, proxies, area, proxyUrl, true);
165:
166: SourceUtil.writeDOM(dom, configUri, getManager());
167: }
168:
169: protected void addProxyElement(NamespaceHelper helper,
170: Element proxies, String area, String proxyUrl, boolean ssl) {
171: Element proxyElement = helper.createElement("proxy");
172: proxyElement.setAttribute("ssl", Boolean.toString(ssl));
173: proxyElement.setAttribute("area", area);
174: proxyElement.setAttribute("url", proxyUrl);
175: proxies.appendChild(proxyElement);
176: }
177:
178: protected boolean existsPublication(String pubId) {
179: Publication[] pubs = getFactory().getPublications();
180: List pubIds = new ArrayList();
181: for (int i = 0; i < pubs.length; i++) {
182: pubIds.add(pubs[i].getId());
183: }
184: return pubIds.contains(pubId);
185: }
186: }
|