1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty.plus.annotation;
17
18 import java.io.IOException;
19 import java.lang.reflect.InvocationTargetException;
20 import java.lang.reflect.Method;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 public class PojoServlet extends HttpServlet implements PojoWrapper
28 {
29 private Object _pojo;
30 private String _deleteMethodName;
31 private Method _deleteMethod;
32 private String _putMethodName;
33 private Method _putMethod;
34 private String _headMethodName;
35 private Method _headMethod;
36 private String _postMethodName;
37 private Method _postMethod;
38 private String _getMethodName;
39 private Method _getMethod;
40 private static final Class[] __params = new Class[]{HttpServletRequest.class, HttpServletResponse.class};
41
42 public PojoServlet (Object pojo)
43 {
44 if (pojo==null)
45 throw new IllegalArgumentException("Pojo is null");
46
47 _pojo=pojo;
48 }
49
50 public Object getPojo()
51 {
52 return _pojo;
53 }
54 public void setDeleteMethodName (String name)
55 {
56 _deleteMethodName = name;
57 }
58 public String getDeleteMethodName ()
59 {
60 return _deleteMethodName;
61 }
62 public void setPutMethodName (String name)
63 {
64 _putMethodName = name;
65 }
66 public String getPutMethodName ()
67 {
68 return _putMethodName;
69 }
70 public void setHeadMethodName (String name)
71 {
72 _headMethodName = name;
73 }
74
75 public String getHeadMethodName ()
76 {
77 return _headMethodName;
78 }
79 public void setPostMethodName (String name)
80 {
81 _postMethodName = name;
82 }
83 public String getPostMethodName ()
84 {
85 return _postMethodName;
86 }
87
88 public void setGetMethodName (String name)
89 {
90 _getMethodName = name;
91 }
92 public String getGetMethodName ()
93 {
94 return _getMethodName;
95 }
96
97
98 public void init() throws ServletException
99 {
100
101 try
102 {
103 if (_getMethodName != null)
104 _getMethod = _pojo.getClass().getDeclaredMethod(_getMethodName, __params);
105 if (_postMethodName != null)
106 _postMethod = _pojo.getClass().getDeclaredMethod(_postMethodName, __params);
107 if (_headMethodName != null)
108 _headMethod = _pojo.getClass().getDeclaredMethod(_headMethodName, __params);
109 if (_putMethodName != null)
110 _putMethod = _pojo.getClass().getDeclaredMethod(_putMethodName, __params);
111 if (_deleteMethodName != null)
112 _deleteMethod = _pojo.getClass().getDeclaredMethod(_deleteMethodName, __params);
113 }
114 catch (NoSuchMethodException e)
115 {
116 throw new ServletException (e);
117 }
118 super.init();
119 }
120
121
122 protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
123 throws ServletException, IOException
124 {
125 invoke (_deleteMethod, req, resp);
126 }
127
128
129 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
130 throws ServletException, IOException
131 {
132 invoke(_getMethod,req, resp);
133 }
134
135
136 protected void doHead(HttpServletRequest req, HttpServletResponse resp)
137 throws ServletException, IOException
138 {
139 invoke(_headMethod, req, resp);
140 }
141
142
143 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
144 throws ServletException, IOException
145 {
146 invoke(_postMethod, req, resp);
147 }
148
149
150 protected void doPut(HttpServletRequest req, HttpServletResponse resp)
151 throws ServletException, IOException
152 {
153 invoke(_putMethod, req, resp);
154 }
155
156 private void invoke (Method method, HttpServletRequest req, HttpServletResponse resp)
157 throws ServletException
158 {
159 if (method == null)
160 throw new ServletException ("No method");
161
162 try
163 {
164 method.invoke(_pojo, new Object[]{req, resp});
165 }
166 catch (IllegalAccessException e)
167 {
168 throw new ServletException (e);
169 }
170 catch (InvocationTargetException e)
171 {
172 throw new ServletException (e);
173 }
174 }
175
176 }