diff --git a/jetty-osgi/jetty-osgi-servletbridge/src/main/java/org/eclipse/jetty/nested/Dump.java b/jetty-osgi/jetty-osgi-servletbridge/src/main/java/org/eclipse/jetty/nested/Dump.java new file mode 100644 index 00000000000..f11e02729c1 --- /dev/null +++ b/jetty-osgi/jetty-osgi-servletbridge/src/main/java/org/eclipse/jetty/nested/Dump.java @@ -0,0 +1,1122 @@ +package org.eclipse.jetty.nested; +// ======================================================================== +// Copyright (c) 1996-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + + +import java.io.BufferedWriter; +import java.io.File; +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.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; +import java.util.Date; +import java.util.Enumeration; +import java.util.Locale; +import java.util.Map.Entry; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletRequestWrapper; +import javax.servlet.ServletResponse; +import javax.servlet.ServletResponseWrapper; +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 javax.servlet.http.HttpServletResponseWrapper; +import javax.sql.DataSource; + +//import org.eclipse.jetty.continuation.Continuation; +//import org.eclipse.jetty.continuation.ContinuationListener; +//import org.eclipse.jetty.continuation.ContinuationSupport; +//import org.eclipse.jetty.http.HttpHeaders; +//import org.eclipse.jetty.util.StringUtil; +//import org.eclipse.jetty.util.log.Log; + + + +/* ------------------------------------------------------------ */ +/** Dump Servlet Request. + * + */ +public class Dump extends HttpServlet +{ + boolean fixed; + + /* ------------------------------------------------------------ */ + @Override + 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"))); + } + } + + /* ------------------------------------------------------------ */ + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + doGet(request, response); + } + + /* ------------------------------------------------------------ */ + @Override + public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException + { + // Handle a dump of data + final String data= request.getParameter("data"); + final String chars= request.getParameter("chars"); + final String block= request.getParameter("block"); + final String dribble= request.getParameter("dribble"); + final boolean flush= request.getParameter("flush")!=null?Boolean.parseBoolean(request.getParameter("flush")):false; + + + if(request.getPathInfo()!=null && request.getPathInfo().toLowerCase().indexOf("script")!=-1) + { + response.sendRedirect(response.encodeRedirectURL(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")); + if (request.getHeader(HttpHeaders.EXPECT)!=null &&request.getHeader(HttpHeaders.EXPECT).indexOf("102")>=0) + { + Thread.sleep(s/2); + response.sendError(102); + Thread.sleep(s/2); + } + else + Thread.sleep(s); + } + catch (InterruptedException e) + { + return; + } + catch (Exception e) + { + throw new ServletException(e); + } + } + + if (request.getAttribute("RESUME")==null && request.getParameter("resume")!=null) + { + request.setAttribute("RESUME",Boolean.TRUE); + + final long resume=Long.parseLong(request.getParameter("resume")); + new Thread(new Runnable() + { + public void run() + { + try + { + Thread.sleep(resume); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + Continuation continuation = ContinuationSupport.getContinuation(request); + continuation.resume(); + } + + }).start(); + } + + if (request.getParameter("complete")!=null) + { + final long complete=Long.parseLong(request.getParameter("complete")); + new Thread(new Runnable() + { + public void run() + { + try + { + Thread.sleep(complete); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + try + { + response.setContentType("text/html"); + response.getOutputStream().println("

COMPLETED

"); + Continuation continuation = ContinuationSupport.getContinuation(request); + continuation.complete(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + }).start(); + } + + if (request.getParameter("suspend")!=null && request.getAttribute("SUSPEND")!=Boolean.TRUE) + { + request.setAttribute("SUSPEND",Boolean.TRUE); + try + { + Continuation continuation = ContinuationSupport.getContinuation(request); + continuation.setTimeout(Long.parseLong(request.getParameter("suspend"))); + continuation.suspend(); + + continuation.addContinuationListener(new ContinuationListener() + { + public void onTimeout(Continuation continuation) + { + response.addHeader("Dump","onTimeout"); + try + { + dump(response,data,chars,block,dribble,flush); + continuation.complete(); + } + catch (IOException e) + { + Log.ignore(e); + } + } + + public void onComplete(Continuation continuation) + { + response.addHeader("Dump","onComplete"); + } + }); + + continuation.undispatch(); + } + 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 + if (dump(response,data,chars,block,dribble,flush)) + 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(response.encodeRedirectURL(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; + } + + // Handle a extra headers + String headers= request.getParameter("headers"); + if (headers != null && headers.length() > 0) + { + long h=Long.parseLong(headers); + for (int i=0;i 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("This text should be reset".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("\n\n"); + pout.write("

Dump Servlet

\n"); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + pout.write(""); + + Enumeration locales= request.getLocales(); + while (locales.hasMoreElements()) + { + pout.write("\n"); + pout.write(""); + pout.write(""); + } + pout.write("\n"); + + pout.write(""); + Enumeration h= request.getHeaderNames(); + String name; + while (h.hasMoreElements()) + { + name= (String)h.nextElement(); + + Enumeration h2= request.getHeaders(name); + while (h2.hasMoreElements()) + { + String hv= (String)h2.nextElement(); + pout.write("\n"); + pout.write(""); + pout.write(""); + } + } + + //Test the system properties + if ("true".equals(request.getParameter("env"))) + { + pout.write("\n"); + pout.write(""); + + for (Entry e : System.getProperties().entrySet()) + { + pout.write("\n"); + pout.write(""); + pout.write(""); + } + + } + + //handle testing jdbc connections: + String jdbcUrl = request.getParameter("jdbc-url"); + String jdbcDriver = request.getParameter("jdbc-driver"); + if (jdbcUrl != null) + { + Connection con = null; + try + { + String user = request.getParameter("jdbc-user"); + String pass = request.getParameter("jdbc-pass"); + String query = request.getParameter("jdbc-query"); + if (user.length() == 0) user = null; + if (pass.length() == 0) pass = null; + if (query == null || query.length() == 0) query = "show tables;"; + pout.write("\n"); + pout.write(""); + + Class driver = Thread.currentThread().getContextClassLoader().loadClass(jdbcDriver); + + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + + con = user != null ? DriverManager.getConnection(jdbcUrl, user, pass) : DriverManager.getConnection(jdbcUrl); + + Statement statement = con.createStatement(); + boolean success = statement.execute(query); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + + } + catch (Throwable t) + { + pout.write("\n"); + pout.write(""); + pout.write(""); + } + finally + { + if (con != null) + { + try { con.close(); } catch (Throwable ee) {} + } + } + } + + + pout.write("\n"); + pout.write(""); + h= request.getParameterNames(); + while (h.hasMoreElements()) + { + name= (String)h.nextElement(); + pout.write("\n"); + pout.write(""); + pout.write(""); + String[] values= request.getParameterValues(name); + if (values == null) + { + pout.write("\n"); + pout.write(""); + pout.write(""); + } + else if (values.length > 1) + { + for (int i= 0; i < values.length; i++) + { + pout.write("\n"); + pout.write(""); + pout.write(""); + } + } + } + + pout.write("\n"); + pout.write(""); + Cookie[] cookies = request.getCookies(); + for (int i=0; cookies!=null && i\n"); + pout.write(""); + pout.write(""); + } + + String content_type=request.getContentType(); + if (content_type!=null && + !content_type.startsWith("application/x-www-form-urlencoded") && + !content_type.startsWith("multipart/form-data")) + { + pout.write("\n"); + pout.write(""); + pout.write("\n"); + pout.write(""); + } + + pout.write("\n"); + pout.write(""); + Enumeration a= request.getAttributeNames(); + while (a.hasMoreElements()) + { + name= (String)a.nextElement(); + pout.write("\n"); + pout.write(""); + Object value=request.getAttribute(name); + if (value instanceof File) + { + File file = (File)value; + pout.write(""); + } + else + pout.write(""); + } + request.setAttribute("org.eclipse.jetty.servlet.MultiPartFilter.files",null); + + + pout.write("\n"); + pout.write(""); + a= getInitParameterNames(); + while (a.hasMoreElements()) + { + name= (String)a.nextElement(); + pout.write("\n"); + pout.write(""); + pout.write(""); + } + + pout.write("\n"); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + + String webinfRealPath = getServletContext().getRealPath("/WEB-INF/"); + if (webinfRealPath != null) + { + try + { + File webInfRealPathFile = new File(webinfRealPath); + pout.write("\n"); + pout.write(""); + pout.write(""); + if (webInfRealPathFile.exists() && webInfRealPathFile.isDirectory()) + { + File webxml = new File(webInfRealPathFile, "web.xml"); + pout.write("\n"); + pout.write(""); + pout.write(""); + } + } + catch (Throwable t) + { + pout.write(""); + pout.write(""); + } + } + + + pout.write("\n"); + pout.write(""); + pout.write(""); + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + a= getServletContext().getInitParameterNames(); + while (a.hasMoreElements()) + { + name= (String)a.nextElement(); + pout.write("\n"); + pout.write(""); + pout.write(""); + } + + pout.write("\n"); + pout.write(""); + a= getServletContext().getAttributeNames(); + while (a.hasMoreElements()) + { + name= (String)a.nextElement(); + pout.write("\n"); + pout.write(""); + pout.write(""); + } + + String res= request.getParameter("resource"); + if (res != null && res.length() > 0) + { + pout.write("\n"); + pout.write(""); + + pout.write("\n"); + pout.write(""); + + ServletContext context = getServletContext().getContext(res); + pout.write(""); + + if (context!=null) + { + String cp=context.getContextPath(); + if (cp==null || "/".equals(cp)) + cp=""; + pout.write("\n"); + pout.write(""); + pout.write(""); + } + + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + pout.write(""); + + pout.write("\n"); + pout.write(""); + try{pout.write("");} + catch(Exception e) {pout.write("");} + } + + + pout.write("
getMethod: " + notag(request.getMethod())+"
getContentLength: "+Integer.toString(request.getContentLength())+"
getContentType: "+notag(request.getContentType())+"
getRequestURI: "+notag(request.getRequestURI())+"
getRequestURL: "+notag(request.getRequestURL().toString())+"
getContextPath: "+request.getContextPath()+"
getServletPath: "+notag(request.getServletPath())+"
getPathInfo: "+notag(request.getPathInfo())+"
getPathTranslated: "+notag(request.getPathTranslated())+"
getQueryString: "+notag(request.getQueryString())+"
getProtocol: "+request.getProtocol()+"
getScheme: "+request.getScheme()+"
getServerName: "+notag(request.getServerName())+"
getServerPort: "+Integer.toString(request.getServerPort())+"
getLocalName: "+request.getLocalName()+"
getLocalAddr: "+request.getLocalAddr()+"
getLocalPort: "+Integer.toString(request.getLocalPort())+"
getRemoteUser: "+request.getRemoteUser()+"
getUserPrincipal: "+request.getUserPrincipal()+"
getRemoteAddr: "+request.getRemoteAddr()+"
getRemoteHost: "+request.getRemoteHost()+"
getRemotePort: "+request.getRemotePort()+"
getRequestedSessionId: "+request.getRequestedSessionId()+"
isSecure(): "+request.isSecure()+"
isUserInRole(admin): "+request.isUserInRole("admin")+"
getLocale: "+request.getLocale()+"
getLocales: "+locales.nextElement()+"

Other HTTP Headers:
"+notag(name)+": "+notag(hv)+"

Environment System-Properties: 
" + notag(String.valueOf(e.getKey())) + ": "+notag(String.valueOf(e.getValue()))+"

JDBC test: 
Driver class: "+driver.getName()+" loaded by " + driver.getClassLoader()+"
Connection url: "+jdbcUrl+"
User (optional): "+user+"
Is there a password (optional): "+(pass != null ? "yes" : "no" )+"
Query: "+query+"
Successful query: "+success+"
JDBC test error: "+t+"

Request Parameters:
"+notag(name)+": "+notag(request.getParameter(name))+"
"+notag(name)+" Values: "+"NULL!"+"
"+notag(name)+"["+i+"]: "+notag(values[i])+"

Cookies:
"+notag(cookie.getName())+": "+notag(cookie.getValue())+"

Content:
");
+                char[] content= new char[4096];
+                int len;
+                try{
+                    Reader in=request.getReader();
+                    
+                    while((len=in.read(content))>=0)
+                        pout.write(notag(new String(content,0,len)));
+                }
+                catch(IOException e)
+                {
+                    pout.write(e.toString());
+                }
+                
+                pout.write("

Request Attributes:
"+name.replace("."," .")+": "+"
" + file.getName()+" ("+file.length()+" "+new Date(file.lastModified())+ ")
"+"
"+"
" + toString(request.getAttribute(name)) + "
"+"

Servlet InitParameters:
"+name+": "+ toString(getInitParameter(name)) +"

ServletContext Misc:
"+"servletContext.getContextPath()"+": "+ getServletContext().getContextPath() + "
"+"getServletContext().getRealPath(\"/WEB-INF/\")"+": "+ getServletContext().getRealPath("/WEB-INF/") + "
"+"new File(getServletContext().getRealPath(\"/WEB-INF/\"))"+": exists()="+ webInfRealPathFile.exists() + + "; isFile()="+webInfRealPathFile.isFile() + + "; isDirectory()="+webInfRealPathFile.isDirectory() + + "; isAbsolute()=" + webInfRealPathFile.isAbsolute() + + "; canRead()=" + webInfRealPathFile.canRead() + + "; canWrite()=" + webInfRealPathFile.canWrite() + +"
"+"new File(getServletContext().getRealPath(\"/WEB-INF/web.xml\"))"+": exists()="+ webxml.exists() + + "; isFile()="+webxml.isFile() + + "; isDirectory()="+webxml.isDirectory() + + "; isAbsolute()=" + webxml.isAbsolute() + + "; canRead()=" + webxml.canRead() + + "; canWrite()=" + webxml.canWrite() + +""+"Error probing the java.io.File(getServletContext().getRealPath(\"/WEB-INF/\"))"+": "+ t + "
"+"getServletContext().getServerInfo()"+": "+ getServletContext().getServerInfo() + "
"+"getServletContext().getServletContextName()"+": "+ getServletContext().getServletContextName() + "

Context InitParameters:
"+name.replace("."," .")+": "+ toString(getServletContext().getInitParameter(name)) + "

Context Attributes:
"+name.replace("."," .")+": "+"
" + toString(getServletContext().getAttribute(name)) + "
"+"

Get Resource: \""+res+"\"
getServletContext().getContext(...): "+context+"
getServletContext().getContext(...),getRequestDispatcher(...): "+getServletContext().getContext(res).getRequestDispatcher(res.substring(cp.length()))+"
this.getClass().getResource(...): "+this.getClass().getResource(res)+"
this.getClass().getClassLoader().getResource(...): "+this.getClass().getClassLoader().getResource(res)+"
Thread.currentThread().getContextClassLoader().getResource(...): "+Thread.currentThread().getContextClassLoader().getResource(res)+"
getServletContext().getResource(...): "+getServletContext().getResource(res)+""+"" +e+"
\n"); + + /* ------------------------------------------------------------ */ + pout.write("

Request Wrappers

\n"); + ServletRequest rw=request; + int w=0; + while (rw !=null) + { + pout.write((w++)+": "+rw.getClass().getName()+"
"); + if (rw instanceof HttpServletRequestWrapper) + rw=((HttpServletRequestWrapper)rw).getRequest(); + else if (rw instanceof ServletRequestWrapper) + rw=((ServletRequestWrapper)rw).getRequest(); + else + rw=null; + } + + /* ------------------------------------------------------------ */ + pout.write("

Response Wrappers

\n"); + ServletResponse rsw=response; + w=0; + while (rsw !=null) + { + pout.write((w++)+": "+rsw.getClass().getName()+"
"); + if (rsw instanceof HttpServletResponseWrapper) + rsw=((HttpServletResponseWrapper)rsw).getResponse(); + else if (rsw instanceof ServletResponseWrapper) + rsw=((ServletResponseWrapper)rsw).getResponse(); + else + rsw=null; + } + + pout.write("
"); + pout.write("

International Characters (UTF-8)

"); + pout.write("LATIN LETTER SMALL CAPITAL AE
\n"); + pout.write("Directly uni encoded(\\u1d01): \u1d01
"); + pout.write("HTML reference (&AElig;): Æ
"); + pout.write("Decimal (&#7425;): ᴁ
"); + pout.write("Javascript unicode (\\u1d01) :
"); + pout.write("
"); + pout.write("

Form to generate GET content

"); + pout.write("
"); + pout.write("TextField:
\n"); + pout.write(""); + pout.write("
"); + + pout.write("
"); + + pout.write("

Form to generate POST content

"); + pout.write("
"); + pout.write("TextField:
\n"); + pout.write("Select:
"); + pout.write("
"); + pout.write("
"); + pout.write("
"); + + pout.write("

Form to generate UPLOAD content

"); + pout.write("
"); + pout.write("TextField:
\n"); + pout.write("File 1:
\n"); + pout.write("File 2:
\n"); + pout.write("
"); + pout.write("
"); + + pout.write("

Form to set Cookie

"); + pout.write("
"); + pout.write("cookie:
\n"); + pout.write("value:
\n"); + pout.write(""); + pout.write("
\n"); + + pout.write("

Form to get Resource

"); + pout.write("
"); + pout.write("resource:
\n"); + pout.write(""); + pout.write("
\n"); + + pout.write("

Form to test a JDBC connection URL

"); + pout.write("
"); + pout.write("JDBC Driver class:
\n"); + pout.write("JDBC URL:
\n"); + pout.write("JDBC Username:
\n"); + pout.write("JDBC Password:
\n"); + pout.write("JDBC Query:
\n"); + pout.write("
"); + pout.write("
"); + pout.write("
"); + + } + catch (Exception e) + { + getServletContext().log("dump", e); + } + + String lines= request.getParameter("lines"); + if (lines!=null) + { + char[] line = "A line of characters. Blah blah blah blah. blooble blooble
\n".toCharArray(); + for (int l=Integer.parseInt(lines);l-->0;) + { + pout.write(""+l+" "); + pout.write(line); + } + } + + pout.write("\n\n"); + + pout.close(); + + if (pi != null) + { + if ("/ex4".equals(pi)) + throw new ServletException("test ex4", new Throwable()); + if ("/ex5".equals(pi)) + throw new IOException("test ex5"); + if ("/ex6".equals(pi)) + throw new UnavailableException("test ex6"); + } + + + } + + + /* ------------------------------------------------------------ */ + @Override + public String getServletInfo() + { + return "Dump Servlet"; + } + + /* ------------------------------------------------------------ */ + @Override + public synchronized void destroy() + { + } + + /* ------------------------------------------------------------ */ + private String getURI(HttpServletRequest request) + { + String uri= (String)request.getAttribute("javax.servlet.forward.request_uri"); + if (uri == null) + uri= request.getRequestURI(); + return uri; + } + + /* ------------------------------------------------------------ */ + private static String toString(Object o) + { + if (o == null) + return null; + + try + { + if (o.getClass().isArray()) + { + StringBuffer sb = new StringBuffer(); + if (!o.getClass().getComponentType().isPrimitive()) + { + Object[] array= (Object[])o; + for (int i= 0; i < array.length; i++) + { + if (i > 0) + sb.append("\n"); + sb.append(array.getClass().getComponentType().getName()); + sb.append("["); + sb.append(i); + sb.append("]="); + sb.append(toString(array[i])); + } + return sb.toString(); + } + else + { + int length = Array.getLength(o); + for (int i=0;i 0) + sb.append("\n"); + sb.append(o.getClass().getComponentType().getName()); + sb.append("["); + sb.append(i); + sb.append("]="); + sb.append(toString(Array.get(o, i))); + } + return sb.toString(); + } + } + else + return o.toString(); + } + catch (Exception e) + { + return e.toString(); + } + } + + private boolean dump(HttpServletResponse response, String data, String chars, String block, String dribble, boolean flush) throws IOException + { + if (data != null && data.length() > 0) + { + long d=Long.parseLong(data); + int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50; + byte[] buf=new byte[b]; + for (int i=0;i 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,(int)d); + d=0; + } + + if (dribble!=null) + { + out.flush(); + try + { + Thread.sleep(Long.parseLong(dribble)); + } + catch (Exception e) + { + e.printStackTrace(); + break; + } + } + + } + + if (flush) + out.flush(); + + return true; + } + + // Handle a dump of data + if (chars != null && chars.length() > 0) + { + long d=Long.parseLong(chars); + int b=(block!=null&&block.length()>0)?Integer.parseInt(block):50; + char[] buf=new char[b]; + for (int i=0;i 0 && !out.checkError()) + { + 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,(int)d); + d=0; + } + } + return true; + } + return false; + } + + private String notag(String s) + { + if (s==null) + return "null"; + s=replace(s,"&","&"); + s=replace(s,"<","<"); + s=replace(s,">",">"); + return s; + } + + /** + * replace substrings within string. + */ + public static String replace(String s, String sub, String with) + { + int c=0; + int i=s.indexOf(sub,c); + if (i == -1) + return s; + + StringBuffer buf = new StringBuffer(s.length()+with.length()); + + synchronized(buf) + { + do + { + buf.append(s.substring(c,i)); + buf.append(with); + c=i+sub.length(); + } while ((i=s.indexOf(sub,c))!=-1); + + if (c