01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.lenya.cms.linking;
19:
20: import org.apache.lenya.cms.publication.Proxy;
21: import org.apache.lenya.cms.publication.Publication;
22:
23: /**
24: * <p>
25: * Converts browser-based links to web application links by using the
26: * publication's proxy settings.
27: * </p>
28: * <p>
29: * Objects of this class are not thread-safe.
30: * </p>
31: */
32: public class IncomingLinkRewriter implements LinkRewriter {
33:
34: private Publication pub;
35:
36: /**
37: * @param pub The current publication.
38: */
39: public IncomingLinkRewriter(Publication pub) {
40: this .pub = pub;
41: }
42:
43: public boolean matches(String url) {
44: return getMatchingProxyConfiguration(url) != null;
45: }
46:
47: protected ProxyConfiguration getMatchingProxyConfiguration(
48: String url) {
49: ProxyConfiguration config = null;
50: String[] areas = this .pub.getAreaNames();
51: Boolean[] sslValues = { Boolean.FALSE, Boolean.TRUE };
52: for (int a = 0; a < areas.length; a++) {
53: for (int s = 0; s < sslValues.length; s++) {
54: Proxy proxy = this .pub.getProxy(areas[a], sslValues[s]
55: .booleanValue());
56: if (config == null && url.startsWith(proxy.getUrl())) {
57: config = new ProxyConfiguration(areas[a],
58: sslValues[s].booleanValue());
59: }
60: }
61: }
62: return config;
63: }
64:
65: public String rewrite(String url) {
66: ProxyConfiguration config = getMatchingProxyConfiguration(url);
67: if (config == null) {
68: throw new RuntimeException(
69: "No matching proxy config for URL [" + url + "]");
70: }
71: Proxy proxy = this .pub.getProxy(config.area, config.ssl);
72: String suffix = url.substring(proxy.getUrl().length());
73: return "/" + this .pub.getId() + "/" + config.area + suffix;
74: }
75:
76: protected static class ProxyConfiguration {
77: protected String area;
78: protected boolean ssl;
79:
80: protected ProxyConfiguration(String area, boolean ssl) {
81: this.area = area;
82: this.ssl = ssl;
83: }
84: }
85:
86: }
|