KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > PageEnvelope


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: PageEnvelope.java 42716 2004-03-16 11:12:16Z gregor $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.util.Map JavaDoc;
23
24 import org.apache.cocoon.environment.ObjectModelHelper;
25 import org.apache.cocoon.environment.Request;
26 import org.apache.lenya.cms.rc.RCEnvironment;
27 import org.apache.lenya.util.ServletHelper;
28
29 /**
30  * A page envelope carries a set of information that are needed during the presentation of a
31  * document.
32  */

33 public class PageEnvelope {
34     public static final String JavaDoc PUBLICATION_ID = "publication-id";
35     public static final String JavaDoc PUBLICATION = "publication";
36     public static final String JavaDoc PUBLICATION_LANGUAGES_CSV = "publication-languages-csv";
37     public static final String JavaDoc CONTEXT = "context-prefix";
38     public static final String JavaDoc AREA = "area";
39     public static final String JavaDoc DEFAULT_LANGUAGE = "default-language";
40     public static final String JavaDoc DOCUMENT = "document";
41     public static final String JavaDoc DOCUMENT_ID = "document-id";
42     public static final String JavaDoc DOCUMENT_NAME = "document-name";
43     public static final String JavaDoc DOCUMENT_TYPE = "document-type";
44     public static final String JavaDoc DOCUMENT_NODE_ID = "document-node-id";
45     public static final String JavaDoc DOCUMENT_LABEL = "document-label";
46     public static final String JavaDoc DOCUMENT_URL = "document-url";
47     public static final String JavaDoc DOCUMENT_URL_WITHOUT_LANGUAGE = "document-url-without-language";
48     public static final String JavaDoc DOCUMENT_FILE = "document-file";
49     public static final String JavaDoc DOCUMENT_PATH = "document-path";
50     public static final String JavaDoc DOCUMENT_EXTENSION = "document-extension";
51     public static final String JavaDoc DOCUMENT_LANGUAGE = "document-language";
52     public static final String JavaDoc DOCUMENT_LANGUAGES = "document-languages";
53     public static final String JavaDoc DOCUMENT_LANGUAGES_CSV = "document-languages-csv";
54     
55     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
56     public static final String JavaDoc DOCUMENT_DC_TITLE = "document-dc-title";
57     
58     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
59     public static final String JavaDoc DOCUMENT_DC_CREATOR = "document-dc-creator";
60     
61     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
62     public static final String JavaDoc DOCUMENT_DC_SUBJECT = "document-dc-subject";
63     
64     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
65     public static final String JavaDoc DOCUMENT_DC_PUBLISHER = "document-dc-publisher";
66     
67     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
68     public static final String JavaDoc DOCUMENT_DC_DATE_CREATED = "document-dc-date-created";
69     
70     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
71     public static final String JavaDoc DOCUMENT_DC_DESCRIPTION = "document-dc-description";
72     
73     /** @deprecated Use {@link org.apache.lenya.cms.publication.Document#getDublinCore()} instead */
74     public static final String JavaDoc DOCUMENT_DC_RIGHTS = "document-dc-rights";
75     
76     public static final String JavaDoc DOCUMENT_LASTMODIFIED = "document-lastmodified";
77
78     public static final String JavaDoc BREADCRUMB_PREFIX = "breadcrumb-prefix";
79
80     public static final String JavaDoc SSL_PREFIX = "ssl-prefix";
81
82     public static final String JavaDoc NAMESPACE = "http://apache.org/cocoon/lenya/page-envelope/1.0";
83     public static final String JavaDoc DEFAULT_PREFIX = "lenya";
84
85     private String JavaDoc context;
86
87     /**
88      * Constructor.
89      */

90     protected PageEnvelope() {
91     }
92
93     /**
94      * Creates a new instance of PageEnvelope from a sitemap inside a publication.
95      *
96      * @param publication
97      * The publication the page belongs to.
98      * @param request
99      * The request that calls the page.
100      * @exception PageEnvelopeException
101      * if an error occurs
102      * @deprecated Performance problems. Use {@link PageEnvelopeFactory#getPageEnvelope(Map)}
103      * instead.
104      */

105     public PageEnvelope(Publication publication, Request request) throws PageEnvelopeException {
106         init(publication, request);
107     }
108
109     /**
110      * Creates a page envelope from an object model.
111      *
112      * @param objectModel
113      * The object model.
114      * @throws PageEnvelopeException
115      * when something went wrong.
116      * @deprecated Performance problems. Use {@link PageEnvelopeFactory#getPageEnvelope(Map)}
117      * instead.
118      */

119     public PageEnvelope(Map JavaDoc objectModel) throws PageEnvelopeException {
120         try {
121             init(
122                 PublicationFactory.getPublication(objectModel),
123                 ObjectModelHelper.getRequest(objectModel));
124         } catch (PublicationException e) {
125             throw new PageEnvelopeException(e);
126         }
127     }
128
129     /**
130      * Creates a new instance of PageEnvelope from a sitemap inside a publication.
131      *
132      * @param publication The publication the page belongs to.
133      * @param request The request that calls the page.
134      * @param createdByFactory
135      * A dummy parameter to allow creating an additional protected constructor that is
136      * not deprecated.
137      * @exception PageEnvelopeException if an error occurs
138      */

139     public PageEnvelope(Publication publication, Request request, boolean createdByFactory)
140         throws PageEnvelopeException {
141         this(publication, request);
142     }
143
144     /**
145      * Creates a page envelope from an object model.
146      *
147      * @param objectModel
148      * The object model.
149      * @param createdByFactory
150      * A dummy parameter to allow creating an additional protected constructor that is
151      * not deprecated.
152      * @throws PageEnvelopeException
153      * when something went wrong.
154      */

155     protected PageEnvelope(Map JavaDoc objectModel, boolean createdByFactory)
156         throws PageEnvelopeException {
157         this(objectModel);
158     }
159
160     /**
161      * Setup an instance of Publication.
162      *
163      * Shared by multiple constructors.
164      *
165      * @param publication
166      * The publication the page belongs to.
167      * @param request
168      * The request that calls the page.
169      *
170      * @throws PageEnvelopeException
171      * if an error occurs.
172      */

173     protected void init(Publication publication, Request request)
174     // FIXME: this method is mainly needed because the deprecated
175
// constructor PageEnvelope(Map objectModel) needs to handle an exception in
176
// one of the arguments to another constructor. That's why the constructor
177
// functionality is factored out into this method.
178
// If the deprecated constructor PageEnvelope(Map objectModel) is removed
179
// this method might not be needed anymore and the functionality could
180
// be moved back to the constructor PageEnvelope(Publication publication, Request request).
181
throws PageEnvelopeException {
182         assert publication != null;
183         assert request != null;
184         String JavaDoc webappURI;
185         try {
186
187             context = request.getContextPath();
188             if (context == null) {
189                 context = "";
190             }
191
192             webappURI = ServletHelper.getWebappURI(request);
193             Document document =
194                 publication.getDocumentBuilder().buildDocument(publication, webappURI);
195             setDocument(document);
196
197         } catch (Exception JavaDoc e) {
198             throw new PageEnvelopeException(e);
199         }
200
201         // plausibility check
202
/*
203          * if (!webappURI .startsWith( "/" + getPublication().getId() + "/" + document.getArea() +
204          * document.getId())) { throw new PageEnvelopeException(createExceptionMessage(request)); }
205          */

206     }
207
208     /**
209      * Creates the message to report when creating the envelope failed.
210      *
211      * @param request
212      * The request.
213      * @return A string.
214      */

215     protected String JavaDoc createExceptionMessage(Request request) {
216         return "Resolving page envelope failed:"
217             + "\n URI: "
218             + request.getRequestURI()
219             + "\n Context: "
220             + getContext()
221             + "\n Publication ID: "
222             + getPublication().getId()
223             + "\n Area: "
224             + document.getArea()
225             + "\n Document ID: "
226             + document.getId();
227     }
228
229     /**
230      * Returns the publication of this PageEnvelope.
231      *
232      * @return a <code>Publication</code> value
233      */

234     public Publication getPublication() {
235         return getDocument().getPublication();
236     }
237
238     /**
239      * Returns the rcEnvironment.
240      *
241      * @return a <code>RCEnvironment</code> value
242      * @deprecated We should detach the RC environment from the page envelope.
243      */

244     public RCEnvironment getRCEnvironment() {
245         return RCEnvironment.getInstance(getPublication().getServletContext().getAbsolutePath());
246     }
247
248     /**
249      * Returns the context, e.g. "/lenya".
250      *
251      * @return a <code>String</code> value
252      */

253     public String JavaDoc getContext() {
254         return context;
255     }
256
257     /**
258      * Returns the document-path.
259      *
260      * @return a <code>File<code> value
261      */

262     public String JavaDoc getDocumentPath() {
263
264         return getPublication().getPathMapper().getPath(
265             getDocument().getId(),
266             getDocument().getLanguage());
267     }
268
269     /**
270      * The names of the page envelope parameters.
271      */

272     public static final String JavaDoc[] PARAMETER_NAMES =
273         {
274             PageEnvelope.AREA,
275             PageEnvelope.CONTEXT,
276             PageEnvelope.PUBLICATION_ID,
277             PageEnvelope.PUBLICATION,
278             PageEnvelope.PUBLICATION_LANGUAGES_CSV,
279             PageEnvelope.DOCUMENT,
280             PageEnvelope.DOCUMENT_ID,
281             PageEnvelope.DOCUMENT_NAME,
282             PageEnvelope.DOCUMENT_NODE_ID,
283             PageEnvelope.DOCUMENT_LABEL,
284             PageEnvelope.DOCUMENT_URL,
285             PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE,
286             PageEnvelope.DOCUMENT_PATH,
287             PageEnvelope.DOCUMENT_EXTENSION,
288             PageEnvelope.DEFAULT_LANGUAGE,
289             PageEnvelope.DOCUMENT_LANGUAGE,
290             PageEnvelope.DOCUMENT_LANGUAGES,
291             PageEnvelope.DOCUMENT_LANGUAGES_CSV,
292             PageEnvelope.DOCUMENT_DC_TITLE,
293             PageEnvelope.DOCUMENT_DC_CREATOR,
294             PageEnvelope.DOCUMENT_DC_PUBLISHER,
295             PageEnvelope.DOCUMENT_DC_SUBJECT,
296             PageEnvelope.DOCUMENT_DC_DATE_CREATED,
297             PageEnvelope.DOCUMENT_DC_DESCRIPTION,
298             PageEnvelope.DOCUMENT_DC_RIGHTS,
299             PageEnvelope.DOCUMENT_LASTMODIFIED,
300             PageEnvelope.BREADCRUMB_PREFIX,
301             PageEnvelope.SSL_PREFIX };
302
303     /**
304      * @param string
305      * The context.
306      */

307     protected void setContext(String JavaDoc string) {
308         context = string;
309     }
310
311     private Document document;
312
313     /**
314      * Returns the document.
315      *
316      * @return A document
317      */

318     public Document getDocument() {
319         return document;
320     }
321
322     /**
323      * Sets the document.
324      *
325      * @param document
326      * A document.
327      */

328     public void setDocument(Document document) {
329         this.document = document;
330     }
331
332 }
333
Popular Tags