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 com.acme;
20  
21  import java.io.IOException;
22  
23  import javax.annotation.security.DeclareRoles;
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.ServletOutputStream;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  /**
32   * RoleAnnotationTest
33   * 
34   * Use DeclareRolesAnnotations from within Jetty.
35   * 
36   *
37   */
38  
39  
40  @DeclareRoles({"server-administrator","user"})
41  public class RoleAnnotationTest extends HttpServlet 
42  {
43      private ServletConfig _config;
44      
45      public void init(ServletConfig config) throws ServletException
46      {
47          super.init(config);
48          _config = config;
49      }
50  
51      
52      
53      /* ------------------------------------------------------------ */
54      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
55      {
56          doGet(request, response);
57      }
58  
59      /* ------------------------------------------------------------ */
60      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
61      {      
62          try
63          {
64              response.setContentType("text/html");
65              ServletOutputStream out = response.getOutputStream();
66              out.println("<html>");
67              out.println("<h1>Jetty DeclareRoles Annotation Results</h1>");
68              out.println("<body>");
69              
70              out.println("<h2>Roles</h2>");
71              boolean result = request.isUserInRole("other");
72              out.println("<br/><b>Result: isUserInRole(\"other\")="+result+":"+ (result==false?" PASS":" FAIL")+"</b>");
73  
74              result = request.isUserInRole("manager");
75              out.println("<br/><b>Result: isUserInRole(\"manager\")="+result+":"+ (result?" PASS":" FAIL")+"</b>");
76              result = request.isUserInRole("user");
77              out.println("<br/><b>Result: isUserInRole(\"user\")="+result+":"+ (result==false?" PASS":" FAIL")+"</b>");
78              String context = _config.getServletContext().getContextPath();
79              if (!context.endsWith("/"))
80                  context += "/";
81              
82              out.println("<p><A HREF=\""+context+"logout.jsp\">Logout</A></p>");
83              
84              out.println("</body>");            
85              out.println("</html>");
86              out.flush();
87          }
88          catch (Exception e)
89          {
90              throw new ServletException(e);
91          }
92      }
93  
94  }