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;
20  import java.io.IOException;
21  import java.io.OutputStreamWriter;
22  import java.io.PrintWriter;
23  
24  import javax.servlet.ServletConfig;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServlet;
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.eclipse.jetty.util.StringUtil;
31  
32  
33  
34  
35  /* ------------------------------------------------------------ */
36  /** Rego Servlet - tests being accessed from servlet 3.0 programmatic 
37   * configuration.
38   * 
39   */
40  public class RegTest extends HttpServlet
41  {
42      
43      /* ------------------------------------------------------------ */
44      @Override
45      public void init(ServletConfig config) throws ServletException
46      {
47          super.init(config);
48      }
49  
50      /* ------------------------------------------------------------ */
51      @Override
52      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
53      {
54          doGet(request, response);
55      }
56  
57      /* ------------------------------------------------------------ */
58      @Override
59      public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException
60      { 
61          request.setCharacterEncoding("UTF-8");        
62          PrintWriter pout=null;
63          
64          try
65          {
66              pout =response.getWriter();
67          }
68          catch(IllegalStateException e)
69          {
70              pout=new PrintWriter(new OutputStreamWriter(response.getOutputStream(),"UTF-8"));
71          }
72     
73          try
74          {
75              pout.write("<html>\n<body>\n");
76              pout.write("<h1>Rego Servlet</h1>\n");
77              pout.write("<table width=\"95%\">");
78              pout.write("<tr>\n");
79              pout.write("<th align=\"right\">getMethod:&nbsp;</th>");
80              pout.write("<td>" + notag(request.getMethod())+"</td>");
81              pout.write("</tr><tr>\n");
82              pout.write("<th align=\"right\">getContentLength:&nbsp;</th>");
83              pout.write("<td>"+Integer.toString(request.getContentLength())+"</td>");
84              pout.write("</tr><tr>\n");
85              pout.write("<th align=\"right\">getContentType:&nbsp;</th>");
86              pout.write("<td>"+notag(request.getContentType())+"</td>");
87              pout.write("</tr><tr>\n");
88              pout.write("<th align=\"right\">getRequestURI:&nbsp;</th>");
89              pout.write("<td>"+notag(request.getRequestURI())+"</td>");
90              pout.write("</tr><tr>\n");
91              pout.write("<th align=\"right\">getRequestURL:&nbsp;</th>");
92              pout.write("<td>"+notag(request.getRequestURL().toString())+"</td>");
93              pout.write("</tr><tr>\n");
94              pout.write("<th align=\"right\">getContextPath:&nbsp;</th>");
95              pout.write("<td>"+request.getContextPath()+"</td>");
96              pout.write("</tr><tr>\n");
97              pout.write("<th align=\"right\">getServletPath:&nbsp;</th>");
98              pout.write("<td>"+notag(request.getServletPath())+"</td>");
99              pout.write("</tr><tr>\n");
100             pout.write("<th align=\"right\">getPathInfo:&nbsp;</th>");
101             pout.write("<td>"+notag(request.getPathInfo())+"</td>");
102             pout.write("</tr><tr>\n");
103             pout.write("<th align=\"right\">getPathTranslated:&nbsp;</th>");
104             pout.write("<td>"+notag(request.getPathTranslated())+"</td>");
105             pout.write("</tr><tr>\n");
106             pout.write("<th align=\"right\">getQueryString:&nbsp;</th>");
107             pout.write("<td>"+notag(request.getQueryString())+"</td>");
108             pout.write("</tr><tr>\n");
109             
110             pout.write("<th align=\"right\">getProtocol:&nbsp;</th>");
111             pout.write("<td>"+request.getProtocol()+"</td>");
112             pout.write("</tr><tr>\n");
113             pout.write("<th align=\"right\">getScheme:&nbsp;</th>");
114             pout.write("<td>"+request.getScheme()+"</td>");
115             pout.write("</tr><tr>\n");
116             pout.write("<th align=\"right\">getServerName:&nbsp;</th>");
117             pout.write("<td>"+notag(request.getServerName())+"</td>");
118             pout.write("</tr><tr>\n");
119             pout.write("<th align=\"right\">getServerPort:&nbsp;</th>");
120             pout.write("<td>"+Integer.toString(request.getServerPort())+"</td>");
121             pout.write("</tr><tr>\n");
122             pout.write("<th align=\"right\">getLocalName:&nbsp;</th>");
123             pout.write("<td>"+request.getLocalName()+"</td>");
124             pout.write("</tr><tr>\n");
125             pout.write("<th align=\"right\">getLocalAddr:&nbsp;</th>");
126             pout.write("<td>"+request.getLocalAddr()+"</td>");
127             pout.write("</tr><tr>\n");
128             pout.write("<th align=\"right\">getLocalPort:&nbsp;</th>");
129             pout.write("<td>"+Integer.toString(request.getLocalPort())+"</td>");
130             pout.write("</tr><tr>\n");
131             pout.write("<th align=\"right\">getRemoteUser:&nbsp;</th>");
132             pout.write("<td>"+request.getRemoteUser()+"</td>");
133             pout.write("</tr><tr>\n");
134             pout.write("<th align=\"right\">getUserPrincipal:&nbsp;</th>");
135             pout.write("<td>"+request.getUserPrincipal()+"</td>");
136             pout.write("</tr><tr>\n");
137             pout.write("<th align=\"right\">getRemoteAddr:&nbsp;</th>");
138             pout.write("<td>"+request.getRemoteAddr()+"</td>");
139             pout.write("</tr><tr>\n");
140             pout.write("<th align=\"right\">getRemoteHost:&nbsp;</th>");
141             pout.write("<td>"+request.getRemoteHost()+"</td>");
142             pout.write("</tr><tr>\n");
143             pout.write("<th align=\"right\">getRemotePort:&nbsp;</th>");
144             pout.write("<td>"+request.getRemotePort()+"</td>");
145             pout.write("</tr><tr>\n");
146             pout.write("<th align=\"right\">getRequestedSessionId:&nbsp;</th>");
147             pout.write("<td>"+request.getRequestedSessionId()+"</td>");
148             pout.write("</tr><tr>\n");
149             pout.write("<th align=\"right\">isSecure():&nbsp;</th>");
150             pout.write("<td>"+request.isSecure()+"</td>");
151 
152             pout.write("</tr><tr>\n");
153             pout.write("<th align=\"right\">isUserInRole(admin):&nbsp;</th>");
154             pout.write("<td>"+request.isUserInRole("admin")+"</td>");
155 
156             pout.write("</tr></table>");
157    
158         }
159         catch (Exception e)
160         {
161             getServletContext().log("dump "+e);
162         }
163         
164         
165         pout.write("</body>\n</html>\n");
166         
167         pout.close();
168     }
169 
170 
171     /* ------------------------------------------------------------ */
172     @Override
173     public String getServletInfo()
174     {
175         return "Rego Servlet";
176     }
177 
178     /* ------------------------------------------------------------ */
179     @Override
180     public synchronized void destroy()
181     {
182     }
183 
184     
185     private String notag(String s)
186     {
187         if (s==null)
188             return "null";
189         s=StringUtil.replace(s,"&","&amp;");
190         s=StringUtil.replace(s,"<","&lt;");
191         s=StringUtil.replace(s,">","&gt;");
192         return s;
193     }
194 }