001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.geronimo.j2ee.deployment.annotation;
019:
020: import javax.annotation.security.DeclareRoles;
021: import javax.annotation.security.RunAs;
022:
023: import java.io.IOException;
024: import java.io.PrintWriter;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.Cookie;
027: import javax.servlet.http.HttpServlet;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: @DeclareRoles({"securityRole1","securityRole2","securityRole3"})
032: @RunAs("securityRole4")
033: public class SecurityAnnotationTest extends HttpServlet {
034:
035: public void doGet(HttpServletRequest request,
036: HttpServletResponse response) throws IOException,
037: ServletException {
038:
039: response.setContentType("text/html");
040:
041: PrintWriter out = response.getWriter();
042: out.println("<html>");
043: out.println("<body bgcolor=\"white\">");
044: out.println("<head>");
045:
046: String title = "cookies.title";
047: out.println("<title>" + title + "</title>");
048: out.println("</head>");
049: out.println("<body>");
050:
051: // relative links
052:
053: // XXX
054: // making these absolute till we work out the
055: // addition of a PathInfo issue
056:
057: out.println("<a href=\"../cookies.html\">");
058: out
059: .println("<img src=\"../images/code.gif\" height=24 "
060: + "width=24 align=right border=0 alt=\"view code\"></a>");
061: out.println("<a href=\"../index.html\">");
062: out.println("<img src=\"../images/return.gif\" height=24 "
063: + "width=24 align=right border=0 alt=\"return\"></a>");
064:
065: out.println("<h3>" + title + "</h3>");
066:
067: Cookie[] cookies = request.getCookies();
068: if ((cookies != null) && (cookies.length > 0)) {
069: out.println("cookies.cookies" + "<br>");
070: for (int i = 0; i < cookies.length; i++) {
071: Cookie cookie = cookies[i];
072: out.print("Cookie Name: " + cookie.getName() + "<br>");
073: out.println(" Cookie Value: " + cookie.getValue()
074: + "<br><br>");
075: }
076: } else {
077: out.println("cookies.no-cookies");
078: }
079:
080: String cookieName = request.getParameter("cookiename");
081: String cookieValue = request.getParameter("cookievalue");
082: if (cookieName != null && cookieValue != null) {
083: Cookie cookie = new Cookie(cookieName, cookieValue);
084: response.addCookie(cookie);
085: out.println("<P>");
086: out.println("cookies.set" + "<br>");
087: out.print("cookies.name" + " " + cookieName + "<br>");
088: out.print("cookies.value" + " " + cookieValue);
089: }
090:
091: out.println("<P>");
092: out.println("cookies.make-cookie" + "<br>");
093: out.print("<form action=\"");
094: out.println("CookieExample\" method=POST>");
095: out.print("cookies.name" + " ");
096: out.println("<input type=text length=20 name=cookiename><br>");
097: out.print("cookies.value" + " ");
098: out.println("<input type=text length=20 name=cookievalue><br>");
099: out.println("<input type=submit></form>");
100:
101: out.println("</body>");
102: out.println("</html>");
103: }
104:
105: public void doPost(HttpServletRequest request,
106: HttpServletResponse response) throws IOException,
107: ServletException {
108: doGet(request, response);
109: }
110:
111: }
|