/jetty-8.1.5.v20120716/test-jetty-servlet/src/main/java/org/eclipse/jetty/testing/ServletTester.java

# · Java · 374 lines · 174 code · 37 blank · 163 comment · 5 complexity · b22f14b3182964553d04efbfcf59f475 MD5 · raw file

  1. // ========================================================================
  2. // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
  3. // ------------------------------------------------------------------------
  4. // All rights reserved. This program and the accompanying materials
  5. // are made available under the terms of the Eclipse Public License v1.0
  6. // and Apache License v2.0 which accompanies this distribution.
  7. // The Eclipse Public License is available at
  8. // http://www.eclipse.org/legal/epl-v10.html
  9. // The Apache License v2.0 is available at
  10. // http://www.opensource.org/licenses/apache2.0.php
  11. // You may elect to redistribute this code under either of these licenses.
  12. // ========================================================================
  13. package org.eclipse.jetty.testing;
  14. import java.net.InetAddress;
  15. import java.util.EnumSet;
  16. import java.util.Enumeration;
  17. import java.util.EventListener;
  18. import javax.servlet.DispatcherType;
  19. import org.eclipse.jetty.io.ByteArrayBuffer;
  20. import org.eclipse.jetty.server.LocalConnector;
  21. import org.eclipse.jetty.server.Server;
  22. import org.eclipse.jetty.server.bio.SocketConnector;
  23. import org.eclipse.jetty.server.handler.ErrorHandler;
  24. import org.eclipse.jetty.server.nio.SelectChannelConnector;
  25. import org.eclipse.jetty.servlet.FilterHolder;
  26. import org.eclipse.jetty.servlet.ServletContextHandler;
  27. import org.eclipse.jetty.servlet.ServletHolder;
  28. import org.eclipse.jetty.util.Attributes;
  29. /* ------------------------------------------------------------ */
  30. /** Testing support for servlets and filters.
  31. *
  32. * Allows a programatic setup of a context with servlets and filters for
  33. * testing. Raw HTTP requests may be sent to the context and responses received.
  34. * To avoid handling raw HTTP see {@link org.eclipse.jetty.testing.HttpTester}.
  35. * <pre>
  36. * ServletTester tester=new ServletTester();
  37. * tester.setContextPath("/context");
  38. * tester.addServlet(TestServlet.class, "/servlet/*");
  39. * tester.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/");
  40. * tester.start();
  41. * String response = tester.getResponses("GET /context/servlet/info HTTP/1.0\r\n\r\n");
  42. * </pre>
  43. *
  44. * @see org.eclipse.jetty.testing.HttpTester
  45. *
  46. *
  47. */
  48. public class ServletTester
  49. {
  50. Server _server = new Server();
  51. LocalConnector _connector = new LocalConnector();
  52. // Context _context = new Context(Context.SESSIONS|Context.SECURITY);
  53. //jaspi why security if it is not set up?
  54. ServletContextHandler _context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  55. public ServletTester()
  56. {
  57. try
  58. {
  59. _server.addBean(new ErrorHandler());
  60. _server.setSendServerVersion(false);
  61. _server.addConnector(_connector);
  62. _server.setHandler(_context);
  63. }
  64. catch (Error e)
  65. {
  66. throw e;
  67. }
  68. catch (RuntimeException e)
  69. {
  70. throw e;
  71. }
  72. catch (Exception e)
  73. {
  74. throw new RuntimeException(e);
  75. }
  76. }
  77. /* ------------------------------------------------------------ */
  78. public void dump()
  79. {
  80. _server.dump();
  81. }
  82. /* ------------------------------------------------------------ */
  83. public void start() throws Exception
  84. {
  85. _server.start();
  86. }
  87. /* ------------------------------------------------------------ */
  88. public void stop() throws Exception
  89. {
  90. _server.stop();
  91. }
  92. /* ------------------------------------------------------------ */
  93. public ServletContextHandler getContext()
  94. {
  95. return _context;
  96. }
  97. /* ------------------------------------------------------------ */
  98. /** Get raw HTTP responses from raw HTTP requests.
  99. * Multiple requests and responses may be handled, but only if
  100. * persistent connections conditions apply.
  101. * @param rawRequests String of raw HTTP requests
  102. * @return String of raw HTTP responses
  103. * @throws Exception
  104. */
  105. public String getResponses(String rawRequests) throws Exception
  106. {
  107. return _connector.getResponses(rawRequests);
  108. }
  109. /* ------------------------------------------------------------ */
  110. /** Get raw HTTP responses from raw HTTP requests.
  111. * Multiple requests and responses may be handled, but only if
  112. * persistent connections conditions apply.
  113. * @param rawRequests String of raw HTTP requests
  114. * @param connector The connector to handle the responses
  115. * @return String of raw HTTP responses
  116. * @throws Exception
  117. */
  118. public String getResponses(String rawRequests, LocalConnector connector) throws Exception
  119. {
  120. return connector.getResponses(rawRequests);
  121. }
  122. /* ------------------------------------------------------------ */
  123. /** Get raw HTTP responses from raw HTTP requests.
  124. * Multiple requests and responses may be handled, but only if
  125. * persistent connections conditions apply.
  126. * @param rawRequests String of raw HTTP requests
  127. * @return String of raw HTTP responses
  128. * @throws Exception
  129. */
  130. public ByteArrayBuffer getResponses(ByteArrayBuffer rawRequests) throws Exception
  131. {
  132. return _connector.getResponses(rawRequests,false);
  133. }
  134. /* ------------------------------------------------------------ */
  135. /** Create a Socket connector.
  136. * This methods adds a socket connector to the server
  137. * @param localhost if true, only listen on local host, else listen on all interfaces.
  138. * @return A URL to access the server via the socket connector.
  139. * @throws Exception
  140. */
  141. public String createSocketConnector(boolean localhost)
  142. throws Exception
  143. {
  144. synchronized (this)
  145. {
  146. SocketConnector connector = new SocketConnector();
  147. if (localhost)
  148. connector.setHost("127.0.0.1");
  149. _server.addConnector(connector);
  150. if (_server.isStarted())
  151. connector.start();
  152. else
  153. connector.open();
  154. return "http://127.0.0.1:"+connector.getLocalPort();
  155. }
  156. }
  157. /* ------------------------------------------------------------ */
  158. /** Create a SelectChannel connector.
  159. * This methods adds a select channel connector to the server
  160. * @return A URL to access the server via the connector.
  161. * @throws Exception
  162. */
  163. public String createChannelConnector(boolean localhost)
  164. throws Exception
  165. {
  166. synchronized (this)
  167. {
  168. SelectChannelConnector connector = new SelectChannelConnector();
  169. if (localhost)
  170. connector.setHost("127.0.0.1");
  171. _server.addConnector(connector);
  172. if (_server.isStarted())
  173. connector.start();
  174. else
  175. connector.open();
  176. return "http://"+(localhost?"127.0.0.1":
  177. InetAddress.getLocalHost().getHostAddress()
  178. )+":"+connector.getLocalPort();
  179. }
  180. }
  181. /* ------------------------------------------------------------ */
  182. /** Create a local connector.
  183. * This methods adds a local connector to the server
  184. * @return The LocalConnector object
  185. * @throws Exception
  186. */
  187. public LocalConnector createLocalConnector()
  188. throws Exception
  189. {
  190. synchronized (this)
  191. {
  192. LocalConnector connector = new LocalConnector();
  193. _server.addConnector(connector);
  194. if (_server.isStarted())
  195. connector.start();
  196. return connector;
  197. }
  198. }
  199. /* ------------------------------------------------------------ */
  200. /**
  201. * @param listener
  202. * @see org.eclipse.jetty.server.handler.ContextHandler#addEventListener(java.util.EventListener)
  203. */
  204. public void addEventListener(EventListener listener)
  205. {
  206. _context.addEventListener(listener);
  207. }
  208. /* ------------------------------------------------------------ */
  209. /**
  210. * @param filterClass
  211. * @param pathSpec
  212. * @param dispatches
  213. * @return the FilterHolder
  214. * @see org.eclipse.jetty.servlet.ServletContextHandler#addFilter(java.lang.Class, java.lang.String, int)
  215. */
  216. public FilterHolder addFilter(Class filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
  217. {
  218. return _context.addFilter(filterClass,pathSpec,dispatches);
  219. }
  220. /* ------------------------------------------------------------ */
  221. /**
  222. * @param filterClass
  223. * @param pathSpec
  224. * @param dispatches
  225. * @return the FilterHolder
  226. * @see org.eclipse.jetty.servlet.ServletContextHandler#addFilter(java.lang.String, java.lang.String, int)
  227. */
  228. public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
  229. {
  230. return _context.addFilter(filterClass,pathSpec,dispatches);
  231. }
  232. /* ------------------------------------------------------------ */
  233. /**
  234. * @param servlet
  235. * @param pathSpec
  236. * @return the ServletHolder
  237. * @see org.eclipse.jetty.servlet.ServletContextHandler#addServlet(java.lang.Class, java.lang.String)
  238. */
  239. public ServletHolder addServlet(Class servlet, String pathSpec)
  240. {
  241. return _context.addServlet(servlet,pathSpec);
  242. }
  243. /* ------------------------------------------------------------ */
  244. /**
  245. * @param className
  246. * @param pathSpec
  247. * @return the ServletHolder
  248. * @see org.eclipse.jetty.servlet.ServletContextHandler#addServlet(java.lang.String, java.lang.String)
  249. */
  250. public ServletHolder addServlet(String className, String pathSpec)
  251. {
  252. return _context.addServlet(className,pathSpec);
  253. }
  254. /* ------------------------------------------------------------ */
  255. /**
  256. * @param name
  257. * @return the Attribute object
  258. * @see org.eclipse.jetty.servlet.ServletContextHandler#getAttribute(java.lang.String)
  259. */
  260. public Object getAttribute(String name)
  261. {
  262. return _context.getAttribute(name);
  263. }
  264. /* ------------------------------------------------------------ */
  265. /**
  266. * @return the Attribute Names
  267. * @see org.eclipse.jetty.servlet.ServletContextHandler#getAttributeNames()
  268. */
  269. public Enumeration getAttributeNames()
  270. {
  271. return _context.getAttributeNames();
  272. }
  273. /* ------------------------------------------------------------ */
  274. /**
  275. * @return the attributes
  276. * @see org.eclipse.jetty.servlet.ServletContextHandler#getAttributes()
  277. */
  278. public Attributes getAttributes()
  279. {
  280. return _context.getAttributes();
  281. }
  282. /* ------------------------------------------------------------ */
  283. /**
  284. * @return the resource base
  285. * @see org.eclipse.jetty.servlet.ServletContextHandler#getResourceBase()
  286. */
  287. public String getResourceBase()
  288. {
  289. return _context.getResourceBase();
  290. }
  291. /* ------------------------------------------------------------ */
  292. /**
  293. * @param name
  294. * @param value
  295. * @see org.eclipse.jetty.servlet.ServletContextHandler#setAttribute(java.lang.String, java.lang.Object)
  296. */
  297. public void setAttribute(String name, Object value)
  298. {
  299. _context.setAttribute(name,value);
  300. }
  301. /* ------------------------------------------------------------ */
  302. /**
  303. * @param classLoader
  304. * @see org.eclipse.jetty.servlet.ServletContextHandler#setClassLoader(java.lang.ClassLoader)
  305. */
  306. public void setClassLoader(ClassLoader classLoader)
  307. {
  308. _context.setClassLoader(classLoader);
  309. }
  310. /* ------------------------------------------------------------ */
  311. /**
  312. * @param contextPath
  313. * @see org.eclipse.jetty.servlet.ServletContextHandler#setContextPath(java.lang.String)
  314. */
  315. public void setContextPath(String contextPath)
  316. {
  317. _context.setContextPath(contextPath);
  318. }
  319. /* ------------------------------------------------------------ */
  320. /**
  321. * @param eventListeners
  322. * @see org.eclipse.jetty.servlet.ServletContextHandler#setEventListeners(java.util.EventListener[])
  323. */
  324. public void setEventListeners(EventListener[] eventListeners)
  325. {
  326. _context.setEventListeners(eventListeners);
  327. }
  328. /* ------------------------------------------------------------ */
  329. /**
  330. * @param resourceBase
  331. * @see org.eclipse.jetty.servlet.ServletContextHandler#setResourceBase(java.lang.String)
  332. */
  333. public void setResourceBase(String resourceBase)
  334. {
  335. _context.setResourceBase(resourceBase);
  336. }
  337. }