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.components.modules.input;
019:
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import org.apache.avalon.framework.configuration.Configuration;
025: import org.apache.avalon.framework.configuration.ConfigurationException;
026: import org.apache.avalon.framework.service.ServiceException;
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.avalon.framework.service.Serviceable;
029: import org.apache.cocoon.components.modules.input.AbstractInputModule;
030: import org.apache.cocoon.environment.ObjectModelHelper;
031: import org.apache.cocoon.environment.Request;
032: import org.apache.lenya.cms.linking.LinkRewriter;
033: import org.apache.lenya.cms.linking.OutgoingLinkRewriter;
034: import org.apache.lenya.cms.repository.RepositoryException;
035: import org.apache.lenya.cms.repository.RepositoryUtil;
036: import org.apache.lenya.cms.repository.Session;
037:
038: /**
039: * <p>
040: * Input module for getting the base URL which may be prepended to internal URLs
041: * to construct links. The functionality corresponds to the
042: * {@link org.apache.lenya.cms.cocoon.transformation.ProxyTransformer} with one
043: * exception: If the <em>webappUrl</em> parameter is an empty string, the root
044: * proxy URL (or the context prefix, resp.) is returned.
045: * </p>
046: * <p>
047: * Usage: <code>{proxy:{webappUrl}}</code>
048: * </p>
049: * <p>
050: * The module can be configured to use absolute or relative URLs in the same way
051: * as the {@link org.apache.lenya.cms.cocoon.transformation.ProxyTransformer}.
052: * </p>
053: */
054: public class ProxyModule extends AbstractInputModule implements
055: Serviceable {
056:
057: protected static final String ATTRIBUTE_TYPE = "type";
058: protected static final String URL_TYPE_ABSOLUTE = "absolute";
059: protected static final String URL_TYPE_RELATIVE = "relative";
060: protected static final String PARAMETER_URLS = "urls";
061:
062: private ServiceManager manager;
063: private boolean relativeUrls;
064:
065: /**
066: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
067: * org.apache.avalon.framework.configuration.Configuration,
068: * java.util.Map)
069: */
070: public Object getAttribute(String name, Configuration modeConf,
071: Map objectModel) throws ConfigurationException {
072:
073: final String webappUrl = name;
074: Request request = ObjectModelHelper.getRequest(objectModel);
075:
076: String value = null;
077: try {
078: if (webappUrl.equals("")) {
079: value = rewrite(request, "/");
080: if (value.endsWith("/")) {
081: value = value.substring(0, value.length() - 1);
082: }
083: } else {
084: value = rewrite(request, webappUrl);
085: }
086: } catch (ConfigurationException e) {
087: throw e;
088: } catch (Exception e) {
089: throw new ConfigurationException("Obtaining value for ["
090: + name + "] failed: ", e);
091: }
092: return value;
093: }
094:
095: protected String rewrite(Request request, String url)
096: throws RepositoryException, ConfigurationException {
097: Session session = RepositoryUtil.getSession(this .manager,
098: request);
099: LinkRewriter rewriter = new OutgoingLinkRewriter(this .manager,
100: session, request.getRequestURI(), request.isSecure(),
101: false, this .relativeUrls);
102: if (!rewriter.matches(url)) {
103: throw new ConfigurationException("The URL [" + url
104: + "] can't be rewritten!");
105: }
106: return rewriter.rewrite(url);
107: }
108:
109: /**
110: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration,
111: * java.util.Map)
112: */
113: public Iterator getAttributeNames(Configuration modeConf,
114: Map objectModel) throws ConfigurationException {
115: return Collections.EMPTY_SET.iterator();
116: }
117:
118: /**
119: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String,
120: * org.apache.avalon.framework.configuration.Configuration,
121: * java.util.Map)
122: */
123: public Object[] getAttributeValues(String name,
124: Configuration modeConf, Map objectModel)
125: throws ConfigurationException {
126: Object[] objects = { getAttribute(name, modeConf, objectModel) };
127: return objects;
128: }
129:
130: /**
131: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
132: */
133: public void service(ServiceManager manager) throws ServiceException {
134: this .manager = manager;
135: }
136:
137: public void configure(Configuration conf)
138: throws ConfigurationException {
139: super .configure(conf);
140: Configuration urlConfig = conf.getChild(PARAMETER_URLS, false);
141: if (urlConfig != null) {
142: String value = urlConfig.getAttribute(ATTRIBUTE_TYPE);
143: setUrlType(value);
144: }
145: }
146:
147: protected void setUrlType(String value)
148: throws ConfigurationException {
149: if (value.equals(URL_TYPE_RELATIVE)) {
150: this .relativeUrls = true;
151: } else if (value.equals(URL_TYPE_ABSOLUTE)) {
152: this .relativeUrls = false;
153: } else {
154: throw new ConfigurationException("Invalid URL type ["
155: + value + "], must be relative or absolute.");
156: }
157: }
158:
159: }
|