📄 dump.java
字号:
// ========================================================================// Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package com.acme;import java.io.BufferedWriter;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.Reader;import java.lang.reflect.Array;import java.lang.reflect.Field;import java.util.Enumeration;import java.util.Locale;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletRequestWrapper;import javax.servlet.UnavailableException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import javax.servlet.http.HttpServletResponse;import org.mortbay.util.StringUtil;import org.mortbay.util.ajax.Continuation;import org.mortbay.util.ajax.ContinuationSupport;/* ------------------------------------------------------------ *//** Dump Servlet Request. * */public class Dump extends HttpServlet{ static boolean fixed; /* ------------------------------------------------------------ */ public void init(ServletConfig config) throws ServletException { super.init(config); if (config.getInitParameter("unavailable")!=null && !fixed) { fixed=true; throw new UnavailableException("Unavailable test",Integer.parseInt(config.getInitParameter("unavailable"))); } } /* ------------------------------------------------------------ */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /* ------------------------------------------------------------ */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if(request.getPathInfo()!=null && request.getPathInfo().toLowerCase().indexOf("script")!=-1) { response.sendRedirect(getServletContext().getContextPath() + "/dump/info"); return; } request.setCharacterEncoding("UTF-8"); if (request.getParameter("empty")!=null) { response.setStatus(200); response.flushBuffer(); return; } if (request.getParameter("sleep")!=null) { try { long s = Long.parseLong(request.getParameter("sleep")); Thread.sleep(s/2); response.sendError(102); Thread.sleep(s/2); } catch (InterruptedException e) { return; } catch (Exception e) { throw new ServletException(e); } } if (request.getParameter("continue")!=null) { try { Continuation continuation = ContinuationSupport.getContinuation(request, null); continuation.suspend(Long.parseLong(request.getParameter("continue"))); } catch(Exception e) { throw new ServletException(e); } } request.setAttribute("Dump", this); getServletContext().setAttribute("Dump",this); // getServletContext().log("dump "+request.getRequestURI()); // Force a content length response String length= request.getParameter("length"); if (length != null && length.length() > 0) { response.setContentLength(Integer.parseInt(length)); } // Handle a dump of data String data= request.getParameter("data"); String block= request.getParameter("block"); String dribble= request.getParameter("dribble"); if (data != null && data.length() > 0) { int d=Integer.parseInt(data); int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50; byte[] buf=new byte[b]; for (int i=0;i<b;i++) { buf[i]=(byte)('0'+(i%10)); if (i%10==9) buf[i]=(byte)'\n'; } buf[0]='o'; OutputStream out=response.getOutputStream(); response.setContentType("text/plain"); while (d > 0) { if (b==1) { out.write(d%80==0?'\n':'.'); d--; } else if (d>=b) { out.write(buf); d=d-b; } else { out.write(buf,0,d); d=0; } if (dribble!=null) { out.flush(); try { Thread.sleep(Long.parseLong(dribble)); } catch (Exception e) { e.printStackTrace(); break; } } } return; } // handle an exception String info= request.getPathInfo(); if (info != null && info.endsWith("Exception")) { try { throw (Throwable) Thread.currentThread().getContextClassLoader().loadClass(info.substring(1)).newInstance(); } catch (Throwable th) { throw new ServletException(th); } } // test a reset String reset= request.getParameter("reset"); if (reset != null && reset.length() > 0) { response.getOutputStream().println("THIS SHOULD NOT BE SEEN!"); response.setHeader("SHOULD_NOT","BE SEEN"); response.reset(); } // handle an redirect String redirect= request.getParameter("redirect"); if (redirect != null && redirect.length() > 0) { response.getOutputStream().println("THIS SHOULD NOT BE SEEN!"); response.sendRedirect(redirect); try { response.getOutputStream().println("THIS SHOULD NOT BE SEEN!"); } catch(IOException e) { // ignored as stream is closed. } return; } // handle an error String error= request.getParameter("error"); if (error != null && error.length() > 0 && request.getAttribute("javax.servlet.error.status_code")==null) { response.getOutputStream().println("THIS SHOULD NOT BE SEEN!"); response.sendError(Integer.parseInt(error)); try { response.getOutputStream().println("THIS SHOULD NOT BE SEEN!"); } catch(IllegalStateException e) { try { response.getWriter().println("NOR THIS!!"); } catch(IOException e2){} } catch(IOException e){} return; } String buffer= request.getParameter("buffer"); if (buffer != null && buffer.length() > 0) response.setBufferSize(Integer.parseInt(buffer)); String charset= request.getParameter("charset"); if (charset==null) charset="UTF-8"; response.setCharacterEncoding(charset); response.setContentType("text/html"); if (info != null && info.indexOf("Locale/") >= 0) { try { String locale_name= info.substring(info.indexOf("Locale/") + 7); Field f= java.util.Locale.class.getField(locale_name); response.setLocale((Locale)f.get(null)); } catch (Exception e) { e.printStackTrace(); response.setLocale(Locale.getDefault()); } } String cn= request.getParameter("cookie"); String cv=request.getParameter("cookiev"); if (cn!=null && cv!=null) { Cookie cookie= new Cookie(cn, cv); if (request.getParameter("version")!=null) cookie.setVersion(Integer.parseInt(request.getParameter("version"))); cookie.setComment("Cookie from dump servlet"); response.addCookie(cookie); } String pi= request.getPathInfo(); if (pi != null && pi.startsWith("/ex")) { OutputStream out= response.getOutputStream(); out.write("</H1>This text should be reset</H1>".getBytes()); if ("/ex0".equals(pi)) throw new ServletException("test ex0", new Throwable()); else if ("/ex1".equals(pi)) throw new IOException("test ex1"); else if ("/ex2".equals(pi)) throw new UnavailableException("test ex2"); else if (pi.startsWith("/ex3/")) throw new UnavailableException("test ex3",Integer.parseInt(pi.substring(5))); throw new RuntimeException("test"); } if ("true".equals(request.getParameter("close"))) response.setHeader("Connection","close"); String buffered= request.getParameter("buffered"); PrintWriter pout=null; try { pout =response.getWriter(); } catch(IllegalStateException e) { pout=new PrintWriter(new OutputStreamWriter(response.getOutputStream(),charset)); } if (buffered!=null) pout = new PrintWriter(new BufferedWriter(pout,Integer.parseInt(buffered))); try { pout.write("<html>\n<body>\n"); pout.write("<h1>Dump Servlet</h1>\n"); pout.write("<table width=\"95%\">"); pout.write("<tr>\n"); pout.write("<th align=\"right\">getMethod: </th>"); pout.write("<td>" + notag(request.getMethod())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getContentLength: </th>"); pout.write("<td>"+Integer.toString(request.getContentLength())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getContentType: </th>"); pout.write("<td>"+notag(request.getContentType())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getRequestURI: </th>"); pout.write("<td>"+notag(request.getRequestURI())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getRequestURL: </th>"); pout.write("<td>"+notag(request.getRequestURL().toString())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getContextPath: </th>"); pout.write("<td>"+request.getContextPath()+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getServletPath: </th>"); pout.write("<td>"+notag(request.getServletPath())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getPathInfo: </th>"); pout.write("<td>"+notag(request.getPathInfo())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getPathTranslated: </th>"); pout.write("<td>"+notag(request.getPathTranslated())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getQueryString: </th>"); pout.write("<td>"+notag(request.getQueryString())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getProtocol: </th>"); pout.write("<td>"+request.getProtocol()+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getScheme: </th>"); pout.write("<td>"+request.getScheme()+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getServerName: </th>"); pout.write("<td>"+notag(request.getServerName())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getServerPort: </th>"); pout.write("<td>"+Integer.toString(request.getServerPort())+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getLocalName: </th>"); pout.write("<td>"+request.getLocalName()+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getLocalAddr: </th>"); pout.write("<td>"+request.getLocalAddr()+"</td>"); pout.write("</tr><tr>\n"); pout.write("<th align=\"right\">getLocalPort: </th>"); pout.write("<td>"+Integer.toString(request.getLocalPort())+"</td>"); pout.write("</tr><tr>\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -