01: package org.claros.intouch.webdisk.services;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import java.io.PrintWriter;
06:
07: import javax.servlet.ServletException;
08: import javax.servlet.http.HttpServletRequest;
09: import javax.servlet.http.HttpServletResponse;
10:
11: import org.claros.intouch.common.services.BaseService;
12: import org.claros.intouch.webdisk.controllers.WebdiskController;
13:
14: public class DeleteItemService extends BaseService {
15: private static final long serialVersionUID = -7181255224935275339L;
16:
17: /**
18: * The doPost method of the servlet. <br>
19: *
20: * This method is called when a form has its tag value method equals to post.
21: *
22: * @param request the request send by the client to the server
23: * @param response the response send by the server to the client
24: * @throws ServletException if an error occurred
25: * @throws IOException if an error occurred
26: */
27: public void doPost(HttpServletRequest request,
28: HttpServletResponse response) throws ServletException,
29: IOException {
30: response.setHeader("Expires", "-1");
31: response.setHeader("Pragma", "no-cache");
32: response.setHeader("Cache-control", "no-cache");
33: response.setContentType("text/html");
34: PrintWriter out = response.getWriter();
35:
36: try {
37: String path = request.getParameter("path");
38: String username = getAuthProfile(request).getUsername();
39:
40: File f = WebdiskController.getUserFile(username, path);
41:
42: File uploadDir = WebdiskController.getUploadDir(username);
43: if (uploadDir.getAbsolutePath().equals(f.getAbsolutePath())) {
44: out.print("ok");
45: } else {
46: if (deleteItem(f)) {
47: out.print("ok");
48: } else {
49: out.print("fail");
50: }
51: }
52: } catch (Exception e) {
53: out.print("fail");
54: }
55: }
56:
57: /**
58: * Deletes all files and subdirectories under dir.
59: * Returns true if all deletions were successful.
60: * If a deletion fails, the method stops attempting to delete and returns false.
61: *
62: * @param dir
63: * @return
64: */
65: public static boolean deleteItem(File dir) {
66: if (dir.isDirectory()) {
67: String[] children = dir.list();
68: for (int i = 0; i < children.length; i++) {
69: boolean success = deleteItem(new File(dir, children[i]));
70: if (!success) {
71: return false;
72: }
73: }
74: }
75: return dir.delete();
76: }
77: }
|