1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.webdav.util;
14
15 import org.abstracthorizon.danube.http.HTTPConnection;
16 import org.abstracthorizon.danube.http.util.IOUtils;
17 import org.abstracthorizon.danube.webdav.ResourceAdapter;
18
19 import java.io.File;
20 import java.io.PrintWriter;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24
25
26
27
28
29
30 public class SimpleHTMLCollectionRenderer implements CollectionHTMLRenderer {
31
32
33 public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
34
35
36 public static final long KILOBYTE = 1024L;
37
38
39 public static final long MEGABYTE = KILOBYTE*1024L;
40
41
42 public static final long GIGABYTE = MEGABYTE*1024L;
43
44
45 public static final long TERABYTE = GIGABYTE*1024L;
46
47
48
49
50
51
52
53 public void render(HTTPConnection httpConnection, ResourceAdapter adapter, Object dir) {
54 String uri = httpConnection.getComponentPath();
55 if (!uri.endsWith("/")) {
56 uri = uri + "/";
57 }
58
59 String path = httpConnection.getComponentResourcePath();
60 if (!path.startsWith("/")) {
61 path = "/" + path;
62 }
63
64 String requestPath = httpConnection.getRequestPath();
65
66 String prefix = requestPath.substring(0, requestPath.length() - path.length() + 1);
67
68 PrintWriter out = (PrintWriter)httpConnection.adapt(PrintWriter.class);
69 out.println("<!--");
70 out.println("uri = " + uri);
71 out.println("path = " + path);
72 out.println("requestPath = " + requestPath);
73 out.println("prefix = " + prefix);
74 out.println("-->");
75 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
76 out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
77 out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
78 out.println("<head>");
79 out.println(" <title>Index of " + path + "</title>");
80 out.println(" <style type=\"text/css\">");
81 out.println(" img { border: 0; padding: 0 2px; vertical-align: text-bottom; }");
82 out.println(" td { font-family: monospace; padding: 2px 3px; text-align: right; vertical-align: bottom; white-space: pre; }");
83 out.println(" td:first-child { text-align: left; padding: 2px 10px 2px 3px; }");
84 out.println(" table { border: 0; }");
85 out.println(" a.symlink { font-style: italic; }");
86 out.println(" </style>");
87 out.println("</head>");
88 out.println("<body>");
89 out.println("<h1>Index of " + path + "</h1>");
90 out.println("<hr/>");
91 out.println("<table>");
92
93 String parent = new File(path).getParent();
94 if (parent == null) {
95 parent = "";
96 }
97 if (!parent.endsWith("/")) {
98 parent = parent + "/";
99 }
100 if (!path.equals("/")) {
101 out.print("<tr><td><a href=\"");
102 out.print(IOUtils.addPaths(prefix, parent));
103 out.print("\">Up to higher level directory</a></td><td> </td><td colspan=\"3\"> </td></tr>");
104 out.println();
105 }
106
107 Object[] files = adapter.collectionElements(dir);
108 if (files != null) {
109 for (int i = 0; i < files.length; i++) {
110
111 String p = IOUtils.addPaths(prefix, path);
112 p = IOUtils.addPaths(p, adapter.getResourceName(files[i]));
113
114 String link = encode(p);
115 String name = encode(adapter.getResourceName(files[i]));
116 String len = getLengthAsString(adapter, files[i]);
117
118 String date = "";
119 long lastModified = adapter.resourceLastModified(files[i]);
120 if (lastModified > 0) {
121 dateFormat.format(new Date(lastModified));
122 }
123
124 out.print("<tr><td><a href=\"");
125 out.print(link);
126 out.print("\">");
127 out.print(name);
128 out.print("</a></td><td>");
129 out.print(len);
130 out.print("</td><td>");
131 out.print(date);
132 out.print("</td></tr>");
133 out.println();
134 }
135 }
136 out.println("</table>");
137 out.println("<hr/>");
138 out.println("</body>");
139 out.println("</html>");
140 }
141
142
143
144
145
146
147 protected String getLengthAsString(ResourceAdapter adapter, Object f) {
148 if (adapter.isCollection(f)) {
149 return "<dir>";
150 }
151 long l = adapter.resourceLength(f);
152 if (l >= 0) {
153 if (l < KILOBYTE) {
154 return Long.toString(l);
155 }
156 if (l < MEGABYTE) {
157 return Long.toString(l / KILOBYTE) + " KB";
158 }
159 if (l < GIGABYTE) {
160 return Long.toString(l / MEGABYTE) + " MB";
161 }
162 if (l < TERABYTE) {
163 return Long.toString(l / GIGABYTE) + " MB";
164 }
165 return Long.toString(l / TERABYTE) + " TB";
166 } else {
167 return "unknown";
168 }
169 }
170
171
172
173
174
175
176 protected String encode(String string) {
177 return string;
178 }
179 }