View Javadoc

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