View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.test;
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("<HEAD><link rel=\"stylesheet\" type=\"text/css\"  href=\"stylesheet.css\"/></HEAD>");
68              out.println("<h1>Jetty DeclareRoles Annotation Results</h1>");
69              out.println("<body>");
70              
71              out.println("<h2>Roles</h2>");
72              boolean result = request.isUserInRole("other");
73              out.println("<br/><b>Result: isUserInRole(\"other\")="+result+":"+ (result==false?" <span class=\"pass\">PASS":" <span class=\"fail\">FAIL")+"</span></b>");
74  
75              result = request.isUserInRole("manager");
76              out.println("<br/><b>Result: isUserInRole(\"manager\")="+result+":"+ (result?" <span class=\"pass\">PASS":" <span class=\"fail\">FAIL")+"</span></b>");
77              result = request.isUserInRole("user");
78              out.println("<br/><b>Result: isUserInRole(\"user\")="+result+":"+ (result?" <span class=\"pass\">PASS":" <span class=\"fail\">FAIL")+"</span></b>");
79              String context = _config.getServletContext().getContextPath();
80              if (!context.endsWith("/"))
81                  context += "/";
82              
83              out.println("<p><A HREF=\""+context+"logout.jsp\">Logout</A></p>");
84              
85              out.println("</body>");            
86              out.println("</html>");
87              out.flush();
88          }
89          catch (Exception e)
90          {
91              throw new ServletException(e);
92          }
93      }
94  
95  }