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.util.Map;
022:
023: import org.apache.avalon.framework.configuration.Configuration;
024: import org.apache.avalon.framework.configuration.ConfigurationException;
025: import org.apache.avalon.framework.parameters.Parameters;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.environment.ObjectModelHelper;
028: import org.apache.cocoon.environment.Request;
029: import org.apache.cocoon.environment.SourceResolver;
030: import org.apache.lenya.cms.linking.LinkRewriter;
031: import org.apache.lenya.cms.linking.OutgoingLinkRewriter;
032: import org.apache.lenya.cms.repository.RepositoryUtil;
033: import org.apache.lenya.cms.repository.Session;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * <p>
038: * Proxy transformer.
039: * </p>
040: * <p>
041: * The resulting URLs can either be absolute (default) or relative. You can
042: * either configure this when declaring the transformer:
043: * </p>
044: * <code><pre>
045: * <map:transformer ... >
046: * <urls type="relative"/>
047: * ...
048: * </map:transformer>
049: * </pre></code>
050: * <p>
051: * or pass a parameter:
052: * </p>
053: * <code><pre>
054: * <map:parameter name="urls" value="relative"/>
055: * </pre></code>
056: * @see OutgoingLinkRewriter
057: */
058: public class ProxyTransformer extends AbstractLinkTransformer {
059:
060: protected static final String ATTRIBUTE_TYPE = "type";
061: protected static final String URL_TYPE_ABSOLUTE = "absolute";
062: protected static final String URL_TYPE_RELATIVE = "relative";
063: protected static final String PARAMETER_URLS = "urls";
064:
065: private boolean relativeUrls = false;
066: private LinkRewriter rewriter;
067:
068: public void setup(SourceResolver _resolver, Map _objectModel,
069: String _source, Parameters _parameters)
070: throws ProcessingException, SAXException, IOException {
071: super .setup(_resolver, _objectModel, _source, _parameters);
072: Request _request = ObjectModelHelper.getRequest(_objectModel);
073:
074: try {
075: if (_parameters.isParameter(PARAMETER_URLS)) {
076: setUrlType(_parameters.getParameter(PARAMETER_URLS));
077: }
078: Session session = RepositoryUtil.getSession(this .manager,
079: _request);
080: this .rewriter = new OutgoingLinkRewriter(this .manager,
081: session, _request.getRequestURI(), request
082: .isSecure(), false, this .relativeUrls);
083: } catch (final Exception e) {
084: throw new RuntimeException(e);
085: }
086: }
087:
088: public void configure(Configuration config)
089: throws ConfigurationException {
090: super .configure(config);
091: Configuration urlConfig = config
092: .getChild(PARAMETER_URLS, false);
093: if (urlConfig != null) {
094: String value = urlConfig.getAttribute(ATTRIBUTE_TYPE);
095: setUrlType(value);
096: }
097: }
098:
099: protected void setUrlType(String value)
100: throws ConfigurationException {
101: if (value.equals(URL_TYPE_RELATIVE)) {
102: this .relativeUrls = true;
103: } else if (value.equals(URL_TYPE_ABSOLUTE)) {
104: this .relativeUrls = false;
105: } else {
106: throw new ConfigurationException("Invalid URL type ["
107: + value + "], must be relative or absolute.");
108: }
109: }
110:
111: protected LinkRewriter getLinkRewriter() {
112: return this.rewriter;
113: }
114:
115: }
|