KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > iCalServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.net;
26
27 import org.snipsnap.app.Application;
28 import org.snipsnap.snip.Snip;
29 import org.snipsnap.snip.SnipSpace;
30 import org.snipsnap.snip.SnipSpaceFactory;
31 import org.snipsnap.user.User;
32 import org.snipsnap.user.UserManager;
33 import org.snipsnap.user.AuthenticationService;
34 import org.snipsnap.user.UserManagerFactory;
35 import org.snipsnap.util.Base64;
36 import org.snipsnap.container.Components;
37
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.http.HttpServlet JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43 import java.io.BufferedReader JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.util.Iterator JavaDoc;
47
48 /**
49  * Store an iCalendar.
50  * @author Matthias L. Jugel
51  * @version $Id: iCalServlet.java 1256 2003-12-11 13:24:57Z leo $
52  */

53 public class iCalServlet extends HttpServlet JavaDoc {
54   private final static int WD_CREATED = 201;
55   private final static int WD_BAD_REQUEST = 400;
56   private final static int WD_UNAUTHORIZED = 401;
57
58   public void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
59     HttpSession JavaDoc session = request.getSession();
60     UserManager um = UserManagerFactory.getInstance();
61
62     String JavaDoc method = request.getMethod();
63     String JavaDoc pathInfo = request.getPathInfo();
64
65     // make sure the user is authorized
66
String JavaDoc auth = request.getHeader("Authorization");
67     String JavaDoc login = "", password = "";
68
69     if (auth != null) {
70       auth = new String JavaDoc(Base64.decode(auth.substring(auth.indexOf(' ') + 1)));
71       login = auth.substring(0, auth.indexOf(':'));
72       password = auth.substring(auth.indexOf(':') + 1);
73     }
74
75     User user = ((AuthenticationService) Components.getComponent(AuthenticationService.class)).authenticate(login, password);
76     if (user == null) {
77       response.setHeader("WWW-Authenticate", "Basic realm=\"SnipSnap\"");
78       response.setStatus(WD_UNAUTHORIZED);
79       return;
80     } else {
81       Application.get().setUser(user);
82     }
83
84
85     String JavaDoc name = user.getLogin();
86     String JavaDoc file = pathInfo.substring(1);
87     try {
88       int slashIdx = pathInfo.indexOf('/', 1);
89       if (slashIdx > -1) {
90         name = pathInfo.substring(1, slashIdx);
91         file = pathInfo.substring(slashIdx + 1);
92       }
93     } catch (Exception JavaDoc e) {
94       // ignore and let the if below handle
95
}
96
97     System.err.println("iCalServlet: " + method + "(" + user.getLogin() + "," + file + ")");
98
99     // check that we have a name and a file
100
if (!user.getLogin().equals(name) || null == name || null == file) {
101       response.setStatus(WD_BAD_REQUEST);
102       return;
103     }
104
105     // dispatch to methods
106
if ("DELETE".equals(method)) {
107       delete(name, file);
108     } else if ("PUT".equals(method)) {
109       put(name, file, request, response);
110     } else if ("GET".equals(method)) {
111       get(name, file, request, response);
112     }
113
114
115     response.setStatus(WD_CREATED);
116   }
117
118   protected void delete(String JavaDoc name, String JavaDoc file) {
119     SnipSpace space = SnipSpaceFactory.getInstance();
120     if (space.exists(name)) {
121       Snip userSnip = space.load(name);
122       Iterator JavaDoc it = userSnip.getChildren().iterator();
123       while (it.hasNext()) {
124         Snip snip = (Snip) it.next();
125         if (snip.getName().equals("calendar-" + name + "-" + file)) {
126           space.remove(snip);
127         }
128       }
129     }
130   }
131
132   StringBuffer JavaDoc content = new StringBuffer JavaDoc();
133
134   protected void put(String JavaDoc name, String JavaDoc file,
135                      HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
136     SnipSpace space = SnipSpaceFactory.getInstance();
137     if (space.exists(name)) {
138       Snip userSnip = space.load(name);
139
140       BufferedReader JavaDoc r = request.getReader();
141       content.setLength(0);
142       char buffer[] = new char[1024];
143       int l = 0;
144       while ((l = r.read(buffer)) != -1) {
145         content.append(buffer, 0, l);
146       }
147       Snip snip = space.create("calendar-" + name + "-" + file, content.toString());
148       userSnip.addSnip(snip);
149     }
150   }
151
152   protected void get(String JavaDoc name, String JavaDoc file,
153                      HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc {
154     SnipSpace space = SnipSpaceFactory.getInstance();
155     if (space.exists(name)) {
156       PrintWriter JavaDoc w = response.getWriter();
157       Snip userSnip = space.load(name);
158       Iterator JavaDoc it = userSnip.getChildren().iterator();
159       while (it.hasNext()) {
160         Snip snip = (Snip) it.next();
161         if (snip.getName().equals("calendar-" + name + "-" + file)) {
162           String JavaDoc content = snip.getContent();
163           response.setContentLength(content.length());
164           response.setContentType("application/octet-stream");
165           w.print(content);
166           w.flush();
167           w.close();
168           return;
169         }
170       }
171     }
172   }
173
174
175 }
176
Popular Tags