KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > components > modules > input > PageEnvelopeModule


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: PageEnvelopeModule.java 43041 2004-05-23 17:27:06Z roku $ */
19
20 package org.apache.lenya.cms.cocoon.components.modules.input;
21
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.parameters.Parameters;
31 import org.apache.avalon.framework.service.ServiceException;
32 import org.apache.avalon.framework.service.ServiceManager;
33 import org.apache.avalon.framework.service.Serviceable;
34 import org.apache.cocoon.environment.ObjectModelHelper;
35 import org.apache.cocoon.environment.Request;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.lenya.cms.cocoon.uriparameterizer.URIParameterizer;
38 import org.apache.lenya.cms.cocoon.uriparameterizer.URIParameterizerException;
39 import org.apache.lenya.cms.publication.DublinCore;
40 import org.apache.lenya.cms.publication.PageEnvelope;
41 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
42
43 /**
44  * Input module wrapping the page envelope. This module provides publication
45  * related information such as document-id, area, publication-id.
46  *
47  * @see org.apache.lenya.cms.publication.PageEnvelope
48  */

49 public class PageEnvelopeModule extends AbstractPageEnvelopeModule implements Serviceable {
50
51     private ServiceManager manager;
52
53     /**
54      * Get the the page envelope for the given objectModel.
55      *
56      * @param objectModel the objectModel for which the page enevelope is requested.
57      *
58      * @return a <code>PageEnvelope</code>
59      *
60      * @throws ConfigurationException if the page envelope could not be instantiated.
61      */

62     protected PageEnvelope getEnvelope(Map JavaDoc objectModel) throws ConfigurationException {
63         PageEnvelope envelope = null;
64
65         if (getLogger().isDebugEnabled()) {
66             Request request = ObjectModelHelper.getRequest(objectModel);
67             getLogger().debug("Resolving page envelope for URL [" + request.getRequestURI() + "]");
68         }
69
70         try {
71             envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel);
72         } catch (Exception JavaDoc e) {
73             throw new ConfigurationException("Resolving page envelope failed: ", e);
74         }
75
76         return envelope;
77     }
78
79     protected static final String JavaDoc URI_PARAMETER_DOCTYPE = "doctype";
80
81     /**
82      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
83      */

84     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
85         throws ConfigurationException {
86         PageEnvelope envelope = getEnvelope(objectModel);
87         Object JavaDoc value = null;
88
89         try {
90             if (name.equals(PageEnvelope.AREA)) {
91                 value = envelope.getDocument().getArea();
92             } else if (name.equals(PageEnvelope.CONTEXT)) {
93                 value = envelope.getContext();
94             } else if (name.equals(PageEnvelope.PUBLICATION_ID)) {
95                 value = envelope.getPublication().getId();
96             } else if (name.equals(PageEnvelope.PUBLICATION)) {
97                 value = envelope.getPublication();
98             } else if (name.equals(PageEnvelope.PUBLICATION_LANGUAGES_CSV)) {
99                 value =
100                     StringUtils.join(
101                         envelope.getPublication().getLanguages(),
102                         ',');
103             } else if (name.equals(PageEnvelope.DOCUMENT)) {
104                 value = envelope.getDocument();
105             } else if (name.equals(PageEnvelope.DOCUMENT_ID)) {
106                 value = envelope.getDocument().getId();
107             } else if (name.equals(PageEnvelope.DOCUMENT_NAME)) {
108                 value = envelope.getDocument().getName();
109             } else if (name.equals(PageEnvelope.DOCUMENT_NODE_ID)) { // FIXME: Why is this here?
110
value = envelope.getDocument().getNodeId();
111             } else if (name.equals(PageEnvelope.DOCUMENT_LABEL)) { // FIXME: Why is this here?
112
value = envelope.getDocument().getLabel();
113             } else if (name.equals(PageEnvelope.DOCUMENT_URL)) {
114                 value = envelope.getDocument().getDocumentURL();
115             } else if (name.equals(PageEnvelope.DOCUMENT_URL_WITHOUT_LANGUAGE)) {
116                 value = envelope.getDocument().getCompleteURLWithoutLanguage();
117             } else if (name.equals(PageEnvelope.DOCUMENT_PATH)) {
118                 value = envelope.getDocumentPath();
119             } else if (name.equals(PageEnvelope.DOCUMENT_FILE)) {
120                 value = envelope.getDocument().getFile();
121             } else if (name.equals(PageEnvelope.DOCUMENT_EXTENSION)) {
122                 value = envelope.getDocument().getExtension();
123             } else if (name.equals(PageEnvelope.DEFAULT_LANGUAGE)) {
124                 value = envelope.getPublication().getDefaultLanguage();
125             } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGE)) {
126                 value = envelope.getDocument().getLanguage();
127             } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES)) {
128                 value = envelope.getDocument().getLanguages();
129             } else if (name.equals(PageEnvelope.DOCUMENT_LANGUAGES_CSV)) {
130                 value =
131                     StringUtils.join(
132                         envelope.getDocument().getLanguages(),
133                         ',');
134             } else if (name.equals(PageEnvelope.DOCUMENT_DC_TITLE)) {
135                 value =
136                     envelope.getDocument().getDublinCore().getFirstValue(DublinCore.ELEMENT_TITLE);
137             } else if (name.equals(PageEnvelope.DOCUMENT_DC_CREATOR)) {
138                 value =
139                     envelope.getDocument().getDublinCore().getFirstValue(
140                         DublinCore.ELEMENT_CREATOR);
141             } else if (name.equals(PageEnvelope.DOCUMENT_DC_PUBLISHER)) {
142                 value =
143                     envelope.getDocument().getDublinCore().getFirstValue(
144                         DublinCore.ELEMENT_PUBLISHER);
145             } else if (name.equals(PageEnvelope.DOCUMENT_DC_SUBJECT)) {
146                 value =
147                     envelope.getDocument().getDublinCore().getFirstValue(
148                         DublinCore.ELEMENT_SUBJECT);
149             } else if (name.equals(PageEnvelope.DOCUMENT_DC_DESCRIPTION)) {
150                 value =
151                     envelope.getDocument().getDublinCore().getFirstValue(
152                         DublinCore.ELEMENT_DESCRIPTION);
153             } else if (name.equals(PageEnvelope.DOCUMENT_DC_RIGHTS)) {
154                 value =
155                     envelope.getDocument().getDublinCore().getFirstValue(DublinCore.ELEMENT_RIGHTS);
156             } else if (name.equals(PageEnvelope.DOCUMENT_DC_DATE_CREATED)) {
157                 value =
158                     envelope.getDocument().getDublinCore().getFirstValue(DublinCore.TERM_CREATED);
159             } else if (name.equals(PageEnvelope.DOCUMENT_LASTMODIFIED)) {
160                 Date JavaDoc date = envelope.getDocument().getLastModified();
161                 value = new SimpleDateFormat JavaDoc(DATE_FORMAT).format(date);
162             } else if (name.equals(PageEnvelope.BREADCRUMB_PREFIX)) {
163                 value = envelope.getPublication().getBreadcrumbPrefix();
164             } else if (name.equals(PageEnvelope.SSL_PREFIX)) {
165                 value = envelope.getPublication().getSSLPrefix();
166             } else if (name.equals(PageEnvelope.DOCUMENT_TYPE)) {
167                 value = getDocumentType(objectModel, envelope);
168             } else {
169                 throw new ConfigurationException("The attribute [" + name + "] is not supported!");
170             }
171         } catch (ConfigurationException e) {
172             throw e;
173         } catch (Exception JavaDoc e) {
174             throw new ConfigurationException(
175                 "Getting attribute for name [" + name + "] failed: ",
176                 e);
177         }
178
179         if (getLogger().isDebugEnabled()) {
180             getLogger().debug("Returning [" + name + "] = [" + value + "]");
181         }
182
183         return value;
184     }
185
186     public static final String JavaDoc DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
187
188     /**
189      * Returns the document type.
190      * @param objectModel The object model.
191      * @param envelope The page envelope.
192      * @return A string.
193      * @throws ServiceException when something went wrong.
194      * @throws URIParameterizerException when something went wrong.
195      */

196     protected String JavaDoc getDocumentType(Map JavaDoc objectModel, PageEnvelope envelope)
197         throws ServiceException, URIParameterizerException {
198         String JavaDoc documentType;
199         URIParameterizer parameterizer = null;
200         Map JavaDoc map = null;
201         try {
202             parameterizer = (URIParameterizer) manager.lookup(URIParameterizer.ROLE);
203
204             Parameters parameters = new Parameters();
205             parameters.setParameter(
206                 URI_PARAMETER_DOCTYPE,
207                 "cocoon://uri-parameter/"
208                     + envelope.getPublication().getId()
209                     + "/"
210                     + URI_PARAMETER_DOCTYPE);
211
212             String JavaDoc source =
213                 envelope.getDocument().getArea() + envelope.getDocument().getDocumentURL();
214
215             Request request = ObjectModelHelper.getRequest(objectModel);
216             map = parameterizer.parameterize(filterURI(request.getRequestURI()), filterURI(source), parameters);
217             documentType = (String JavaDoc) map.get(URI_PARAMETER_DOCTYPE);
218             
219         } finally {
220             if (parameterizer != null) {
221                 manager.release(parameterizer);
222             }
223         }
224         return documentType;
225     }
226
227     /**
228      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
229      */

230     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
231         throws ConfigurationException {
232         return Arrays.asList(PageEnvelope.PARAMETER_NAMES).iterator();
233     }
234
235     /**
236      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
237      */

238     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
239         throws ConfigurationException {
240         Object JavaDoc[] objects = { getAttribute(name, modeConf, objectModel)};
241
242         return objects;
243     }
244
245     /**
246      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
247      */

248     public void service(ServiceManager manager) throws ServiceException {
249         this.manager = manager;
250     }
251     
252     /**
253      * uri will be filtered by certain rules
254      * i.e. session information encoded within the uri will be removed.
255      * @param uri The uri to be filtered
256      * @return uri filtered by certain rules i.e
257      */

258     // FIXME Maybe make this more configureable
259
private String JavaDoc filterURI(final String JavaDoc uri)
260     {
261         final int index = uri.indexOf(";jsessionid");
262         
263         if(index >= 0)
264             return uri.substring(0, index);
265         else
266             return uri;
267     }
268 }
269
Popular Tags