View Javadoc

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