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: PageEnvelope.java 589768 2007-10-29 17:45:43Z andreas $ */
020:
021: package org.apache.lenya.cms.publication;
022:
023: import java.io.File;
024:
025: import org.apache.avalon.framework.logger.ConsoleLogger;
026: import org.apache.cocoon.environment.Request;
027: import org.apache.lenya.cms.rc.RCEnvironment;
028:
029: /**
030: * A page envelope carries a set of information that are needed during the presentation of a
031: * document.
032: */
033: public class PageEnvelope {
034: /**
035: * The names of the page envelope parameters.
036: */
037: public static final String[] PARAMETER_NAMES = { PageEnvelope.AREA,
038: PageEnvelope.CONTEXT, PageEnvelope.PUBLICATION_ID,
039: PageEnvelope.PUBLICATION, PageEnvelope.IS_PUBLICATION,
040: PageEnvelope.PUBLICATION_LANGUAGES_CSV,
041: PageEnvelope.DOCUMENT, PageEnvelope.DOCUMENT_ID,
042: PageEnvelope.DOCUMENT_PARENT, PageEnvelope.DOCUMENT_NAME,
043: PageEnvelope.DOCUMENT_LABEL, PageEnvelope.DOCUMENT_URL,
044: PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE,
045: PageEnvelope.DOCUMENT_PATH,
046: PageEnvelope.DOCUMENT_EXTENSION,
047: PageEnvelope.DOCUMENT_SOURCE_EXTENSION,
048: PageEnvelope.DOCUMENT_UUID, PageEnvelope.DOCUMENT_TYPE,
049: PageEnvelope.DEFAULT_LANGUAGE,
050: PageEnvelope.DOCUMENT_LANGUAGE,
051: PageEnvelope.DOCUMENT_LANGUAGES,
052: PageEnvelope.DOCUMENT_LANGUAGES_CSV,
053: PageEnvelope.DOCUMENT_LASTMODIFIED,
054: PageEnvelope.BREADCRUMB_PREFIX, PageEnvelope.SSL_PREFIX,
055: PageEnvelope.LANGUAGE };
056: /**
057: * <code>PUBLICATION_ID</code> The publication id
058: */
059: public static final String PUBLICATION_ID = "publication-id";
060: /**
061: * <code>PUBLICATION</code> The publication
062: */
063: public static final String PUBLICATION = "publication";
064: /**
065: * <code>IS_PUBLICATION</code> If the current URL refers to a publication ("true" or "false").
066: */
067: public static final String IS_PUBLICATION = "is-publication";
068: /**
069: * <code>PUBLICATION_LANGUAGES_CSV</code> A list of the publication's languages,
070: * comma-seperated
071: */
072: public static final String PUBLICATION_LANGUAGES_CSV = "publication-languages-csv";
073: /**
074: * <code>CONTEXT</code> The context prefix
075: */
076: public static final String CONTEXT = "context-prefix";
077: /**
078: * <code>AREA</code> The area
079: */
080: public static final String AREA = "area";
081: /**
082: * <code>DEFAULT_LANGUAGE</code> The default language of the publication
083: */
084: public static final String DEFAULT_LANGUAGE = "default-language";
085: /**
086: * <code>DOCUMENT</code> The current document
087: */
088: public static final String DOCUMENT = "document";
089: /**
090: * <code>DOCUMENT_ID</code> The document id of the current document
091: */
092: public static final String DOCUMENT_ID = "document-id";
093: /**
094: * <code>DOCUMENT_PARENT</code> The document id of the parent document
095: */
096: public static final String DOCUMENT_PARENT = "document-parent";
097: /**
098: * <code>DOCUMENT_NAME</code> The name of the current document
099: */
100: public static final String DOCUMENT_NAME = "document-name";
101: /**
102: * <code>DOCUMENT_TYPE</code> The type of the current document
103: */
104: public static final String DOCUMENT_TYPE = "document-type";
105: /**
106: * <code>DOCUMENT_LABEL</code> The label of the current document
107: */
108: public static final String DOCUMENT_LABEL = "document-label";
109: /**
110: * <code>DOCUMENT_URL</code> The URL of the current document
111: */
112: public static final String DOCUMENT_URL = "document-url";
113: /**
114: * <code>DOCUMENT_URL_WITHOUT_LANGUAGE</code> The URL of the current document without a
115: * language extension.
116: */
117: public static final String DOCUMENT_URL_WITHOUT_LANGUAGE = "document-url-without-language";
118: /**
119: * <code>DOCUMENT_FILE</code> The file of the current document
120: */
121: public static final String DOCUMENT_FILE = "document-file";
122: /**
123: * <code>DOCUMENT_PATH</code> The path of the current document
124: */
125: public static final String DOCUMENT_PATH = "document-path";
126: /**
127: * <code>DOCUMENT_EXTENSION</code> The extension of the current document
128: */
129: public static final String DOCUMENT_EXTENSION = "document-extension";
130: /**
131: * <code>DOCUMENT_SOURCE_EXTENSION</code> The source extension of the current document
132: */
133: public static final String DOCUMENT_SOURCE_EXTENSION = "document-source-extension";
134: /**
135: * <code>DOCUMENT_UUID</code> The uuid of the current document
136: */
137: public static final String DOCUMENT_UUID = "document-uuid";
138: /**
139: * <code>DOCUMENT_LANGUAGE</code> The language of the current document
140: */
141: public static final String DOCUMENT_LANGUAGE = "document-language";
142: /**
143: * This attribute returns the document language if the document exists, or the
144: * default language otherwise. This makes it suitable for 404 pages.
145: */
146: public static final String LANGUAGE = "language";
147: /**
148: * <code>DOCUMENT_LANGUAGES</code> The languages the current document is available in
149: */
150: public static final String DOCUMENT_LANGUAGES = "document-languages";
151: /**
152: * <code>DOCUMENT_LANGUAGES_CSV</code> The languages the current document is available in,
153: * comma-seperated
154: */
155: public static final String DOCUMENT_LANGUAGES_CSV = "document-languages-csv";
156: /**
157: * <code>DOCUMENT_LASTMODIFIED</code> The last modified date of the current document
158: */
159: public static final String DOCUMENT_LASTMODIFIED = "document-lastmodified";
160: /**
161: * <code>BREADCRUMB_PREFIX</code> The breadcrumb prefix of the publication, used for
162: * navigation
163: */
164: public static final String BREADCRUMB_PREFIX = "breadcrumb-prefix";
165: /**
166: * <code>SSL_PREFIX</code> The SSL prefix of the publication
167: */
168: public static final String SSL_PREFIX = "ssl-prefix";
169: /**
170: * <code>NAMESPACE</code> The page envelope namespace
171: */
172: public static final String NAMESPACE = "http://apache.org/cocoon/lenya/page-envelope/1.0";
173: /**
174: * <code>DEFAULT_PREFIX</code> The default prefix
175: */
176: public static final String DEFAULT_PREFIX = "lenya";
177:
178: private String context;
179: private String area;
180: private Publication publication;
181:
182: /**
183: * Constructor.
184: */
185: protected PageEnvelope() {
186: }
187:
188: /**
189: * Creates a page envelope from an object model.
190: * @param map The identity map to use.
191: * @param contextPath The servlet context prefix.
192: * @param webappUrl The web application URL.
193: * @param servletContext The servlet context directory.
194: * @param publication The publication.
195: * @throws PageEnvelopeException when something went wrong.
196: */
197: public PageEnvelope(DocumentFactory map, String contextPath,
198: String webappUrl, File servletContext,
199: Publication publication) throws PageEnvelopeException {
200: this .factory = map;
201: this .context = contextPath;
202: this .webappUrl = webappUrl;
203: this .publication = publication;
204: }
205:
206: private String webappUrl;
207:
208: private DocumentFactory factory;
209:
210: /**
211: * Returns the document factory.
212: * @return A document factory.
213: */
214: public DocumentFactory getDocumentFactory() {
215: return this .factory;
216: }
217:
218: /**
219: * Creates the message to report when creating the envelope failed.
220: * @param request The request.
221: * @return A string.
222: */
223: protected String createExceptionMessage(Request request) {
224: return "Resolving page envelope failed:" + "\n URI: "
225: + request.getRequestURI() + "\n Context: "
226: + getContext() + "\n Publication ID: "
227: + getPublication().getId() + "\n Area: "
228: + this .document.getArea() + "\n Document UUID: "
229: + this .document.getUUID();
230: }
231:
232: /**
233: * Returns the publication of this PageEnvelope.
234: * @return a <code>Publication</code> value
235: */
236: public Publication getPublication() {
237: /*
238: * if (this.publication == null) { try { Publication pub =
239: * PublicationManagerImpl.getInstance(new ConsoleLogger()) .getPublication(this.webappUrl,
240: * this.servletContext); if (pub.exists()) { this.publication = pub; if
241: * (getIdentityMap().isDocument(this.webappUrl)) { Document _document =
242: * getIdentityMap().getFromURL(this.webappUrl); setDocument(_document); } } } catch
243: * (Exception e) { throw new RuntimeException(e); } }
244: */
245: return this .publication;
246: }
247:
248: /**
249: * @return The current area.
250: */
251: public String getArea() {
252: if (this .area == null) {
253: URLInformation info = new URLInformation(this .webappUrl);
254: this .area = info.getArea();
255: }
256: return this .area;
257: }
258:
259: /**
260: * Returns the rcEnvironment.
261: *
262: * @return a <code>RCEnvironment</code> value
263: * @deprecated We should detach the RC environment from the page envelope.
264: */
265: public RCEnvironment getRCEnvironment() {
266: return RCEnvironment.getInstance(getPublication()
267: .getServletContext().getAbsolutePath(),
268: new ConsoleLogger());
269: }
270:
271: /**
272: * Returns the context, e.g. "/lenya".
273: * @return a <code>String</code> value
274: */
275: public String getContext() {
276: return this .context;
277: }
278:
279: /**
280: * Returns the document-path.
281: * @return a <code>File<code> value
282: */
283: public String getDocumentPath() {
284: return getPublication().getPathMapper().getPath(
285: getDocument().getUUID(), getDocument().getLanguage());
286: }
287:
288: /**
289: * @param string The context.
290: */
291: protected void setContext(String string) {
292: this .context = string;
293: }
294:
295: private Document document;
296: private boolean documentChecked = false;
297:
298: /**
299: * Returns the document or <code>null</code> if the current URL does not represent a document.
300: * @return A document
301: */
302: public Document getDocument() {
303: if (!documentChecked) {
304: try {
305: documentChecked = true;
306: if (getDocumentFactory().isDocument(this .webappUrl)) {
307: this .document = getDocumentFactory().getFromURL(
308: this .webappUrl);
309: }
310: } catch (final Exception e) {
311: throw new RuntimeException(e);
312: }
313: }
314: return this .document;
315: }
316:
317: /**
318: * Sets the document.
319: * @param _document A document.
320: */
321: public void setDocument(Document _document) {
322: this .document = _document;
323: }
324:
325: /**
326: * @return The document language or the default language if the document doesn't exist.
327: * @see #LANGUAGE
328: */
329: public String getLanguage() {
330: Document document = getDocument();
331: if (document == null) {
332: Publication pub = getPublication();
333: if (pub == null) {
334: throw new RuntimeException(
335: "The language attribute can't be used outside a publication.");
336: }
337: return pub.getDefaultLanguage();
338: } else {
339: return document.getLanguage();
340: }
341: }
342:
343: }
|