You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2007/06/12 18:24:46 UTC

svn commit: r546561 - in /lenya/trunk/src/modules-core/linking/java: src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java

Author: andreas
Date: Tue Jun 12 09:24:45 2007
New Revision: 546561

URL: http://svn.apache.org/viewvc?view=rev&rev=546561
Log:
Consider source publication in proxy transformer, update test accordingly

Modified:
    lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java
    lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java

Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java?view=diff&rev=546561&r1=546560&r2=546561
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/ProxyTransformer.java Tue Jun 12 09:24:45 2007
@@ -56,6 +56,7 @@
     private AccessControllerResolver acResolver;
     private AccreditableManager accreditableManager;
     private PolicyManager policyManager;
+    private Publication publication;
 
     protected static final String PARAMETER_FACTORY = "private.factory";
     private static final String ATTRIBUTE_ROOT = "root";
@@ -69,6 +70,11 @@
             Session session = RepositoryUtil.getSession(this.manager, _request);
             this.factory = DocumentUtil.createDocumentFactory(this.manager, session);
             this.url = ServletHelper.getWebappURI(_request);
+            URLInformation info = new URLInformation(this.url);
+            String pubId = info.getPublicationId();
+            if (pubId != null && isPublication(pubId)) {
+                this.publication = this.factory.getPublication(pubId);
+            }
         } catch (final Exception e) {
             throw new RuntimeException(e);
         }
@@ -94,7 +100,7 @@
             rewriteLink(newAttrs, config.attribute, linkUrl);
         }
     }
-    
+
     /**
      * @param pubId The publication ID.
      * @return If a publication with this ID exists.
@@ -111,37 +117,58 @@
     protected void rewriteLink(AttributesImpl newAttrs, String attributeName, String linkUrl)
             throws Exception {
         String rewrittenUrl = "";
-        Policy policy = null;
-        if (this.policyManager != null)
-            policy = this.policyManager.getPolicy(this.accreditableManager, linkUrl);
+        boolean ssl = false;
+        if (this.policyManager != null) {
+            Policy policy = this.policyManager.getPolicy(this.accreditableManager, linkUrl);
+            ssl = policy.isSSLProtected();
+        }
 
         URLInformation info = new URLInformation(linkUrl);
         String pubId = info.getPublicationId();
-        
+
+        // link points to publication
         if (pubId != null && isPublication(pubId)) {
             Publication pub = this.factory.getPublication(pubId);
-            String areaName = info.getArea();
-
-            // valid pub, valid area
-            if (areaName != null && Arrays.asList(pub.getAreaNames()).contains(areaName)) {
-                Proxy proxy = pub.getProxy(areaName, policy.isSSLProtected());
-                String proxiedUrl = info.getDocumentUrl();
-                rewrittenUrl = getProxyUrl(linkUrl, proxy, proxiedUrl);
-            }
-
-            // valid pub, invalid area
-            else {
-                Proxy proxy = pub.getProxy(ATTRIBUTE_ROOT, (policy == null) ? false : policy
-                        .isSSLProtected());
-                rewrittenUrl = getProxyUrl(linkUrl, proxy, linkUrl.substring(1));
-            }
+            rewrittenUrl = rewriteLink(linkUrl, pub, ssl);
         }
 
-        // invalid pub
+        // link doesn't point to publication -> use own publication if exists
+        else if (this.publication != null) {
+            rewrittenUrl = rewriteLink(linkUrl, this.publication, ssl);
+        }
+        
+        // link doesn't point to publication, no own publication
         else {
             rewrittenUrl = this.request.getContextPath() + linkUrl;
         }
+        
         setAttribute(newAttrs, attributeName, rewrittenUrl);
+    }
+
+    /**
+     * @param linkUrl The original link URL.
+     * @param pub The publication to use for proxy resolving.
+     * @param ssl If the URL uses SSL.
+     * @return A link URL.
+     */
+    protected String rewriteLink(String linkUrl, Publication pub, boolean ssl) {
+        URLInformation info = new URLInformation(linkUrl);
+        String rewrittenUrl;
+        String areaName = info.getArea();
+
+        // valid area
+        if (areaName != null && Arrays.asList(pub.getAreaNames()).contains(areaName)) {
+            Proxy proxy = pub.getProxy(areaName, ssl);
+            String proxiedUrl = info.getDocumentUrl();
+            rewrittenUrl = getProxyUrl(linkUrl, proxy, proxiedUrl);
+        }
+
+        // invalid area
+        else {
+            Proxy proxy = pub.getProxy(ATTRIBUTE_ROOT, ssl);
+            rewrittenUrl = getProxyUrl(linkUrl, proxy, linkUrl);
+        }
+        return rewrittenUrl;
     }
 
     /**

Modified: lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java?view=diff&rev=546561&r1=546560&r2=546561
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java (original)
+++ lenya/trunk/src/modules-core/linking/java/test/org/apache/lenya/cms/cocoon/transformation/ProxyTransformerTest.java Tue Jun 12 09:24:45 2007
@@ -50,7 +50,15 @@
 
 public class ProxyTransformerTest extends AbstractAccessControlTest {
 
-    private static final String PUBCONF_NAMESPACE = "http://apache.org/cocoon/lenya/publication/1.1";
+    protected static final String PUBCONF_NAMESPACE = "http://apache.org/cocoon/lenya/publication/1.1";
+    
+    protected static final String NAMESPACE = XHTMLSerializer.XHTML1_NAMESPACE;
+    protected static final String ELEMENT = "a";
+    protected static final String ATTRIBUTE = "href";
+
+    protected String getWebappUrl() {
+        return "/mock/authoring/index.html";
+    }
 
     public void testProxyTransformer() throws Exception {
 
@@ -80,20 +88,14 @@
 
             String documentUrl = "/index.html";
             String linkUrl = "/" + pubId + "/" + area + documentUrl;
-            String namespace = XHTMLSerializer.XHTML1_NAMESPACE;
-            String element = "a";
-            String attribute = "href";
-
-            AttributesImpl attrs = new AttributesImpl();
-            attrs.addAttribute("", attribute, attribute, "string", linkUrl);
-
-            AbstractLinkTransformer.AttributeConfiguration config = new AbstractLinkTransformer.AttributeConfiguration(
-                    namespace, element, attribute);
-
-            transformer.handleLink(linkUrl, config, attrs);
+            String targetUrl = proxyUrl + documentUrl;
+            rewriteLink(transformer, linkUrl, targetUrl);
+            
+            String cssUrl = "/lenya/foo.css";
+            rewriteLink(transformer, cssUrl, proxyUrl + cssUrl);
 
-            String rewrittenUrl = attrs.getValue(attribute);
-            assertEquals(rewrittenUrl, proxyUrl + documentUrl);
+            String moduleUrl = "/modules/foo/bar.html?x=y";
+            rewriteLink(transformer, moduleUrl, proxyUrl + moduleUrl);
 
         } finally {
             if (resolver != null) {
@@ -102,6 +104,19 @@
         }
     }
 
+    protected void rewriteLink(ProxyTransformer transformer, String linkUrl, String targetUrl) throws Exception {
+        AttributesImpl attrs = new AttributesImpl();
+        attrs.addAttribute("", ATTRIBUTE, ATTRIBUTE, "string", linkUrl);
+
+        AbstractLinkTransformer.AttributeConfiguration config = new AbstractLinkTransformer.AttributeConfiguration(
+                NAMESPACE, ELEMENT, ATTRIBUTE);
+
+        transformer.handleLink(linkUrl, config, attrs);
+
+        String rewrittenUrl = attrs.getValue(ATTRIBUTE);
+        assertEquals(rewrittenUrl, targetUrl);
+    }
+
     protected void createMockPublication(String pubId, String area, boolean ssl, String proxyUrl)
             throws PublicationException, ServiceException, Exception {
         if (!existsPublication(pubId)) {
@@ -134,7 +149,8 @@
         NamespaceHelper helper = new NamespaceHelper(PUBCONF_NAMESPACE, "", dom);
 
         Element proxies = helper.createElement("proxies");
-        proxies.setAttribute("ssl", Boolean.toString(ssl));
+        proxies.setAttribute("ssl", Boolean.toString(false));
+        proxies.setAttribute("root", proxyUrl);
         dom.getDocumentElement().appendChild(proxies);
 
         Element proxyElement = helper.createElement("proxy");



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org