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:$ */
020:
021: package org.apache.lenya.cms.cocoon.components.modules.input;
022:
023: import java.text.SimpleDateFormat;
024: import java.util.Date;
025: import java.util.Map;
026:
027: import org.apache.avalon.framework.configuration.Configuration;
028: import org.apache.avalon.framework.configuration.ConfigurationException;
029: import org.apache.avalon.framework.service.ServiceException;
030: import org.apache.avalon.framework.service.ServiceManager;
031: import org.apache.avalon.framework.service.Serviceable;
032: import org.apache.cocoon.components.modules.input.AbstractInputModule;
033: import org.apache.cocoon.environment.ObjectModelHelper;
034: import org.apache.cocoon.environment.Request;
035: import org.apache.lenya.cms.publication.Document;
036: import org.apache.lenya.cms.publication.DocumentException;
037: import org.apache.lenya.cms.publication.DocumentFactory;
038: import org.apache.lenya.cms.publication.DocumentUtil;
039: import org.apache.lenya.cms.publication.Publication;
040: import org.apache.lenya.cms.repository.RepositoryUtil;
041: import org.apache.lenya.cms.repository.Session;
042:
043: /**
044: * Input module to get document information.
045: * {doc-info:{publication-id}:{area}:{uuid}:{document-language}:{property}} where {property} may be:
046: * <ul>
047: * <li><strong><code>contentLength</code></strong> - the content length (number of bytes).</li>
048: * <li><strong><code>expires</code></strong> - the expiration date in ISO 8601 format.</li>
049: * <li><strong><code>lastModified</code></strong> - the last modification date in ISO 8601
050: * format.</li>
051: * <li><strong><code>mimeType</code></strong> - the MIME type</li>
052: * <li><strong><code>nodeName</code></strong> - the name of the node in the site structure</li>
053: * <li><strong><code>path</code></strong> - the path in the site structure (starting with a
054: * slash) or an empty string if the document is not referenced in the site structure.</li>
055: * <li><strong><code>resourceType</code></strong> - the name of the resource type</li>
056: * <li><strong><code>sourceExtension</code></strong> - the source extension</li>
057: * <li><strong><code>visibleInNav</code></strong> - <code>true</code> if the document's node
058: * is visible in the navigation, <code>false</code> otherwise.</li>
059: * <li><strong><code>webappUrl</code></strong> - the web application URL of the document or
060: * an empty string if the document is not referenced in the site structure.</li>
061: * </ul>
062: */
063: public class DocumentInfoModule extends AbstractInputModule implements
064: Serviceable {
065:
066: protected ServiceManager manager;
067:
068: // Input module parameters:
069: protected final static String PARAM_PUBLICATION_ID = "publication-id";
070: protected final static String PARAM_AREA = "area";
071: protected final static String PARAM_UUID = "uuid";
072: protected final static String PARAM_DOCUMENT_LANGUAGE = "document-language";
073: protected final static String PARAM_PROPERTY = "property";
074: protected final static String PARAM_REVISION = "revision";
075: protected final static int MIN_MANDATORY_PARAMS = 5;
076:
077: protected final static String UUID = "uuid";
078: protected final static String LANGUAGE = "language";
079: protected final static String PATH = "path";
080: protected final static String NODE_NAME = "nodeName";
081: protected final static String WEBAPP_URL = "webappUrl";
082: protected final static String DOCUMENT_URL = "documentUrl";
083: protected final static String RESOURCE_TYPE = "resourceType";
084: protected final static String LAST_MODIFIED = "lastModified";
085: protected final static String MIME_TYPE = "mimeType";
086: protected final static String CONTENT_LENGTH = "contentLength";
087: protected final static String SOURCE_EXTENSION = "sourceExtension";
088: protected final static String EXPIRES = "expires";
089: protected final static String VISIBLE_IN_NAVIGATION = "visibleInNav";
090:
091: protected final static String[] PARAMS = { PARAM_PUBLICATION_ID,
092: PARAM_AREA, PARAM_UUID, PARAM_DOCUMENT_LANGUAGE,
093: PARAM_PROPERTY, PARAM_REVISION };
094:
095: protected final static String META_RESOURCE_TYPE = "resourceType";
096: protected final static String META_EXPIRES = "expires";
097:
098: protected SimpleDateFormat dateFormat = new SimpleDateFormat(
099: "yyyy-MM-dd HH:mm:ss Z");
100:
101: /**
102: * Parse the parameters and return a document.
103: * @param publicationId The publication ID.
104: * @param area The area.
105: * @param uuid The document UUID.
106: * @param language The document language.
107: * @param revision The revision.
108: * @param objectModel The object model.
109: * @return The document object created.
110: * @throws ConfigurationException
111: */
112: protected Document getDocument(String publicationId, String area,
113: String uuid, String language, int revision, Map objectModel)
114: throws ConfigurationException {
115: Document document = null;
116:
117: Request request = ObjectModelHelper.getRequest(objectModel);
118:
119: try {
120: Session session = RepositoryUtil.getSession(this .manager,
121: request);
122: DocumentFactory docFactory = DocumentUtil
123: .createDocumentFactory(this .manager, session);
124: Publication pub = docFactory.getPublication(publicationId);
125: document = docFactory.get(pub, area, uuid, language,
126: revision);
127: } catch (Exception e) {
128: throw new ConfigurationException("Error getting document ["
129: + publicationId + ":" + area + ":" + uuid + ":"
130: + language + "]: " + e.getMessage(), e);
131: }
132: return document;
133: }
134:
135: /**
136: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String,
137: * org.apache.avalon.framework.configuration.Configuration, java.util.Map)
138: */
139: public Object getAttribute(String name, Configuration modeConf,
140: Map objectModel) throws ConfigurationException {
141: Object value = null;
142:
143: InputModuleParameters params = new InputModuleParameters(name,
144: PARAMS, MIN_MANDATORY_PARAMS);
145:
146: try {
147: int rev = -1;
148: if (params.isParameter(PARAM_REVISION)) {
149: String revision = params.getParameter(PARAM_REVISION);
150: if (!revision.equals("")) {
151: rev = Integer.valueOf(revision).intValue();
152: }
153: }
154:
155: Document document = getDocument(params
156: .getParameter(PARAM_PUBLICATION_ID), params
157: .getParameter(PARAM_AREA), params
158: .getParameter(PARAM_UUID), params
159: .getParameter(PARAM_DOCUMENT_LANGUAGE), rev,
160: objectModel);
161:
162: String attribute = params.getParameter(PARAM_PROPERTY);
163:
164: if (attribute.equals(RESOURCE_TYPE)) {
165: value = document.getResourceType().getName();
166: } else if (attribute.equals(LAST_MODIFIED)) {
167: value = this .dateFormat.format(new Date(document
168: .getLastModified()));
169: } else if (attribute.equals(MIME_TYPE)) {
170: value = document.getMimeType();
171: } else if (attribute.equals(CONTENT_LENGTH)) {
172: value = Long.toString(document.getContentLength());
173: } else if (attribute.equals(SOURCE_EXTENSION)) {
174: value = document.getSourceExtension();
175: } else if (attribute.equals(LANGUAGE)) {
176: value = document.getLanguage();
177: } else if (attribute.equals(PATH)) {
178: value = document.getPath();
179: } else if (attribute.equals(NODE_NAME)) {
180: value = document.getName();
181: } else if (attribute.equals(UUID)) {
182: value = document.getUUID();
183: } else if (attribute.equals(WEBAPP_URL)) {
184: value = document.getCanonicalWebappURL();
185: } else if (attribute.equals(DOCUMENT_URL)) {
186: value = document.getCanonicalDocumentURL();
187: } else if (attribute.equals(EXPIRES)) {
188: try {
189: Date expires = document.getExpires();
190: value = this .dateFormat.format(expires);
191: } catch (DocumentException e) {
192: throw new ConfigurationException(
193: "Error getting expires date from document.",
194: e);
195: }
196: } else if (attribute.equals(VISIBLE_IN_NAVIGATION)) {
197: value = Boolean
198: .toString(isVisibleInNavigation(document));
199: } else {
200: throw new ConfigurationException("Attribute '"
201: + attribute + "' not supported [" + name + "]");
202: }
203: } catch (ConfigurationException e) {
204: throw e;
205: } catch (Exception e) {
206: throw new ConfigurationException(
207: "Error getting input module parameters.", e);
208: }
209:
210: return value;
211: }
212:
213: protected boolean isVisibleInNavigation(Document document)
214: throws ConfigurationException {
215: try {
216: return document.getLink().getNode().isVisible();
217: } catch (DocumentException e) {
218: throw new ConfigurationException(
219: "Obtaining navigation visibility failed ["
220: + document + "]: " + e.getMessage(), e);
221: }
222:
223: }
224:
225: public void service(ServiceManager manager) throws ServiceException {
226: this.manager = manager;
227: }
228: }
|