1 17 18 19 20 package org.apache.lenya.cms.cocoon.generation; 21 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Enumeration ; 25 26 import javax.servlet.ServletConfig ; 27 import javax.servlet.ServletException ; 28 import javax.servlet.http.Cookie ; 29 import javax.servlet.http.HttpServlet ; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 34 35 public class TestProxyGeneratorServlet extends HttpServlet { 36 43 public void init(ServletConfig config) throws ServletException { 44 super.init(config); 45 } 46 47 56 public void doGet(HttpServletRequest request, HttpServletResponse response) 57 throws IOException , ServletException { 58 response.setContentType("text/xml"); 59 60 PrintWriter writer = response.getWriter(); 61 writer.print("<?xml version=\"1.0\"?>"); 62 writer.print("<servlet class=\"" + this.getClass().getName() + "\">"); 63 writer.print("<request method=\"GET\">"); 64 writer.print(getRequestInfo(request)); 65 writer.print(getParameters(request)); 66 writer.print(getSession(request)); 67 writer.print(getCookies(request)); 68 writer.print("</request>"); 69 writer.print("</servlet>"); 70 } 71 72 81 public void doPost(HttpServletRequest request, HttpServletResponse response) 82 throws ServletException , IOException { 83 response.setContentType("text/xml"); 84 85 PrintWriter writer = response.getWriter(); 86 writer.print("<?xml version=\"1.0\"?>"); 87 writer.print("<servlet class=\"" + this.getClass().getName() + "\">"); 88 writer.print("<request method=\"POST\">"); 89 writer.print(getRequestInfo(request)); 90 writer.print(getParameters(request)); 91 writer.print(getSession(request)); 92 writer.print(getCookies(request)); 93 writer.print("</request>"); 94 writer.print("</servlet>"); 95 } 96 97 104 public String getRequestInfo(HttpServletRequest request) { 105 StringBuffer sb = new StringBuffer (""); 106 sb.append("<URI>" + request.getRequestURI() + "</URI>"); 107 sb.append("<servername>" + request.getServerName() + "</servername>"); 108 sb.append("<serverport>" + request.getServerPort() + "</serverport>"); 109 110 return sb.toString(); 111 } 112 113 120 public String getParameters(HttpServletRequest request) { 121 StringBuffer sb = new StringBuffer (""); 122 Enumeration parameters = request.getParameterNames(); 123 boolean hasParameters = parameters.hasMoreElements(); 124 125 if (hasParameters) { 126 sb.append("<parameters>"); 127 } 128 129 while (parameters.hasMoreElements()) { 130 String name = (String ) parameters.nextElement(); 131 String [] values = request.getParameterValues(name); 132 sb.append("<parameter name=\"" + name + "\">"); 133 134 for (int i = 0; i < values.length; i++) { 135 sb.append("<value>" + values[i] + "</value>"); 136 } 137 138 sb.append("</parameter>"); 139 } 140 141 if (hasParameters) { 142 sb.append("</parameters>"); 143 } 144 145 return sb.toString(); 146 } 147 148 155 public String getSession(HttpServletRequest request) { 156 StringBuffer sb = new StringBuffer (""); 157 158 HttpSession session = request.getSession(false); 160 161 if (session != null) { 162 sb.append("<session>"); 163 164 Enumeration attributes = session.getAttributeNames(); 165 166 if (!attributes.hasMoreElements()) { 167 sb.append("<noattributes/>"); 168 } 169 170 while (attributes.hasMoreElements()) { 171 String attributeName = (String ) attributes.nextElement(); 172 sb.append("<attribute name=\"" + attributeName + "\">"); 173 sb.append("" + session.getAttribute(attributeName)); 174 sb.append("</attribute>"); 175 } 176 177 sb.append("</session>"); 178 } else { 179 sb.append("<nosession/>"); 180 } 181 182 return sb.toString(); 183 } 184 185 192 public String getCookies(HttpServletRequest request) { 193 StringBuffer sb = new StringBuffer (""); 194 195 Cookie [] cookies = request.getCookies(); 196 197 if (cookies != null) { 198 if (cookies.length > 0) { 199 sb.append("<cookies>"); 200 201 for (int i = 0; i < cookies.length; i++) { 202 sb.append("<cookie>"); 203 sb.append("<comment>" + cookies[i].getComment() + "</comment>"); 204 sb.append("<domain>" + cookies[i].getDomain() + "</domain>"); 205 sb.append("<maxage>" + cookies[i].getMaxAge() + "</maxage>"); 206 sb.append("<name>" + cookies[i].getName() + "</name>"); 207 sb.append("<path>" + cookies[i].getPath() + "</path>"); 208 sb.append("<secure>" + cookies[i].getSecure() + "</secure>"); 209 sb.append("<value>" + cookies[i].getValue() + "</value>"); 210 sb.append("<version>" + cookies[i].getVersion() + "</version>"); 211 sb.append("</cookie>"); 212 } 213 214 sb.append("</cookies>"); 215 } else { 216 sb.append("<nocookies/>"); 217 } 218 } else { 219 sb.append("<nocookies/>"); 220 } 221 222 return sb.toString(); 223 } 224 } 225 | Popular Tags |