1 17 18 19 20 package org.apache.lenya.cms.cocoon.acting; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 import java.util.Map ; 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 ; 50 import org.w3c.dom.Element ; 51 import org.xml.sax.InputSource ; 52 import org.xml.sax.SAXException ; 53 54 57 public class XopusHandlerAction extends ConfigurableServiceableAction { 58 private String xmlRoot = null; 59 private String xslRoot = null; 60 private String xsdRoot = null; 61 private String tempRoot = null; 62 private Map relRootDirs = new HashMap (); 63 private String rcmlDirectory = null; 64 private String backupDirectory = null; 65 66 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 relRootDirs.put("xml", xmlRoot); 95 relRootDirs.put("xsl", xslRoot); 96 relRootDirs.put("xsd", xsdRoot); 97 relRootDirs.put("temp", tempRoot); 98 99 rcmlDirectory = conf.getChild("rcmlDirectory").getAttribute("href"); 101 backupDirectory = conf.getChild("backupDirectory").getAttribute("href"); 102 } 103 104 120 public java.util.Map act( 121 Redirector redirector, 122 SourceResolver resolver, 123 Map objectModel, 124 String source, 125 Parameters params) 126 throws IOException , SAXException , ProcessingException, ServiceException { 127 Source input_source = resolver.resolveURI(""); 129 String sitemapPath = input_source.getURI(); 130 sitemapPath = sitemapPath.substring(5); 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 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 DOMParser parser = (DOMParser) this.manager.lookup(DOMParser.ROLE); 151 InputSource saxSource = new InputSource (reqContent); 152 Document requestDoc = parser.parseDocument(saxSource); 153 154 Element root = requestDoc.getDocumentElement(); 156 getLogger().debug(".act(): Root element (should be 'request'): " + root.getTagName()); 157 158 String reqId = root.getAttribute("id"); 159 getLogger().debug(".act(): Request ID: " + reqId); 160 161 String reqType = root.getAttribute("type"); 162 getLogger().debug(".act(): Request Type: " + reqType); 163 164 Element data = (Element ) root.getFirstChild(); 166 getLogger().debug(".act(): first child element (should be 'data'): " + data.getTagName()); 167 168 String reqFile = data.getAttribute("id"); 169 getLogger().debug(".act(): Requested File: " + reqFile); 170 171 String fileType = data.getAttribute("type"); 172 getLogger().debug(".act(): Requested File's Type: " + fileType); 173 174 reqContent.close(); 176 177 File tempFileDir = 179 new File (sitemapPath + relRootDirs.get("temp") + "/" + relRootDirs.get(fileType)); 180 181 if (!(tempFileDir.exists())) { 182 tempFileDir.mkdir(); 183 } 184 185 File tempFile = IOUtils.createFile(tempFileDir, reqFile); 186 File permFile = new File (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 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 Map sitemapParams = new HashMap (); 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 ) relRootDirs.get("temp") 219 + "/" 220 + (String ) 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 ) relRootDirs.get(fileType) + "/" + reqFile); 227 } 228 229 if ("open".equals(reqType)) { 231 return sitemapParams; 232 } 233 234 if ("save".equals(reqType) || "checkin".equals(reqType)) { 236 getLogger().debug(".act(): Write to temp file: " + tempFile); 237 238 try { 239 Element contentNode = (Element ) data.getFirstChild(); 240 DOMParserFactory dpf = new DOMParserFactory(); 241 242 Document contentDocument = dpf.getDocument(); 244 contentDocument.appendChild( 245 dpf.cloneNode(contentDocument, contentNode, true)); 246 new DOMWriter(new FileOutputStream (tempFile)).printWithoutFormatting( 247 contentDocument); 248 } catch (Exception e) { 249 getLogger().error(".act(): Exception during writing to temp file", e); 250 } 251 } 252 253 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 ("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 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 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 |