View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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  
27  import javax.servlet.DispatcherType;
28  import javax.servlet.Filter;
29  import javax.servlet.Servlet;
30  
31  import org.eclipse.jetty.server.Connector;
32  import org.eclipse.jetty.server.LocalConnector;
33  import org.eclipse.jetty.server.Server;
34  import org.eclipse.jetty.server.ServerConnector;
35  import org.eclipse.jetty.util.Attributes;
36  import org.eclipse.jetty.util.component.ContainerLifeCycle;
37  import org.eclipse.jetty.util.resource.Resource;
38  
39  public class ServletTester extends ContainerLifeCycle
40  {
41      private final Server _server=new Server();
42      private final LocalConnector _connector=new LocalConnector(_server);
43      private final ServletContextHandler _context;
44      
45      public Server getServer()
46      {
47          return _server;
48      }
49      
50      public LocalConnector getConnector()
51      {
52          return _connector;
53      }
54      
55      public void setVirtualHosts(String[] vhosts)
56      {
57          _context.setVirtualHosts(vhosts);
58      }
59  
60      public void addVirtualHosts(String[] virtualHosts)
61      {
62          _context.addVirtualHosts(virtualHosts);
63      }
64  
65      public ServletHolder addServlet(String className, String pathSpec)
66      {
67          return _context.addServlet(className,pathSpec);
68      }
69  
70      public ServletHolder addServlet(Class<? extends Servlet> servlet, String pathSpec)
71      {
72          return _context.addServlet(servlet,pathSpec);
73      }
74  
75      public void addServlet(ServletHolder servlet, String pathSpec)
76      {
77          _context.addServlet(servlet,pathSpec);
78      }
79  
80      public void addFilter(FilterHolder holder, String pathSpec, EnumSet<DispatcherType> dispatches)
81      {
82          _context.addFilter(holder,pathSpec,dispatches);
83      }
84  
85      public FilterHolder addFilter(Class<? extends Filter> filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
86      {
87          return _context.addFilter(filterClass,pathSpec,dispatches);
88      }
89  
90      public FilterHolder addFilter(String filterClass, String pathSpec, EnumSet<DispatcherType> dispatches)
91      {
92          return _context.addFilter(filterClass,pathSpec,dispatches);
93      }
94  
95      public Object getAttribute(String name)
96      {
97          return _context.getAttribute(name);
98      }
99  
100     public Enumeration getAttributeNames()
101     {
102         return _context.getAttributeNames();
103     }
104 
105     public Attributes getAttributes()
106     {
107         return _context.getAttributes();
108     }
109 
110     public String getContextPath()
111     {
112         return _context.getContextPath();
113     }
114 
115     public String getInitParameter(String name)
116     {
117         return _context.getInitParameter(name);
118     }
119 
120     public String setInitParameter(String name, String value)
121     {
122         return _context.setInitParameter(name,value);
123     }
124 
125     public Enumeration getInitParameterNames()
126     {
127         return _context.getInitParameterNames();
128     }
129 
130     public Map<String, String> getInitParams()
131     {
132         return _context.getInitParams();
133     }
134 
135     public void removeAttribute(String name)
136     {
137         _context.removeAttribute(name);
138     }
139 
140     public void setAttribute(String name, Object value)
141     {
142         _context.setAttribute(name,value);
143     }
144 
145     public void setContextPath(String contextPath)
146     {
147         _context.setContextPath(contextPath);
148     }
149 
150     public Resource getBaseResource()
151     {
152         return _context.getBaseResource();
153     }
154 
155     public String getResourceBase()
156     {
157         return _context.getResourceBase();
158     }
159 
160     public void setResourceBase(String resourceBase)
161     {
162         _context.setResourceBase(resourceBase);
163     }
164 
165     private final ServletHandler _handler;
166 
167     public ServletTester()
168     {
169         this("/",ServletContextHandler.SECURITY|ServletContextHandler.SESSIONS);
170     }
171 
172     public ServletTester(String ctxPath)
173     {
174         this(ctxPath,ServletContextHandler.SECURITY|ServletContextHandler.SESSIONS);
175     }
176 
177     public ServletTester(String contextPath,int options)
178     {
179         _context=new ServletContextHandler(_server,contextPath,options);
180         _handler=_context.getServletHandler();
181         _server.setConnectors(new Connector[]{_connector});
182         addBean(_server);
183     }
184 
185     public ServletContextHandler getContext()
186     {
187         return _context;
188     }
189 
190     public String getResponses(String request) throws Exception
191     {
192         return _connector.getResponses(request);
193     }
194 
195     public ByteBuffer getResponses(ByteBuffer request) throws Exception
196     {
197         return _connector.getResponses(request);
198     }
199 
200     /* ------------------------------------------------------------ */
201     /** Create a port based connector.
202      * This methods adds a port connector to the server
203      * @return A URL to access the server via the connector.
204      * @throws Exception
205      */
206     public String createConnector(boolean localhost) throws Exception
207     {
208         ServerConnector connector = new ServerConnector(_server);
209         if (localhost)
210             connector.setHost("127.0.0.1");
211         _server.addConnector(connector);
212         if (_server.isStarted())
213             connector.start();
214         else
215             connector.open();
216 
217         return "http://"+(localhost?"127.0.0.1":
218             InetAddress.getLocalHost().getHostAddress()
219         )+":"+connector.getLocalPort();
220     }
221 
222     public LocalConnector createLocalConnector()
223     {
224         LocalConnector connector = new LocalConnector(_server);
225         _server.addConnector(connector);
226         return connector;
227     }
228 
229 
230 
231 }