001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.security.Principal;
027: import java.util.ArrayList;
028: import javax.servlet.ServletConfig;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServlet;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import org.jboss.util.Strings;
035:
036: /** A servlet that calls isUserInRole for every role name defined in the
037: * expectedUserRoles init parameter and validates that each role is assigned
038: * to the user. Any role in the expectedUserRoles for which isUserInRole is
039: * false is added to the X-ExpectedUserRoles-Errors reply header. If the user
040: * has every role from the expectedUserRoles list, the X-ExpectedUserRoles-Errors
041: * header will not be in the reply.
042: *
043: * This servlet also calls isUserInRole for every role name defined in the
044: * unexpectedUserRoles init parameter and validates that each role is NOT
045: * assigned to the user. Any role in the unexpectedUserRoles for which
046: * isUserInRole is true is added to the X-UnexpectedUserRoles-Errors reply
047: * header. If the user has no roles from the unexpectedUserRoles list, the
048: * X-UnexpectedUserRoles-Errors header will not be in the reply.
049: *
050: * @author Scott.Stark@jboss.org
051: * @version $Revision: 57211 $
052: */
053: public class UserInRoleServlet extends HttpServlet {
054: /** The roles for which isUserInRole should return true */
055: private String[] expectedUserRoles;
056: /** The roles for which isUserInRole should return false */
057: private String[] unexpectedUserRoles;
058:
059: public void init(ServletConfig config) throws ServletException {
060: super .init(config);
061: String param = config.getInitParameter("expectedUserRoles");
062: expectedUserRoles = Strings.split(param, ",");
063: param = config.getInitParameter("unexpectedUserRoles");
064: unexpectedUserRoles = Strings.split(param, ",");
065: }
066:
067: protected void processRequest(HttpServletRequest request,
068: HttpServletResponse response) throws ServletException,
069: IOException {
070: Principal user = request.getUserPrincipal();
071: response.setContentType("text/html");
072: PrintWriter out = response.getWriter();
073: out.println("<html>");
074: out.println("<head><title>UserInRoleServlet</title></head>");
075: out.println("<body>");
076: out.println("You have accessed this servlet as user:" + user);
077:
078: out.println("<h1>ExpectedUserRoles</h1>");
079: out.println("<ul>");
080: ArrayList errors = new ArrayList();
081: for (int n = 0; n < expectedUserRoles.length; n++) {
082: String role = expectedUserRoles[n];
083: boolean inRole = request.isUserInRole(role);
084: out.println("<li>isUserInRole(" + role + ") = " + inRole
085: + "</li>");
086: if (inRole == false)
087: errors.add(role);
088: }
089: out.println("</ul>");
090: if (errors.size() > 0) {
091: String value = errors.toString();
092: response.addHeader("X-ExpectedUserRoles-Errors", value);
093: }
094:
095: errors.clear();
096: out.println("<h1>UnexpectedUserRoles</h1>");
097: out.println("<ul>");
098: for (int n = 0; n < unexpectedUserRoles.length; n++) {
099: String role = unexpectedUserRoles[n];
100: boolean inRole = request.isUserInRole(role);
101: out.println("<li>isUserInRole(" + role + ") = " + inRole
102: + "</li>");
103: if (inRole == true)
104: errors.add(role);
105: }
106: if (errors.size() > 0) {
107: String value = errors.toString();
108: response.addHeader("X-UnexpectedUserRoles-Errors", value);
109: }
110: out.println("</ul>");
111:
112: out.println("</body></html>");
113: out.close();
114: }
115:
116: protected void doGet(HttpServletRequest request,
117: HttpServletResponse response) throws ServletException,
118: IOException {
119: processRequest(request, response);
120: }
121:
122: protected void doPost(HttpServletRequest request,
123: HttpServletResponse response) throws ServletException,
124: IOException {
125: processRequest(request, response);
126: }
127:
128: }
|