KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > XopusHandlerAction


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: XopusHandlerAction.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.avalon.excalibur.io.FileUtil;
29 import org.apache.avalon.framework.configuration.Configuration;
30 import org.apache.avalon.framework.configuration.ConfigurationException;
31 import org.apache.avalon.framework.parameters.Parameters;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.cocoon.ProcessingException;
34 import org.apache.cocoon.acting.ConfigurableServiceableAction;
35 import org.apache.cocoon.environment.ObjectModelHelper;
36 import org.apache.cocoon.environment.Redirector;
37 import org.apache.cocoon.environment.Session;
38 import org.apache.cocoon.environment.SourceResolver;
39 import org.apache.cocoon.environment.http.HttpRequest;
40 import org.apache.cocoon.util.IOUtils;
41 import org.apache.cocoon.util.PostInputStream;
42 import org.apache.excalibur.source.Source;
43 import org.apache.excalibur.xml.dom.DOMParser;
44 import org.apache.lenya.ac.Identity;
45 import org.apache.lenya.ac.User;
46 import org.apache.lenya.cms.rc.RevisionController;
47 import org.apache.lenya.xml.DOMParserFactory;
48 import org.apache.lenya.xml.DOMWriter;
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Element JavaDoc;
51 import org.xml.sax.InputSource JavaDoc;
52 import org.xml.sax.SAXException JavaDoc;
53
54 /**
55  * Interfaces with Xopus: handles the requests and replies to them
56  */

57 public class XopusHandlerAction extends ConfigurableServiceableAction {
58     private String JavaDoc xmlRoot = null;
59     private String JavaDoc xslRoot = null;
60     private String JavaDoc xsdRoot = null;
61     private String JavaDoc tempRoot = null;
62     private Map JavaDoc relRootDirs = new HashMap JavaDoc();
63     private String JavaDoc rcmlDirectory = null;
64     private String JavaDoc backupDirectory = null;
65
66     /**
67      * Gets the configuration from the sitemap
68      *
69      * @param conf DOCUMENT ME!
70      *
71      * @throws ConfigurationException DOCUMENT ME!
72      */

73     public void configure(Configuration conf) throws ConfigurationException {
74         super.configure(conf);
75         xmlRoot = conf.getChild("xml").getAttribute("href");
76         xslRoot = conf.getChild("xsl").getAttribute("href");
77         xsdRoot = conf.getChild("xsd").getAttribute("href");
78         tempRoot = conf.getChild("temp").getAttribute("href");
79         getLogger().debug(
80             ".configure(): \n"
81                 + "Relative XML Root Directory: "
82                 + xmlRoot
83                 + "\n"
84                 + "Relative XSL Root Directory: "
85                 + xslRoot
86                 + "\n"
87                 + "Relative XSD Root Directory: "
88                 + xsdRoot
89                 + "\n"
90                 + "Relative Temp Directory: "
91                 + tempRoot);
92
93         // Encode File types and their root directories, relative to the sitemap directory
94
relRootDirs.put("xml", xmlRoot);
95         relRootDirs.put("xsl", xslRoot);
96         relRootDirs.put("xsd", xsdRoot);
97         relRootDirs.put("temp", tempRoot);
98
99         // Revision Control Parameters
100
rcmlDirectory = conf.getChild("rcmlDirectory").getAttribute("href");
101         backupDirectory = conf.getChild("backupDirectory").getAttribute("href");
102     }
103
104     /**
105      * DOCUMENT ME!
106      *
107      * @param redirector DOCUMENT ME!
108      * @param resolver DOCUMENT ME!
109      * @param objectModel DOCUMENT ME!
110      * @param source DOCUMENT ME!
111      * @param params DOCUMENT ME!
112      *
113      * @return DOCUMENT ME!
114      *
115      * @throws IOException DOCUMENT ME!
116      * @throws ComponentException DOCUMENT ME!
117      * @throws SAXException DOCUMENT ME!
118      * @throws ProcessingException DOCUMENT ME!
119      */

120     public java.util.Map JavaDoc act(
121         Redirector redirector,
122         SourceResolver resolver,
123         Map JavaDoc objectModel,
124         String JavaDoc source,
125         Parameters params)
126         throws IOException JavaDoc, SAXException JavaDoc, ProcessingException, ServiceException {
127         // Get absolute path of sitemap directory
128
Source input_source = resolver.resolveURI("");
129         String JavaDoc sitemapPath = input_source.getURI();
130         sitemapPath = sitemapPath.substring(5); // Remove "file:" protocol
131
getLogger().debug(".act(): Absolute Sitemap Directory: " + sitemapPath);
132         getLogger().debug(".act(): Absolute XML Root Directory: " + sitemapPath + xmlRoot);
133         getLogger().debug(".act(): Absolute XSL Root Directory: " + sitemapPath + xslRoot);
134         getLogger().debug(".act(): Absolute XSD Root Directory: " + sitemapPath + xsdRoot);
135         getLogger().debug(".act(): Absolute Temp Root Directory: " + sitemapPath + tempRoot);
136
137         // Get request object
138
HttpRequest httpReq = (HttpRequest) objectModel.get(ObjectModelHelper.REQUEST_OBJECT);
139
140         if (httpReq == null) {
141             getLogger().error("Could not get HTTP_REQUEST_OBJECT from objectModel");
142
143             return null;
144         }
145
146         int length = httpReq.getContentLength();
147         PostInputStream reqContent = new PostInputStream(httpReq.getInputStream(), length);
148
149         // construct DOM document from the request contents
150
DOMParser parser = (DOMParser) this.manager.lookup(DOMParser.ROLE);
151         InputSource JavaDoc saxSource = new InputSource JavaDoc(reqContent);
152         Document JavaDoc requestDoc = parser.parseDocument(saxSource);
153
154         // get the root element (should be "request") and its attributes ---> FixMe: Add error handling
155
Element JavaDoc root = requestDoc.getDocumentElement();
156         getLogger().debug(".act(): Root element (should be 'request'): " + root.getTagName());
157
158         String JavaDoc reqId = root.getAttribute("id");
159         getLogger().debug(".act(): Request ID: " + reqId);
160
161         String JavaDoc reqType = root.getAttribute("type");
162         getLogger().debug(".act(): Request Type: " + reqType);
163
164         // get the first child element for root element (should be "data") and its attributes ---> FixMe: Add error handling
165
Element JavaDoc data = (Element JavaDoc) root.getFirstChild();
166         getLogger().debug(".act(): first child element (should be 'data'): " + data.getTagName());
167
168         String JavaDoc reqFile = data.getAttribute("id");
169         getLogger().debug(".act(): Requested File: " + reqFile);
170
171         String JavaDoc fileType = data.getAttribute("type");
172         getLogger().debug(".act(): Requested File's Type: " + fileType);
173
174         // close the input stream
175
reqContent.close();
176
177         // Define Files
178
File JavaDoc tempFileDir =
179             new File JavaDoc(sitemapPath + relRootDirs.get("temp") + "/" + relRootDirs.get(fileType));
180
181         if (!(tempFileDir.exists())) {
182             tempFileDir.mkdir();
183         }
184
185         File JavaDoc tempFile = IOUtils.createFile(tempFileDir, reqFile);
186         File JavaDoc permFile = new File JavaDoc(sitemapPath + relRootDirs.get(fileType) + "/" + reqFile);
187
188         if (!permFile.exists()) {
189             getLogger().error(".act(): No such file: " + permFile.getAbsolutePath());
190             getLogger().error(
191                 ".act(): No such file: "
192                     + sitemapPath
193                     + "::"
194                     + relRootDirs.get(fileType)
195                     + "::"
196                     + reqFile);
197
198             return null;
199         }
200
201         // make a temporary copy of the file to be edited
202
if ("xml".equals(fileType) && "open".equals(reqType)) {
203             FileUtil.copyFile(permFile, tempFile);
204             getLogger().debug(".act(): PERMANENT FILE: " + permFile.getAbsolutePath());
205             getLogger().debug(".act(): TEMPORARY FILE: " + tempFile.getAbsolutePath());
206         }
207
208         // set sitemap params for response routing
209
Map JavaDoc sitemapParams = new HashMap JavaDoc();
210         sitemapParams.put("reqId", reqId);
211         sitemapParams.put("reqType", reqType);
212         sitemapParams.put("reqFile", reqFile);
213         sitemapParams.put("fileType", fileType);
214
215         if ("xml".equals(fileType) && ("open".equals(reqType) || "save".equals(reqType))) {
216             sitemapParams.put(
217                 "reqFilePath",
218                 (String JavaDoc) relRootDirs.get("temp")
219                     + "/"
220                     + (String JavaDoc) relRootDirs.get(fileType)
221                     + "/"
222                     + reqFile);
223             getLogger().debug(
224                 ".act(): File to be edited (in temp dir): " + sitemapParams.get("reqFilePath"));
225         } else {
226             sitemapParams.put("reqFilePath", (String JavaDoc) relRootDirs.get(fileType) + "/" + reqFile);
227         }
228
229         // The xopus sitemap will return the XML
230
if ("open".equals(reqType)) {
231             return sitemapParams;
232         }
233
234         // save to temporary file, if needed
235
if ("save".equals(reqType) || "checkin".equals(reqType)) {
236             getLogger().debug(".act(): Write to temp file: " + tempFile);
237
238             try {
239                 Element JavaDoc contentNode = (Element JavaDoc) data.getFirstChild();
240                 DOMParserFactory dpf = new DOMParserFactory();
241
242                 // Create a new document, where the actual content starts at the root element, which is the inner part of requestDoc
243
Document JavaDoc contentDocument = dpf.getDocument();
244                 contentDocument.appendChild(
245                     dpf.cloneNode(contentDocument, contentNode, true));
246                 new DOMWriter(new FileOutputStream JavaDoc(tempFile)).printWithoutFormatting(
247                     contentDocument);
248             } catch (Exception JavaDoc e) {
249                 getLogger().error(".act(): Exception during writing to temp file", e);
250             }
251         }
252
253         // save to permanent file, if needed
254
if ("checkin".equals(reqType)) {
255             getLogger().debug(".act(): Save to permanent file: " + permFile);
256
257             RevisionController rc =
258                 new RevisionController(
259                     sitemapPath + rcmlDirectory,
260                     sitemapPath + backupDirectory,
261                     sitemapPath);
262
263             try {
264                 Session session = httpReq.getSession(false);
265
266                 if (session == null) {
267                     throw new Exception JavaDoc("No session");
268                 }
269
270                 Identity identity = (Identity) session.getAttribute(Identity.class.getName());
271                 org.apache.lenya.ac.Identity identityTwo =
272                     (org.apache.lenya.ac.Identity) session.getAttribute(Identity.class.getName());
273                 String JavaDoc username = null;
274                 if (identity != null) {
275                     User user = identity.getUser();
276                     if (user != null) {
277                         username = user.getId();
278                     }
279                 } else if (identityTwo != null) {
280                     username = identityTwo.getUser().getId();
281                 } else {
282                     getLogger().error(".act(): No identity!");
283                 }
284                 getLogger().debug(".act(): Checkin: " + reqFile + "::" + username);
285                 rc.reservedCheckIn(xmlRoot + reqFile, username, true);
286                 FileUtil.copyFile(tempFile, permFile);
287             } catch (Exception JavaDoc e) {
288                 getLogger().error(".act(): Exception during checkin of " + xmlRoot + reqFile, e);
289                 return null;
290             }
291         }
292
293         return sitemapParams;
294     }
295 }
296
Popular Tags