001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.ui;
006:
007: import java.io.OutputStream;
008:
009: import javax.servlet.http.HttpServletResponse;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Element;
015:
016: import com.opensymphony.webwork.ServletActionContext;
017: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.File;
018: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.Folder;
019: import com.opensymphony.webwork.components.AbstractRichtexteditorConnector.FoldersAndFiles;
020: import com.opensymphony.xwork.ActionInvocation;
021:
022: /**
023: * <!-- START SNIPPET: javadoc -->
024: *
025: * WebWork's result, creating the appropriate result (in xml form) and write to
026: * the response stream corresponding to the Rich Text Editor's 'GetFoldersAndFiles'
027: * command
028: *
029: * <p/>
030: *
031: * An example of the response would be as follows:
032: *
033: * <pre>
034: * <?xml version="1.0" encoding="utf-8" ?>
035: * <Connector command="GetFoldersAndFiles" resourceType="File">
036: * <CurrentFolder path="/Samples/Docs/" url="/UserFiles/File/Samples/Docs/" />
037: * <Folders>
038: * <Folder name="Documents" />
039: * <Folder name="Files" />
040: * <Folder name="Other Files" />
041: * <Folder name="Related" />
042: * </Folders>
043: * <Files>
044: * <File name="XML Definition.doc" size="14" />
045: * <File name="Samples.txt" size="5" />
046: * <File name="Definition.txt" size="125" />
047: * <File name="External Resources.drw" size="840" />
048: * <File name="Todo.txt" size="2" />
049: * </Files>
050: * </Connector>
051: * </pre>
052: *
053: * <!-- END SNIPPET: javadoc -->
054: *
055: * @author tm_jee
056: * @version $Date: 2006-02-20 16:24:38 +0100 (Mon, 20 Feb 2006) $ $Id: RichtexteditorGetFoldersAndFilesResult.java 2245 2006-02-20 15:24:38Z tmjee $
057: */
058: public class RichtexteditorGetFoldersAndFilesResult extends
059: AbstractRichtexteditorResult {
060:
061: private static final Log _log = LogFactory
062: .getLog(RichtexteditorGetFoldersAndFilesResult.class);
063:
064: private static final long serialVersionUID = -8405656868125936172L;
065:
066: /**
067: * <!-- START SNIPPET: execute -->
068: *
069: * Write the result (in xml form) to the response stream corresponding to
070: * the Rich Text Editor's 'GetFoldersAndFiles' command.
071: *
072: * <!-- END SNIPPET: execute -->
073: *
074: * @param invocation
075: */
076: public void execute(ActionInvocation invocation) throws Exception {
077:
078: HttpServletResponse response = ServletActionContext
079: .getResponse();
080: response.setContentType("text/xml; charset=UTF-8");
081: response.setHeader("Cache-Control", "no-cache");
082:
083: OutputStream os = response.getOutputStream();
084:
085: Document document = buildDocument();
086: Element root = buildCommonResponseXml(document,
087: getCommand(invocation), getType(invocation),
088: getFolderPath(invocation), getServerPath(invocation));
089:
090: FoldersAndFiles foldersAndFiles = richtexteditorFoldersAndFiles(invocation);
091:
092: Folder[] folders = foldersAndFiles.getFolders();
093: File[] files = foldersAndFiles.getFiles();
094:
095: Element foldersElement = document.createElement("Folders");
096: if (folders != null) {
097: for (int a = 0; a < folders.length; a++) {
098: Element folderElement = document
099: .createElement("Folder");
100: folderElement.setAttribute("name", folders[a]
101: .getFoldername());
102: foldersElement.appendChild(folderElement);
103: }
104: }
105: root.appendChild(foldersElement);
106:
107: Element filesElement = document.createElement("Files");
108: if (files != null) {
109: for (int a = 0; a < files.length; a++) {
110: Element fileElement = document.createElement("File");
111: fileElement
112: .setAttribute("name", files[a].getFilename());
113: fileElement.setAttribute("size", String
114: .valueOf(files[a].getSizeInKb()));
115: filesElement.appendChild(fileElement);
116: }
117: }
118: root.appendChild(filesElement);
119:
120: if (_log.isDebugEnabled()) {
121: String result = stringFromDocument(document);
122: _log.debug(result);
123: }
124:
125: writeDocumentToStream(document, os);
126: os.flush();
127: os.close();
128: }
129: }
|