001: package org.claros.intouch.webdisk.services;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.PrintWriter;
006: import java.net.URLEncoder;
007: import java.text.DecimalFormat;
008: import java.util.Calendar;
009: import java.util.Date;
010: import java.util.Iterator;
011: import java.util.TreeSet;
012:
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016:
017: import org.claros.commons.auth.models.AuthProfile;
018: import org.claros.intouch.common.services.BaseService;
019: import org.claros.intouch.webdisk.controllers.WebdiskController;
020: import org.claros.intouch.webdisk.models.ClarosWebDskFile;
021: import org.claros.intouch.webdisk.models.ClarosWebDskFolder;
022: import org.claros.intouch.webdisk.models.ClarosWebDskObject;
023:
024: public class GetFileListService extends BaseService {
025: private static final long serialVersionUID = 985019861308833738L;
026: private static DecimalFormat df = new DecimalFormat("00");
027: private static final String months[] = new String[] {
028: "january.short", "february.short", "march.short",
029: "april.short", "may.short", "june.short", "july.short",
030: "august.short", "september.short", "october.short",
031: "november.short", "december.short" };
032:
033: /**
034: * @param request the request send by the client to the server
035: * @param response the response send by the server to the client
036: * @throws ServletException if an error occurred
037: * @throws IOException if an error occurred
038: */
039: public void doPost(HttpServletRequest request,
040: HttpServletResponse response) throws ServletException,
041: IOException {
042: response.setHeader("Expires", "-1");
043: response.setHeader("Pragma", "no-cache");
044: response.setHeader("Cache-control", "no-cache");
045: response.setHeader("Content-Type", "text/xml; charset=utf-8");
046: PrintWriter out = response.getWriter();
047:
048: out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
049: out.write("<data>");
050: try {
051: AuthProfile auth = getAuthProfile(request);
052: TreeSet contents = WebdiskController.getUserFiles(auth
053: .getUsername());
054:
055: String homeDir = WebdiskController.getUserHome(
056: auth.getUsername()).getAbsolutePath();
057: if (contents != null) {
058: Iterator iter = contents.iterator();
059: ClarosWebDskObject tmp = null;
060: while (iter.hasNext()) {
061: tmp = (ClarosWebDskObject) iter.next();
062: displayObject(tmp, homeDir, out, request);
063: }
064: }
065: } catch (Exception e) {
066: e.printStackTrace();
067: }
068: out.write("</data>");
069: }
070:
071: private void displayObject(ClarosWebDskObject tmp, String homeDir,
072: PrintWriter out, HttpServletRequest request)
073: throws Exception {
074: if (tmp instanceof ClarosWebDskFile) {
075: ClarosWebDskFile tmpF = (ClarosWebDskFile) tmp;
076: displayFile(tmpF, homeDir, out, request);
077: } else if (tmp instanceof ClarosWebDskFolder) {
078: ClarosWebDskFolder tmpD = (ClarosWebDskFolder) tmp;
079: out.print("<folder>");
080:
081: String tmpPath = WebdiskController.correctPath(tmpD
082: .getPath().substring(
083: tmpD.getPath().indexOf(homeDir)
084: + homeDir.length()));
085:
086: out.print("<name>" + tmpD.getName() + " </name>");
087: out.print("<path>" + WebdiskController.correctPath(tmpPath)
088: + " </path>");
089: out.print("<path-enc>"
090: + URLEncoder.encode(tmpPath, "utf-8")
091: + " </path-enc>");
092:
093: TreeSet files = tmpD.getContents();
094: Iterator iterF = files.iterator();
095: while (iterF.hasNext()) {
096: displayObject((ClarosWebDskObject) iterF.next(),
097: homeDir, out, request);
098: }
099: out.print("</folder>");
100: }
101: }
102:
103: private void displayFile(ClarosWebDskFile tmpF, String homeDir,
104: PrintWriter out, HttpServletRequest request)
105: throws Exception {
106: String icon, mime, name, path, size = null;
107: File ref = null;
108: Calendar cal = Calendar.getInstance();
109:
110: out.print("<file>");
111: icon = tmpF.getIcon();
112: mime = tmpF.getMimeType();
113: ref = tmpF.getFile();
114: name = tmpF.getName();
115: path = tmpF.getPath().substring(
116: tmpF.getPath().indexOf(homeDir) + homeDir.length());
117: size = org.claros.commons.mail.utility.Utility
118: .sizeToHumanReadable(ref.length());
119:
120: cal.setTime(new Date(ref.lastModified()));
121: String day = "" + cal.get(Calendar.DATE);
122: String month = getText(request, months[cal.get(Calendar.MONTH)]);
123: String year = "" + cal.get(Calendar.YEAR);
124: String hour = df.format(cal.get(Calendar.HOUR));
125: String minutes = df.format(cal.get(Calendar.MINUTE));
126: String dateDisplay = day + " " + month + " " + year + " "
127: + hour + ":" + minutes;
128:
129: if (mime == null) {
130: mime = "application/octet-stream";
131: }
132:
133: out.print("<icon>" + icon + " </icon>");
134: out.print("<mime>" + mime + " </mime>");
135: out.print("<name>" + name + " </name>");
136: out.print("<path>" + WebdiskController.correctPath(path)
137: + " </path>");
138: out.print("<path-enc>"
139: + URLEncoder.encode(
140: WebdiskController.correctPath(path), "utf-8")
141: + " </path-enc>");
142: out.print("<size>" + size + " </size>");
143: out.print("<date>" + dateDisplay + " </date>");
144: out.print("</file>");
145: }
146:
147: /**
148: *
149: */
150: public void doGet(HttpServletRequest request,
151: HttpServletResponse response) throws ServletException,
152: IOException {
153: doPost(request, response);
154: }
155:
156: }
|