1 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 ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.servlet.http.HttpSession ; 43 import java.io.BufferedReader ; 44 import java.io.IOException ; 45 import java.io.PrintWriter ; 46 import java.util.Iterator ; 47 48 53 public class iCalServlet extends HttpServlet { 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 request, HttpServletResponse response) throws ServletException , IOException { 59 HttpSession session = request.getSession(); 60 UserManager um = UserManagerFactory.getInstance(); 61 62 String method = request.getMethod(); 63 String pathInfo = request.getPathInfo(); 64 65 String auth = request.getHeader("Authorization"); 67 String login = "", password = ""; 68 69 if (auth != null) { 70 auth = new String (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 name = user.getLogin(); 86 String 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 e) { 94 } 96 97 System.err.println("iCalServlet: " + method + "(" + user.getLogin() + "," + file + ")"); 98 99 if (!user.getLogin().equals(name) || null == name || null == file) { 101 response.setStatus(WD_BAD_REQUEST); 102 return; 103 } 104 105 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 name, String file) { 119 SnipSpace space = SnipSpaceFactory.getInstance(); 120 if (space.exists(name)) { 121 Snip userSnip = space.load(name); 122 Iterator 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 content = new StringBuffer (); 133 134 protected void put(String name, String file, 135 HttpServletRequest request, HttpServletResponse response) throws IOException { 136 SnipSpace space = SnipSpaceFactory.getInstance(); 137 if (space.exists(name)) { 138 Snip userSnip = space.load(name); 139 140 BufferedReader 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 name, String file, 153 HttpServletRequest request, HttpServletResponse response) throws IOException { 154 SnipSpace space = SnipSpaceFactory.getInstance(); 155 if (space.exists(name)) { 156 PrintWriter w = response.getWriter(); 157 Snip userSnip = space.load(name); 158 Iterator it = userSnip.getChildren().iterator(); 159 while (it.hasNext()) { 160 Snip snip = (Snip) it.next(); 161 if (snip.getName().equals("calendar-" + name + "-" + file)) { 162 String 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 |