1 45 package org.exolab.jms.net.tunnel; 46 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.InterruptedIOException ; 50 import java.io.OutputStream ; 51 import java.io.PrintWriter ; 52 import java.net.Socket ; 53 import javax.servlet.ServletException ; 54 import javax.servlet.http.HttpServlet ; 55 import javax.servlet.http.HttpServletRequest ; 56 import javax.servlet.http.HttpServletResponse ; 57 58 59 65 public class TunnelServlet extends HttpServlet { 66 67 70 private String _host; 71 72 75 private int _port; 76 77 80 private static final EndpointManager _manager = new EndpointManager(); 81 82 85 private static final String SERVER_HOST = "host"; 86 87 91 private static final String SERVER_PORT = "port"; 92 93 94 99 public void init() throws ServletException { 100 String host = getInitParameter(SERVER_HOST); 101 String port = getInitParameter(SERVER_PORT); 102 if (host == null) { 103 throw new ServletException ("Property not defined: " + SERVER_HOST); 104 } 105 if (port == null) { 106 throw new ServletException ("Property not defined: " + SERVER_PORT); 107 } 108 _host = host; 109 try { 110 _port = Integer.parseInt(port); 111 } catch (NumberFormatException exception) { 112 throw new ServletException ("Invalid port: " + port); 113 } 114 log("OpenJMS tunnel accepting requests"); 115 } 116 117 125 protected void doGet(HttpServletRequest request, 126 HttpServletResponse response) throws IOException { 127 response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 128 } 129 130 137 protected void doPost(HttpServletRequest request, 138 HttpServletResponse response) throws IOException { 139 140 String action = request.getHeader("action"); 141 142 if (action == null) { 143 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 144 "Invalid action"); 145 } else if (action.equals("open")) { 146 open(request, response); 147 } else { 148 String id = request.getHeader("id"); 149 if (id == null) { 150 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 151 "Invalid connection"); 152 } else if (action.equals("read")) { 153 read(id, response); 154 } else if (action.equals("write")) { 155 write(id, request, response); 156 } else if (action.equals("close")) { 157 close(id, response); 158 } else { 159 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 160 "Invalid action"); 161 } 162 } 163 } 164 165 173 private void open(HttpServletRequest request, 174 HttpServletResponse response) throws IOException { 175 179 response.setContentType("text/plain"); 180 PrintWriter out = new PrintWriter (response.getWriter()); 181 182 try { 183 String id = _manager.open(_host, _port); 184 out.println("OPEN " + id); 185 response.setStatus(HttpServletResponse.SC_OK); 186 } catch (Exception exception) { 187 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 188 exception.getMessage()); 189 log("open failed", exception); 190 } 191 } 192 193 201 private void read(String id, HttpServletResponse response) 202 throws IOException { 203 Socket endpoint = _manager.getEndpoint(id); 204 if (endpoint == null) { 205 log("Connection not found, id=" + id); 206 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 207 "Connection not found"); 208 } else { 209 byte[] data = new byte[1024]; 210 try { 211 endpoint.setSoTimeout(1000); 212 InputStream in = endpoint.getInputStream(); 213 int count = 0; 214 try { 215 count = in.read(data); 216 } catch (InterruptedIOException ignore) { 217 } 218 if (count != -1) { 220 response.setContentLength(count); 221 response.setStatus(HttpServletResponse.SC_OK); 222 OutputStream out = response.getOutputStream(); 223 out.write(data, 0, count); 224 out.flush(); 225 } else { 226 remove(id); 227 response.setStatus( 228 HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 229 } 230 } catch (IOException exception) { 231 String message = exception.getMessage(); 232 log("read failed", exception); 233 remove(id); 234 response.sendError( 235 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 236 exception.getMessage()); 237 } 238 } 239 } 240 241 249 private void write(String id, HttpServletRequest request, 250 HttpServletResponse response) throws IOException { 251 Socket endpoint = _manager.getEndpoint(id); 252 if (endpoint == null) { 253 response.sendError(HttpServletResponse.SC_BAD_REQUEST, 254 "Connection not found"); 255 } else { 256 try { 257 InputStream in = request.getInputStream(); 261 OutputStream out = endpoint.getOutputStream(); 262 byte[] data = new byte[1024]; 263 int count = 0; 264 while (count != -1) { 265 count = in.read(data); 266 if (count > 0) { 267 out.write(data, 0, count); 268 } 269 } 270 in.close(); 271 out.flush(); 272 response.setStatus(HttpServletResponse.SC_OK); 273 } catch (IOException exception) { 274 String message = exception.getMessage(); 275 log("write failed", exception); 276 remove(id); 277 response.sendError( 278 HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 279 exception.getMessage()); 280 } 281 } 282 } 283 284 291 private void close(String id, HttpServletResponse response) 292 throws IOException { 293 294 try { 295 log("close(id=" + id + ")"); 296 _manager.close(id); 297 response.setStatus(HttpServletResponse.SC_OK); 298 } catch (IOException exception) { 299 log("close failed", exception); 300 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 301 exception.getMessage()); 302 } 303 } 304 305 310 private void remove(String id) { 311 try { 312 _manager.close(id); 313 } catch (IOException ignore) { 314 } 315 } 316 317 } 318 | Popular Tags |