1
2
3
4
5
6
7
8
9
10
11
12package security.servlet;
13
14import java.io.IOException;
15
16import javax.servlet.ServletException;
17import javax.servlet.http.HttpServlet;
18import javax.servlet.http.HttpServletRequest;
19import javax.servlet.http.HttpServletResponse;
20
21import security.command.Command;
22import security.command.CommandSet;
23import security.user.ServletUser;
24import security.user.UserContext;
25
26public class CommandServlet extends HttpServlet
27{
28 private CommandSet _commands;
29
30 public void init() throws ServletException
31 {
32 String commandSetClassName = getInitParameter("commands");
33 try
34 {
35 _commands = (CommandSet) buildCommandSet(commandSetClassName);
36 }
37 catch (ClassNotFoundException e)
38 {
39 throw new ServletException(CommandSet.class + " " + commandSetClassName + " not found", e);
40 }
41 catch (InstantiationException e)
42 {
43 throw new ServletException(CommandSet.class + " " + commandSetClassName + " cannot be instantiated", e);
44 }
45 catch (IllegalAccessException e)
46 {
47 throw new ServletException(CommandSet.class + " " + commandSetClassName + " cannot be accessed", e);
48 }
49 }
50
51 private CommandSet buildCommandSet(String commandSetClassName) throws ClassNotFoundException, ServletException,
52 InstantiationException, IllegalAccessException
53 {
54 Class< ? > commandSetClass = Class.forName(commandSetClassName);
55 if (!CommandSet.class.isInstance(commandSetClass))
56 {
57 throw new ServletException("Class " + commandSetClassName + " is not a " + CommandSet.class);
58 }
59 Object instance = commandSetClass.newInstance();
60 return (CommandSet) instance;
61 }
62
63 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
64 {
65 UserContext.begin(new ServletUser(request));
66
67 String path = request.getPathInfo();
68 while (path.length() > 0 && path.charAt(0) == '/')
69 {
70 path = path.substring(1);
71 }
72 if (path.length() == 0)
73 {
74 response.sendError(HttpServletResponse.SC_NOT_FOUND);
75 }
76 Command command = _commands.getCommand(path);
77 if (command == null)
78 {
79 response.sendError(HttpServletResponse.SC_NOT_FOUND);
80 }
81
82 command.execute(request, response, getServletContext());
83 }
84}
// Copyright 2007, Tim Vernum
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package security.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import security.command.Command;
import security.command.CommandSet;
import security.user.ServletUser;
import security.user.UserContext;
public class CommandServlet extends HttpServlet
{
private CommandSet _commands;
public void init() throws ServletException
{
String commandSetClassName = getInitParameter("commands");
try
{
_commands = (CommandSet) buildCommandSet(commandSetClassName);
}
catch (ClassNotFoundException e)
{
throw new ServletException(CommandSet.class + " " + commandSetClassName + " not found", e);
}
catch (InstantiationException e)
{
throw new ServletException(CommandSet.class + " " + commandSetClassName + " cannot be instantiated", e);
}
catch (IllegalAccessException e)
{
throw new ServletException(CommandSet.class + " " + commandSetClassName + " cannot be accessed", e);
}
}
private CommandSet buildCommandSet(String commandSetClassName) throws ClassNotFoundException, ServletException,
InstantiationException, IllegalAccessException
{
Class< ? > commandSetClass = Class.forName(commandSetClassName);
if (!CommandSet.class.isInstance(commandSetClass))
{
throw new ServletException("Class " + commandSetClassName + " is not a " + CommandSet.class);
}
Object instance = commandSetClass.newInstance();
return (CommandSet) instance;
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
UserContext.begin(new ServletUser(request));
String path = request.getPathInfo();
while (path.length() > 0 && path.charAt(0) == '/')
{
path = path.substring(1);
}
if (path.length() == 0)
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
Command command = _commands.getCommand(path);
if (command == null)
{
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
command.execute(request, response, getServletContext());
}
}