New web administration interface
[bm-groupware-server.git] / bm-webconfig / webconfig-webapp / src / main / java / net / bionicmessage / funambol / webconfig / principalSyncs.java
blob5d6e349fd4bfb40081887217cbce348b7cfc38d5
1 /*
2 * WebConfig - a web administration interface for the Funambol DS Server.
3 * Copyright (C) 2008 Mathew McBride
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.bionicmessage.funambol.webconfig;
20 import com.funambol.framework.server.LastTimestamp;
21 import com.google.gson.Gson;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import net.bionicmessage.funambol.configuration.PrincipalUtils;
30 import java.text.DateFormat;
31 import java.util.Date;
32 import org.apache.log4j.helpers.ISO8601DateFormat;
34 /**
36 * @author matt
38 public class principalSyncs extends HttpServlet {
40 /**
41 * Handles the HTTP <code>GET</code> method.
42 * @param request servlet request
43 * @param response servlet response
45 protected void doGet(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 response.setContentType("application/json;charset=UTF-8");
48 PrintWriter out = response.getWriter();
49 HttpSession session = request.getSession();
50 boolean isAdministrator = (session.getAttribute("isadministrator") != null);
51 String curUser = (String) session.getAttribute("user");
52 try {
53 String id = request.getParameter("id");
54 if (!isAdministrator &&
55 !PrincipalUtils.isUserAllowedToModifyPrincipal(id, curUser)) {
56 // check we have authority to operate on this id
57 throw new IllegalArgumentException("You are not authorized to edit this principal");
59 LastTimestamp[] lt = PrincipalUtils.getSyncsForPrincipal(id);
60 String[][] retData = new String[lt.length][];
61 DateFormat df = new ISO8601DateFormat();
62 for (int i = 0; i < lt.length; i++) {
63 LastTimestamp l = lt[i];
64 String[] lData = {
65 l.database,
66 PrincipalUtils.typeStringForSyncCode(l.syncType),
67 Integer.toString(l.status),
68 df.format(new Date(l.start)),
69 df.format(new Date(l.end))
71 retData[i] = lData;
73 Gson gson = new Gson();
74 out.println(gson.toJson(retData));
75 } catch (Exception e) {
76 response.setStatus(500);
77 out.println("Error: " + e.getMessage());
78 e.printStackTrace(out);
79 } finally {
80 out.close();
84 /**
85 * Handles the HTTP <code>POST</code> method.
86 * @param request servlet request
87 * @param response servlet response
89 protected void doPost(HttpServletRequest request, HttpServletResponse response)
90 throws ServletException, IOException {
91 response.setContentType("text/plain;charset=utf8");
92 HttpSession session = request.getSession();
93 PrintWriter out = response.getWriter();
94 boolean isAdministrator = (session.getAttribute("isadministrator") != null);
95 String curUser = (String) session.getAttribute("user");
96 String syncSource = request.getParameter("syncsource");
97 String principalId = request.getParameter("principalId");
98 try {
99 if (!isAdministrator &&
100 !PrincipalUtils.isUserAllowedToModifyPrincipal(principalId, curUser)) {
101 // check we have authority to operate on this id
102 throw new IllegalArgumentException("You are not authorized to edit this principal");
104 PrincipalUtils.deletePrincipalSync(principalId, syncSource);
105 out.println("Success");
106 } catch (Exception e) {
107 response.setStatus(500);
108 out.println(e.getMessage());
109 } finally {
110 out.close();
114 out.println("Method not implemented");
117 /**
118 * Returns a short description of the servlet.
120 public String getServletInfo() {
121 return "Short description";
122 }// </editor-fold>