View | Details | Raw Unified | Return to bug 42050
Collapse All | Expand All

(-)src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java (-11 / +74 lines)
Lines 18-27 Link Here
18
package org.apache.lenya.cms.cocoon.transformation;
18
package org.apache.lenya.cms.cocoon.transformation;
19
19
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.util.ArrayList;
21
import java.util.Arrays;
22
import java.util.Arrays;
23
import java.util.List;
22
import java.util.Map;
24
import java.util.Map;
23
25
24
import org.apache.avalon.framework.activity.Disposable;
26
import org.apache.avalon.framework.activity.Disposable;
27
import org.apache.avalon.framework.configuration.Configuration;
28
import org.apache.avalon.framework.configuration.ConfigurationException;
25
import org.apache.avalon.framework.parameters.Parameters;
29
import org.apache.avalon.framework.parameters.Parameters;
26
import org.apache.avalon.framework.service.ServiceException;
30
import org.apache.avalon.framework.service.ServiceException;
27
import org.apache.avalon.framework.service.ServiceSelector;
31
import org.apache.avalon.framework.service.ServiceSelector;
Lines 47-52 Link Here
47
import org.apache.lenya.cms.repository.Session;
51
import org.apache.lenya.cms.repository.Session;
48
import org.apache.lenya.util.Query;
52
import org.apache.lenya.util.Query;
49
import org.apache.lenya.util.ServletHelper;
53
import org.apache.lenya.util.ServletHelper;
54
import org.apache.lenya.util.StringUtil;
50
import org.xml.sax.Attributes;
55
import org.xml.sax.Attributes;
51
import org.xml.sax.SAXException;
56
import org.xml.sax.SAXException;
52
import org.xml.sax.helpers.AttributesImpl;
57
import org.xml.sax.helpers.AttributesImpl;
Lines 99-104 Link Here
99
    private DocumentFactory factory;
104
    private DocumentFactory factory;
100
    private LinkResolver linkResolver;
105
    private LinkResolver linkResolver;
101
    private String contextPath;
106
    private String contextPath;
107
    
108
    private boolean relativeUrls = false;
102
109
103
    /**
110
    /**
104
     * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
111
     * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
Lines 155-160 Link Here
155
        }
162
        }
156
    }
163
    }
157
164
165
    public void configure(Configuration config) throws ConfigurationException {
166
        super.configure(config);
167
        Configuration urlConfig = config.getChild("urls", false);
168
        if (urlConfig != null) {
169
            String value = urlConfig.getAttribute("type");
170
            if (value.equals("relative")) {
171
                this.relativeUrls = true;
172
            }
173
            else if (value.equals("absolute")) {
174
                this.relativeUrls = false;
175
            }
176
            else {
177
                throw new ConfigurationException("Invalid URL type [" + value
178
                        + "], must be relative or absolute.");
179
            }
180
        }
181
    }
182
158
    /**
183
    /**
159
     * Returns the currently processed document.
184
     * Returns the currently processed document.
160
     * 
185
     * 
Lines 330-346 Link Here
330
            throws AccessControlException {
355
            throws AccessControlException {
331
356
332
        String webappUrl = targetDocument.getCanonicalWebappURL();
357
        String webappUrl = targetDocument.getCanonicalWebappURL();
333
        Policy policy = this.policyManager.getPolicy(this.accreditableManager, webappUrl);
358
        
334
335
        Proxy proxy = targetDocument.getPublication().getProxy(targetDocument,
336
                policy.isSSLProtected());
337
338
        String rewrittenURL;
359
        String rewrittenURL;
339
        if (proxy == null) {
360
        if (this.relativeUrls) {
340
            rewrittenURL = this.request.getContextPath() + webappUrl;
361
            rewrittenURL = getRelativeUrlTo(webappUrl);
341
        } else {
342
            rewrittenURL = proxy.getURL(targetDocument);
343
        }
362
        }
363
        else {
364
            Policy policy = this.policyManager.getPolicy(this.accreditableManager, webappUrl);
365
    
366
            Proxy proxy = targetDocument.getPublication().getProxy(targetDocument,
367
                    policy.isSSLProtected());
368
    
369
            if (proxy == null) {
370
                rewrittenURL = this.request.getContextPath() + webappUrl;
371
            } else {
372
                rewrittenURL = proxy.getURL(targetDocument);
373
            }
374
            if (getLogger().isDebugEnabled()) {
375
                getLogger().debug(this.indent + "SSL protection: [" + policy.isSSLProtected() + "]");
376
                getLogger().debug(this.indent + "Resolved proxy: [" + proxy + "]");
377
            }
378
        }
344
379
345
        int lastDotIndex = rewrittenURL.lastIndexOf(".");
380
        int lastDotIndex = rewrittenURL.lastIndexOf(".");
346
        if (lastDotIndex > -1) {
381
        if (lastDotIndex > -1) {
Lines 356-369 Link Here
356
        }
391
        }
357
392
358
        if (getLogger().isDebugEnabled()) {
393
        if (getLogger().isDebugEnabled()) {
359
            getLogger().debug(this.indent + "SSL protection: [" + policy.isSSLProtected() + "]");
360
            getLogger().debug(this.indent + "Resolved proxy: [" + proxy + "]");
361
            getLogger().debug(this.indent + "Rewriting URL to: [" + rewrittenURL + "]");
394
            getLogger().debug(this.indent + "Rewriting URL to: [" + rewrittenURL + "]");
362
        }
395
        }
363
396
364
        setAttribute(newAttrs, attributeName, rewrittenURL);
397
        setAttribute(newAttrs, attributeName, rewrittenURL);
365
    }
398
    }
366
399
400
    private String getRelativeUrlTo(String webappUrl) {
401
        String currentUrl = this.currentDocument.getCanonicalWebappURL();
402
        List sourceSteps = toList(currentUrl);
403
        List targetSteps = toList(webappUrl);
404
        
405
        while (!sourceSteps.isEmpty() && !targetSteps.isEmpty()
406
                && sourceSteps.get(0).equals(targetSteps.get(0))) {
407
            sourceSteps.remove(0);
408
            targetSteps.remove(0);
409
        }
410
        
411
        String upDots = "";
412
        if (sourceSteps.size() > 1) {
413
            String[] upDotsArray = new String[sourceSteps.size() - 1];
414
            Arrays.fill(upDotsArray, "..");
415
            upDots = StringUtil.join(upDotsArray, "/") + "/";
416
        }
417
        
418
        String[] targetArray = (String[]) targetSteps.toArray(new String[targetSteps.size()]);
419
        String targetPath = StringUtil.join(targetArray, "/");
420
        
421
        String relativeUrl = upDots + targetPath;
422
        return relativeUrl;
423
        
424
    }
425
426
    protected List toList(String url) {
427
        return new ArrayList(Arrays.asList(url.substring(1).split("/")));
428
    }
429
367
    /**
430
    /**
368
     * Sets the value of the href attribute.
431
     * Sets the value of the href attribute.
369
     * 
432
     * 

Return to bug 42050