View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.servlet;
20  
21  import java.net.InetAddress;
22  import java.nio.ByteBuffer;
23  import java.util.EnumSet;
24  import java.util.Enumeration;
25  import java.util.Map;
26  import javax.servlet.DispatcherType;
27  import javax.servlet.Filter;
28  import javax.servlet.Servlet;
29  
30  import org.eclipse.jetty.server.Connector;
31  import org.eclipse.jetty.server.LocalConnector;
32  import org.eclipse.jetty.server.ServerConnector;
33  import org.eclipse.jetty.server.Server;
34  import org.eclipse.jetty.util.Attributes;
35  import org.eclipse.jetty.util.component.ContainerLifeCycle;
36  import org.eclipse.jetty.util.resource.Resource;
37  
38  public class ServletTester extends ContainerLifeCycle
39  {
40      private final Server _server=new Server();
41      private final LocalConnector _connector=new LocalConnector(_server);
42      private final ServletContextHandler _context;
43      public void setVirtualHosts(String[] vhosts)
44      {
45          _context.setVirtualHosts(vhosts);
46      }
47  
48      public void addVirtualHosts(String[] virtualHosts)
49      {
50          _context.addVirtualHosts(virtualHosts);
51      }
52  
53      public ServletHolder addServlet(String className, String pathSpec)
54      {
55          return _context.addServlet(className,pathSpec);
56      }
57  
58      public ServletHolder addServlet(Class<? extends Servlet> servlet, String pathSpec)
59      {
60          return _context.addServlet(servlet,pathSpec);
61      }
62  
63      public void addServlet(ServletHolder servlet, String pathSpec)
64      {
65          _context.addServlet(servlet,pathSpec);
66      }
67  
68      public void addFilter(FilterHolder holder, String pathSpec, EnumSet<DispatcherType> dispatches)
69      {
70          _context.addFilter(holder,pathSpec,dispatches);
71      }
72  
73      public FilterHolder addFilter(Class<? extends Filter> filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
74      {
75          return _context.addFilter(filterClass,pathSpec,dispatches);
76      }
77  
78      public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
79      {
80          return _context.addFilter(filterClass,pathSpec,dispatches);
81      }
82  
83      public Object getAttribute(String name)
84      {
85          return _context.getAttribute(name);
86      }
87  
88      public Enumeration getAttributeNames()
89      {
90          return _context.getAttributeNames();
91      }
92  
93      public Attributes getAttributes()
94      {
95          return _context.getAttributes();
96      }
97  
98      public String getContextPath()
99      {
100         return _context.getContextPath();
101     }
102 
103     public String getInitParameter(String name)
104     {
105         return _context.getInitParameter(name);
106     }
107 
108     public String setInitParameter(String name, String value)
109     {
110         return _context.setInitParameter(name,value);
111     }
112 
113     public Enumeration getInitParameterNames()
114     {
115         return _context.getInitParameterNames();
116     }
117 
118     public Map<String, String> getInitParams()
119     {
120         return _context.getInitParams();
121     }
122 
123     public void removeAttribute(String name)
124     {
125         _context.removeAttribute(name);
126     }
127 
128     public void setAttribute(String name, Object value)
129     {
130         _context.setAttribute(name,value);
131     }
132 
133     public void setContextPath(String contextPath)
134     {
135         _context.setContextPath(contextPath);
136     }
137 
138     public Resource getBaseResource()
139     {
140         return _context.getBaseResource();
141     }
142 
143     public String getResourceBase()
144     {
145         return _context.getResourceBase();
146     }
147 
148     public void setResourceBase(String resourceBase)
149     {
150         _context.setResourceBase(resourceBase);
151     }
152 
153     private final ServletHandler _handler;
154 
155     public ServletTester()
156     {
157         this("/",ServletContextHandler.SECURITY|ServletContextHandler.SESSIONS);
158     }
159 
160     public ServletTester(String ctxPath)
161     {
162         this(ctxPath,ServletContextHandler.SECURITY|ServletContextHandler.SESSIONS);
163     }
164 
165     public ServletTester(String contextPath,int options)
166     {
167         _context=new ServletContextHandler(_server,contextPath,options);
168         _handler=_context.getServletHandler();
169         _server.setConnectors(new Connector[]{_connector});
170         addBean(_server);
171     }
172 
173     public ServletContextHandler getContext()
174     {
175         return _context;
176     }
177 
178     public String getResponses(String request) throws Exception
179     {
180         return _connector.getResponses(request);
181     }
182 
183     public ByteBuffer getResponses(ByteBuffer request) throws Exception
184     {
185         return _connector.getResponses(request);
186     }
187 
188     /* ------------------------------------------------------------ */
189     /** Create a port based connector.
190      * This methods adds a port connector to the server
191      * @return A URL to access the server via the connector.
192      * @throws Exception
193      */
194     public String createConnector(boolean localhost) throws Exception
195     {
196         ServerConnector connector = new ServerConnector(_server);
197         if (localhost)
198             connector.setHost("127.0.0.1");
199         _server.addConnector(connector);
200         if (_server.isStarted())
201             connector.start();
202         else
203             connector.open();
204 
205         return "http://"+(localhost?"127.0.0.1":
206             InetAddress.getLocalHost().getHostAddress()
207         )+":"+connector.getLocalPort();
208     }
209 
210     public LocalConnector createLocalConnector()
211     {
212         LocalConnector connector = new LocalConnector(_server);
213         _server.addConnector(connector);
214         return connector;
215     }
216 
217 
218 
219 }