1   //========================================================================
2   //$Id: PojoAnnotationTest.java 3363 2008-07-22 13:40:59Z janb $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  package com.acme;
16  import java.io.IOException;
17  
18  import javax.annotation.PostConstruct;
19  import javax.annotation.PreDestroy;
20  import javax.annotation.Resource;
21  import javax.annotation.security.RunAs;
22  import javax.servlet.ServletException;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  import javax.servlet.http.HttpSession;
26  import javax.servlet.http.annotation.Servlet;
27  import javax.servlet.http.annotation.InitParam;
28  import javax.servlet.http.annotation.jaxrs.GET;
29  import javax.servlet.http.annotation.jaxrs.POST;
30      
31  
32  
33  @Servlet(urlMappings = { "/ttt/*" }, 
34          name="PojoAnnotationTest",  
35          initParams={@InitParam(name="x", value="y")})
36  @RunAs("special")        
37  public class PojoAnnotationTest
38  {
39      @Resource(mappedName="maxAmount")
40      private Double maxAmount;
41      
42      boolean postConstruct;
43      boolean preDestroy;
44      
45      @PostConstruct
46      private void myPostConstructMethod ()
47      {       
48          postConstruct = true;
49      }
50      
51      @PreDestroy
52      private void myPreDestroyMethod()
53      {
54          preDestroy = true;
55      }
56      
57      @GET()
58      @POST()
59      public void anything (HttpServletRequest request, HttpServletResponse response)
60      throws ServletException, IOException
61      {
62          response.setContentType("text/html");
63          response.getWriter().println("<h1>Pojo Servlet</h1>");
64          response.getWriter().println("<p>Acting like a Servlet.</p>");
65          boolean result = (request.getMethod().equalsIgnoreCase("GET") || request.getMethod().equalsIgnoreCase("POST"));
66          response.getWriter().println("<p>Method = "+request.getMethod()+(result?" PASS":" FAIL")+"</p>");
67        
68          result = (maxAmount != null && maxAmount.doubleValue() == 55.0);
69          response.getWriter().println("<p>Resource injection maxAmount = "+maxAmount+(result?" PASS":" FAIL")+"</p>");
70          result = request.isUserInRole("special");
71          response.getWriter().println("<p>RunAs userIsInRole special = "+result+"</p>");
72          response.getWriter().println("<p>PostConstruct called "+postConstruct+"</p>");
73          response.getWriter().println("<p>PreDestroy not called "+!preDestroy+"</p>");
74         
75          response.getWriter().println("<p>ContextInitialized called "+request.getServletContext().getAttribute("contextInitialized"));
76  
77          HttpSession session = request.getSession(true);
78          Boolean filterResult = (Boolean)session.getAttribute("action");
79          result = (filterResult != null && filterResult.booleanValue());
80          response.getWriter().println("<p>Filter was called "+result+"</p>");     
81      }
82  }