001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders.files;
004:
005: import fitnesse.http.*;
006: import fitnesse.*;
007: import fitnesse.util.FileUtil;
008: import fitnesse.authentication.*;
009: import fitnesse.responders.*;
010: import fitnesse.html.HtmlTableListingBuilder;
011: import fitnesse.html.*;
012: import java.io.File;
013: import java.text.SimpleDateFormat;
014: import java.util.*;
015:
016: public class DirectoryResponder implements SecureResponder {
017: private String resource;
018:
019: private File requestedDirectory;
020:
021: private FitNesseContext context;
022:
023: private SimpleDateFormat dateFormat = new SimpleDateFormat(
024: "MMM dd, yyyy, hh:mm a");
025:
026: public DirectoryResponder(String resource, File requestedFile) {
027: this .resource = resource;
028: requestedDirectory = requestedFile;
029: }
030:
031: public Response makeResponse(FitNesseContext context,
032: Request request) throws Exception {
033: this .context = context;
034:
035: SimpleResponse simpleResponse = new SimpleResponse();
036: if (!resource.endsWith("/"))
037: setRedirectForDirectory(simpleResponse);
038: else
039: simpleResponse.setContent(makeDirectoryListingPage());
040: return simpleResponse;
041: }
042:
043: private void setRedirectForDirectory(Response response) {
044: if (!resource.startsWith("/"))
045: resource = "/" + resource;
046: response.redirect(resource + "/");
047: }
048:
049: private String makeDirectoryListingPage() throws Exception {
050: HtmlPage page = context.htmlPageFactory.newPage();
051: page.title.use("Files: " + resource);
052: page.header.use(HtmlUtil.makeBreadCrumbsWithPageType(resource,
053: "/", "Files Section"));
054: page.actions.use(HtmlUtil.makeActionLink("/FrontPage",
055: "FrontPage", null, "f", false));
056: page.main.use(makeRightColumn());
057:
058: return page.html();
059: }
060:
061: private String makeRightColumn() throws Exception {
062: TagGroup html = new TagGroup();
063: html.add(addFiles(FileUtil
064: .getDirectoryListing(requestedDirectory)));
065: html.add(HtmlUtil.HR.html());
066: html.add(makeUploadForm());
067: html.add(makeDirectoryForm());
068: return html.html();
069: }
070:
071: private HtmlTag addFiles(File[] files) throws Exception {
072: HtmlTableListingBuilder table = new HtmlTableListingBuilder();
073: makeHeadingRow(table);
074: addFileRows(files, table);
075:
076: return table.getTable();
077: }
078:
079: private void addFileRows(File[] files, HtmlTableListingBuilder table)
080: throws Exception {
081: for (int i = 0; i < files.length; i++) {
082: File file = files[i];
083: HtmlTag nameItem = makeLinkToFile(file);
084: HtmlElement sizeItem = new RawHtml(getSizeString(file));
085: HtmlElement dateItem = new RawHtml(dateFormat
086: .format(new Date(file.lastModified())));
087: TagGroup actionItem = new TagGroup();
088: actionItem.add(makeRenameButton(file.getName()));
089: actionItem.add("|");
090: actionItem.add(makeDeleteButton(file.getName()));
091: table.addRow(new HtmlElement[] { nameItem, sizeItem,
092: dateItem, actionItem });
093: }
094: }
095:
096: private void makeHeadingRow(HtmlTableListingBuilder table)
097: throws Exception {
098: HtmlTag nameHeading = HtmlUtil.makeSpanTag("caps", "Name");
099: HtmlTag sizeHeading = HtmlUtil.makeSpanTag("caps", "Size");
100: HtmlTag dateHeading = HtmlUtil.makeSpanTag("caps", "Date");
101: HtmlTag actionHeading = HtmlUtil.makeSpanTag("caps", "Action");
102: table.addRow(new HtmlTag[] { nameHeading, sizeHeading,
103: dateHeading, actionHeading });
104: }
105:
106: private HtmlTag makeDeleteButton(String filename) throws Exception {
107: return HtmlUtil.makeLink(
108: "?responder=deleteConfirmation&filename=" + filename,
109: "Delete");
110: }
111:
112: private HtmlTag makeRenameButton(String filename) throws Exception {
113: return HtmlUtil.makeLink(
114: "?responder=renameConfirmation&filename=" + filename,
115: "Rename");
116: }
117:
118: private HtmlTag makeLinkToFile(File file) {
119: String href = file.getName();
120: if (file.isDirectory()) {
121: href += "/";
122: HtmlTag image = new HtmlTag("img");
123: image.addAttribute("src", "/files/images/folder.gif");
124: image.addAttribute("class", "left");
125: HtmlTag link = HtmlUtil.makeLink(href, image);
126: link.add(file.getName());
127: return link;
128: } else
129: return HtmlUtil.makeLink(href, file.getName());
130: }
131:
132: private HtmlTag makeUploadForm() throws Exception {
133: HtmlTag uploadForm = HtmlUtil.makeFormTag("post", "/"
134: + resource);
135: uploadForm.addAttribute("enctype", "multipart/form-data");
136: uploadForm.addAttribute("class", "left");
137: uploadForm.add("<!--upload form-->");
138: uploadForm.add(HtmlUtil.makeSpanTag("caps", "Upload a file:"));
139: uploadForm.add(HtmlUtil.makeInputTag("hidden", "responder",
140: "upload"));
141: uploadForm.add(HtmlUtil.BR);
142: uploadForm.add(HtmlUtil.makeInputTag("file", "file", ""));
143: uploadForm.add(HtmlUtil.BR);
144: uploadForm.add(HtmlUtil.makeInputTag("submit", "", "Upload"));
145: return uploadForm;
146: }
147:
148: private HtmlTag makeDirectoryForm() throws Exception {
149: HtmlTag dirForm = HtmlUtil.makeFormTag("get", "/" + resource);
150: dirForm.addAttribute("class", "right");
151: dirForm.add(HtmlUtil.makeInputTag("hidden", "responder",
152: "createDir"));
153: dirForm.add("<!--create directory form-->");
154: dirForm
155: .add(HtmlUtil
156: .makeSpanTag("caps", "Create a directory:"));
157: dirForm.add(HtmlUtil.BR);
158: dirForm.add(HtmlUtil.makeInputTag("text", "dirname", ""));
159: dirForm.add(HtmlUtil.BR);
160: dirForm.add(HtmlUtil.makeInputTag("submit", "", "Create"));
161: return dirForm;
162: }
163:
164: public static String getSizeString(File file) {
165: if (file.isDirectory())
166: return "";
167: else
168: return file.length() + " bytes";
169: }
170:
171: public SecureOperation getSecureOperation() {
172: return new AlwaysSecureOperation();
173: }
174: }
|