1 15 package org.apache.activemq.util; 16 17 import java.io.File ; 18 import java.io.FileOutputStream ; 19 import java.io.IOException ; 20 import java.net.URL ; 21 22 import javax.servlet.Filter ; 23 import javax.servlet.FilterChain ; 24 import javax.servlet.FilterConfig ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.UnavailableException ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 32 import org.mortbay.log.Log; 33 import org.mortbay.util.IO; 34 import org.mortbay.util.URIUtil; 35 36 import sun.net.www.protocol.http.HttpURLConnection; 37 38 47 public class RestFilter implements Filter { 48 49 private static final String HTTP_HEADER_DESTINATION = "Destination"; 50 private static final String HTTP_METHOD_MOVE = "MOVE"; 51 private static final String HTTP_METHOD_PUT = "PUT"; 52 private static final String HTTP_METHOD_GET = "GET"; 53 private static final String HTTP_METHOD_DELETE = "DELETE"; 54 55 private String readPermissionRole = null; 56 private String writePermissionRole = null; 57 private FilterConfig filterConfig; 58 59 public void init(FilterConfig filterConfig) throws UnavailableException 60 { 61 this.filterConfig = filterConfig; 62 readPermissionRole = filterConfig.getInitParameter("read-permission-role"); 63 writePermissionRole = filterConfig.getInitParameter("write-permission-role"); 64 } 65 66 private File locateFile(HttpServletRequest request) 67 { 68 return new File (filterConfig.getServletContext().getRealPath(URIUtil.addPaths(request.getServletPath(),request.getPathInfo()))); 69 } 70 71 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException 72 { 73 if (!(request instanceof HttpServletRequest && response instanceof HttpServletResponse )) 74 { 75 if (Log.isDebugEnabled()) 76 { 77 Log.debug("request not HTTP, can not understand: " + request.toString()); 78 } 79 chain.doFilter(request, response); 80 return; 81 } 82 83 HttpServletRequest httpRequest = (HttpServletRequest )request; 84 HttpServletResponse httpResponse = (HttpServletResponse )response; 85 86 if (httpRequest.getMethod().equals(HTTP_METHOD_MOVE)) 87 { 88 doMove(httpRequest, httpResponse); 89 } 90 else if (httpRequest.getMethod().equals(HTTP_METHOD_PUT)) 91 { 92 doPut(httpRequest, httpResponse); 93 } 94 else if (httpRequest.getMethod().equals(HTTP_METHOD_GET)) 95 { 96 if (checkGet(httpRequest, httpResponse)) { 97 chain.doFilter(httpRequest, httpResponse); } 99 } 100 else if (httpRequest.getMethod().equals(HTTP_METHOD_DELETE)) 101 { 102 doDelete(httpRequest, httpResponse); 103 } 104 else 105 { 106 chain.doFilter(httpRequest, httpResponse); 107 } 108 } 109 110 protected void doMove(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 111 { 112 if (Log.isDebugEnabled()) 113 { 114 Log.debug("RESTful file access: MOVE request for " + request.getRequestURI()); 115 } 116 117 if (writePermissionRole != null && !request.isUserInRole(writePermissionRole)) 118 { 119 response.sendError(HttpURLConnection.HTTP_FORBIDDEN); 120 return; 121 } 122 123 File file = locateFile(request); 124 String destination = request.getHeader(HTTP_HEADER_DESTINATION); 125 126 if (destination == null) 127 { 128 response.sendError(HttpURLConnection.HTTP_BAD_REQUEST, "Destination header not found"); 129 return; 130 } 131 132 try 133 { 134 URL destinationUrl = new URL (destination); 135 IO.copyFile(file, new File (destinationUrl.getFile())); 136 IO.delete(file); 137 } 138 catch (IOException e) 139 { 140 response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR); return; 142 } 143 144 response.setStatus(HttpURLConnection.HTTP_NO_CONTENT); } 146 147 protected boolean checkGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 148 if (Log.isDebugEnabled()) 149 { 150 Log.debug("RESTful file access: GET request for " + request.getRequestURI()); 151 } 152 153 if (readPermissionRole != null && !request.isUserInRole(readPermissionRole)) 154 { 155 response.sendError(HttpURLConnection.HTTP_FORBIDDEN); 156 return false; 157 } else { 158 return true; 159 } 160 } 161 162 163 protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 164 { 165 if (Log.isDebugEnabled()) 166 { 167 Log.debug("RESTful file access: PUT request for " + request.getRequestURI()); 168 } 169 170 if (writePermissionRole != null && !request.isUserInRole(writePermissionRole)) 171 { 172 response.sendError(HttpURLConnection.HTTP_FORBIDDEN); 173 return; 174 } 175 176 File file = locateFile(request); 177 178 if (file.exists()) 179 { 180 boolean success = file.delete(); if (!success) 182 { 183 response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR); return; 185 } 186 } 187 188 FileOutputStream out = new FileOutputStream (file); 189 try 190 { 191 IO.copy(request.getInputStream(), out); 192 } 193 catch (IOException e) 194 { 195 Log.warn(Log.EXCEPTION, e); out.close(); 197 throw(e); 198 } 199 200 response.setStatus(HttpURLConnection.HTTP_NO_CONTENT); } 202 203 protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException 204 { 205 if (Log.isDebugEnabled()) 206 { 207 Log.debug("RESTful file access: DELETE request for " + request.getRequestURI()); 208 } 209 210 if (writePermissionRole != null && !request.isUserInRole(writePermissionRole)) 211 { 212 response.sendError(HttpURLConnection.HTTP_FORBIDDEN); 213 return; 214 } 215 216 File file = locateFile(request); 217 218 if (!file.exists()) 219 { 220 response.sendError(HttpURLConnection.HTTP_NOT_FOUND); return; 222 } 223 224 boolean success = IO.delete(file); 226 if (success) 227 { 228 response.setStatus(HttpURLConnection.HTTP_NO_CONTENT); } 230 else 231 { 232 response.sendError(HttpURLConnection.HTTP_INTERNAL_ERROR); } 234 } 235 236 public void destroy() 237 { 238 } 240 } 241 242 | Popular Tags |