1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
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;
public class app extends HttpServlet
{
private String id;
private class RequestWrapper extends HttpServletRequestWrapper
{
public RequestWrapper(HttpServletRequest r)
{
super(r);
}
}
private class ResponseWrapper extends HttpServletResponseWrapper
{
public ResponseWrapper(HttpServletResponse r)
{
super(r);
}
}
@Override
public void init(ServletConfig sc)
throws ServletException
{
id = sc.getInitParameter("id");
}
private RequestDispatcher getRequestDispatcher(HttpServletRequest request, String str)
{
String disp = request.getParameter("disp");
if (disp != null && disp.equals("ctx")) {
return request.getServletContext().getRequestDispatcher(str);
}
if (disp != null && disp.equals("name")) {
return request.getServletContext().getNamedDispatcher(str);
}
if (disp == null || disp.equals("req")) {
return request.getRequestDispatcher(str);
}
return null;
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
String dtype = "" + request.getDispatcherType();
response.addHeader("X-" + dtype + "-Id", id);
response.addHeader("X-" + dtype + "-Request-URI", "" + request.getRequestURI());
response.addHeader("X-" + dtype + "-Servlet-Path", "" + request.getServletPath());
response.addHeader("X-" + dtype + "-Path-Info", "" + request.getPathInfo());
response.addHeader("X-" + dtype + "-Query-String", "" + request.getQueryString());
response.addHeader("X-" + dtype + "-Dispatcher-Type", "" + request.getDispatcherType());
response.setContentType("text/plain; charset=utf-8");
Map<String, String[]> pmap = request.getParameterMap();
for (Map.Entry<String,String[]> p : pmap.entrySet()) {
response.addHeader("X-" + dtype + "-Param-" + p.getKey(), "" + String.join(",", p.getValue()));
}
PrintWriter out = response.getWriter();
if (id.equals("fwd")) {
String uri = request.getParameter("uri");
if (uri != null && request.getDispatcherType() != DispatcherType.FORWARD) {
response.addHeader("X-Forward-To", "" + uri);
out.println("Before forwarding.");
RequestDispatcher d = getRequestDispatcher(request, uri);
if (d == null) {
out.println("Dispatcher is null");
return;
}
try {
d.forward(new RequestWrapper(request), new ResponseWrapper(response));
} catch(Exception e) {
response.addHeader("X-Exception", "" + e);
}
response.addHeader("X-After-Forwarding", "you-should-not-see-this");
out.println("After forwarding.");
return;
}
}
if (id.equals("data")) {
response.addHeader("X-" + RequestDispatcher.FORWARD_REQUEST_URI, "" + request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI));
response.addHeader("X-" + RequestDispatcher.FORWARD_CONTEXT_PATH, "" + request.getAttribute(RequestDispatcher.FORWARD_CONTEXT_PATH));
response.addHeader("X-" + RequestDispatcher.FORWARD_SERVLET_PATH, "" + request.getAttribute(RequestDispatcher.FORWARD_SERVLET_PATH));
response.addHeader("X-" + RequestDispatcher.FORWARD_PATH_INFO, "" + request.getAttribute(RequestDispatcher.FORWARD_PATH_INFO));
response.addHeader("X-" + RequestDispatcher.FORWARD_QUERY_STRING, "" + request.getAttribute(RequestDispatcher.FORWARD_QUERY_STRING));
out.println("app.doGet(): #" + this + ", " + id);
out.println("RequestURI: " + request.getRequestURI());
out.println("ServletPath: " + request.getServletPath());
out.println("PathInfo: " + request.getPathInfo());
out.println("DispType: " + request.getDispatcherType());
out.println("QueryString: " + request.getQueryString());
for (Map.Entry<String,String[]> p : pmap.entrySet()) {
out.println("- " + p.getKey() + "=" + String.join(",", p.getValue()));
}
return;
}
response.sendError(404);
}
}
|