1
2
3
4
5
6
7
8
9
10
11
12
13
14 package waffle.servlet;
15
16 import java.io.IOException;
17 import java.security.Principal;
18 import java.util.Enumeration;
19
20 import javax.servlet.ServletException;
21 import javax.servlet.http.HttpServlet;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.transform.OutputKeys;
26 import javax.xml.transform.Transformer;
27 import javax.xml.transform.TransformerConfigurationException;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 import waffle.util.WaffleInfo;
37
38
39
40
41 public class WaffleInfoServlet extends HttpServlet {
42
43
44 private static final long serialVersionUID = 1L;
45
46
47
48
49
50
51 @Override
52 public void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
53 IOException {
54 this.getWaffleInfoResponse(request, response);
55 }
56
57
58
59
60
61
62 @Override
63 public void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException,
64 IOException {
65 this.getWaffleInfoResponse(request, response);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public void getWaffleInfoResponse(final HttpServletRequest request, final HttpServletResponse response)
81 throws IOException, ServletException {
82 final WaffleInfo info = new WaffleInfo();
83 try {
84 final Document doc = info.getWaffleInfo();
85 final Element root = doc.getDocumentElement();
86
87
88 final Element http = this.getRequestInfo(doc, request);
89 root.insertBefore(http, root.getFirstChild());
90
91
92 final String[] lookup = request.getParameterValues("lookup");
93 if (lookup != null) {
94 for (final String name : lookup) {
95 root.appendChild(info.getLookupInfo(doc, name));
96 }
97 }
98
99
100 final TransformerFactory transfac = TransformerFactory.newInstance();
101 final Transformer trans = transfac.newTransformer();
102 trans.setOutputProperty(OutputKeys.INDENT, "yes");
103
104 final StreamResult result = new StreamResult(response.getWriter());
105 final DOMSource source = new DOMSource(doc);
106 trans.transform(source, result);
107 response.setContentType("application/xml");
108 } catch (final ParserConfigurationException e) {
109 throw new ServletException(e);
110 } catch (final TransformerConfigurationException e) {
111 throw new ServletException(e);
112 } catch (final TransformerException e) {
113 throw new ServletException(e);
114 }
115 }
116
117
118
119
120
121
122
123
124
125
126 private Element getRequestInfo(final Document doc, final HttpServletRequest request) {
127 final Element node = doc.createElement("request");
128
129 Element value = doc.createElement("AuthType");
130 value.setTextContent(request.getAuthType());
131 node.appendChild(value);
132
133 final Principal p = request.getUserPrincipal();
134 if (p != null) {
135 final Element child = doc.createElement("principal");
136 child.setAttribute("class", p.getClass().getName());
137
138 value = doc.createElement("name");
139 value.setTextContent(p.getName());
140 child.appendChild(value);
141
142 value = doc.createElement("string");
143 value.setTextContent(p.toString());
144 child.appendChild(value);
145
146 node.appendChild(child);
147 }
148
149 final Enumeration<?> headers = request.getHeaderNames();
150 if (headers.hasMoreElements()) {
151 String name;
152 final Element child = doc.createElement("headers");
153 while (headers.hasMoreElements()) {
154 name = (String) headers.nextElement();
155
156 value = doc.createElement(name);
157 value.setTextContent(request.getHeader(name));
158 child.appendChild(value);
159 }
160 node.appendChild(child);
161 }
162 return node;
163 }
164 }