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:
019: /* $Id: AbstractPageEnvelopeModule.java 568041 2007-08-21 09:47:23Z andreas $ */
020:
021: package org.apache.lenya.cms.cocoon.components.modules.input;
022:
023: import java.io.File;
024: import java.util.Map;
025:
026: import org.apache.avalon.framework.configuration.ConfigurationException;
027: import org.apache.cocoon.environment.Context;
028: import org.apache.cocoon.environment.ObjectModelHelper;
029: import org.apache.cocoon.environment.Request;
030: import org.apache.lenya.cms.publication.DocumentFactory;
031: import org.apache.lenya.cms.publication.PageEnvelope;
032: import org.apache.lenya.cms.publication.PageEnvelopeFactory;
033: import org.apache.lenya.cms.publication.Publication;
034: import org.apache.lenya.cms.publication.URLInformation;
035: import org.apache.lenya.util.ServletHelper;
036:
037: /**
038: * Abstract superclass for classes which need access to the page envelope.
039: *
040: * The web application URL can be provided in the attribute name, separated by a colon (":").
041: */
042: public abstract class AbstractPageEnvelopeModule extends
043: OperationModule {
044:
045: /**
046: * Get the the page envelope for the given objectModel.
047: * @param objectModel the objectModel for which the page enevelope is requested.
048: * @param name The attribute name.
049: * @return a <code>PageEnvelope</code>
050: * @throws ConfigurationException if the page envelope could not be instantiated.
051: */
052: protected PageEnvelope getEnvelope(Map objectModel, String name)
053: throws ConfigurationException {
054:
055: String webappUrl = null;
056: Request request = ObjectModelHelper.getRequest(objectModel);
057:
058: PageEnvelope envelope = (PageEnvelope) request
059: .getAttribute(PageEnvelope.class.getName());
060: if (envelope == null) {
061:
062: String[] snippets = name.split(":");
063: if (snippets.length > 1) {
064: webappUrl = snippets[1];
065: } else {
066: webappUrl = ServletHelper.getWebappURI(request);
067: }
068:
069: if (getLogger().isDebugEnabled()) {
070: getLogger().debug(
071: "Resolving page envelope for URL [" + webappUrl
072: + "]");
073: }
074:
075: String contextPath = request.getContextPath();
076: Context context = ObjectModelHelper.getContext(objectModel);
077: String servletContextPath = context.getRealPath("");
078:
079: try {
080: DocumentFactory factory = getDocumentFactory();
081: Publication pub = null;
082: String pubId = new URLInformation(webappUrl)
083: .getPublicationId();
084: if (pubId != null && factory.existsPublication(pubId)) {
085: pub = factory.getPublication(pubId);
086: }
087: envelope = PageEnvelopeFactory.getInstance()
088: .getPageEnvelope(factory, contextPath,
089: webappUrl,
090: new File(servletContextPath), pub);
091: } catch (Exception e) {
092: throw new ConfigurationException(
093: "Resolving page envelope failed: ", e);
094: }
095: request
096: .setAttribute(PageEnvelope.class.getName(),
097: envelope);
098: }
099: return envelope;
100: }
101:
102: /**
103: * @param name The original attribute name.
104: * @return The attribute name without URL attachment.
105: */
106: protected String getAttributeName(String name) {
107: final String[] snippets = name.split(":");
108: return snippets[0];
109: }
110:
111: }
|