comparison src/org/eclipse/jetty/server/handler/DebugHandler.java @ 802:3428c60d7cfc

replace jetty jars with source
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Sep 2016 21:15:48 -0600
parents
children
comparison
equal deleted inserted replaced
801:6a21393191c1 802:3428c60d7cfc
1 //
2 // ========================================================================
3 // Copyright (c) 1995-2014 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 org.eclipse.jetty.server.handler;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.PrintStream;
24 import java.util.Locale;
25
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.eclipse.jetty.server.Request;
31 import org.eclipse.jetty.server.Response;
32 import org.eclipse.jetty.util.DateCache;
33 import org.eclipse.jetty.util.RolloverFileOutputStream;
34
35
36 /**
37 * Debug Handler.
38 * A lightweight debug handler that can be used in production code.
39 * Details of the request and response are written to an output stream
40 * and the current thread name is updated with information that will link
41 * to the details in that output.
42 */
43 public class DebugHandler extends HandlerWrapper
44 {
45 private DateCache _date=new DateCache("HH:mm:ss", Locale.US);
46 private OutputStream _out;
47 private PrintStream _print;
48
49 /* ------------------------------------------------------------ */
50 /*
51 * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
52 */
53 @Override
54 public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
55 throws IOException, ServletException
56 {
57 final Response base_response = baseRequest.getResponse();
58 final Thread thread=Thread.currentThread();
59 final String old_name=thread.getName();
60
61 boolean suspend=false;
62 boolean retry=false;
63 String name=(String)request.getAttribute("org.eclipse.jetty.thread.name");
64 if (name==null)
65 name=old_name+":"+baseRequest.getScheme()+"://"+baseRequest.getLocalAddr()+":"+baseRequest.getLocalPort()+baseRequest.getUri();
66 else
67 retry=true;
68
69 String ex=null;
70 try
71 {
72 final String d=_date.now();
73 final int ms=_date.lastMs();
74
75 if (retry)
76 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" RETRY");
77 else
78 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+baseRequest.getRemoteAddr()+" "+request.getMethod()+" "+baseRequest.getHeader("Cookie")+"; "+baseRequest.getHeader("User-Agent"));
79 thread.setName(name);
80
81 getHandler().handle(target,baseRequest,request,response);
82 }
83 catch(IOException ioe)
84 {
85 ex=ioe.toString();
86 throw ioe;
87 }
88 catch(ServletException se)
89 {
90 ex=se.toString()+":"+se.getCause();
91 throw se;
92 }
93 catch(RuntimeException rte)
94 {
95 ex=rte.toString();
96 throw rte;
97 }
98 catch(Error e)
99 {
100 ex=e.toString();
101 throw e;
102 }
103 finally
104 {
105 thread.setName(old_name);
106 final String d=_date.now();
107 final int ms=_date.lastMs();
108 suspend=baseRequest.getAsyncContinuation().isSuspended();
109 if (suspend)
110 {
111 request.setAttribute("org.eclipse.jetty.thread.name",name);
112 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" SUSPEND");
113 }
114 else
115 _print.println(d+(ms>99?".":(ms>9?".0":".00"))+ms+":"+name+" "+base_response.getStatus()+
116 (ex==null?"":("/"+ex))+
117 " "+base_response.getContentType()+" "+base_response.getContentCount());
118 }
119 }
120
121 /* (non-Javadoc)
122 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
123 */
124 @Override
125 protected void doStart() throws Exception
126 {
127 if (_out==null)
128 _out=new RolloverFileOutputStream("./logs/yyyy_mm_dd.debug.log",true);
129 _print=new PrintStream(_out);
130 super.doStart();
131 }
132
133 /* (non-Javadoc)
134 * @see org.eclipse.jetty.server.handler.HandlerWrapper#doStop()
135 */
136 @Override
137 protected void doStop() throws Exception
138 {
139 super.doStop();
140 _print.close();
141 }
142
143 /**
144 * @return the out
145 */
146 public OutputStream getOutputStream()
147 {
148 return _out;
149 }
150
151 /**
152 * @param out the out to set
153 */
154 public void setOutputStream(OutputStream out)
155 {
156 _out = out;
157 }
158 }