1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.servlet;
25
26 import jakarta.servlet.http.HttpServlet;
27 import jakarta.servlet.http.HttpServletRequest;
28 import jakarta.servlet.http.HttpServletResponse;
29
30 import java.io.IOException;
31 import java.security.Principal;
32 import java.util.Collections;
33 import java.util.List;
34
35 import javax.xml.XMLConstants;
36 import javax.xml.parsers.ParserConfigurationException;
37 import javax.xml.transform.OutputKeys;
38 import javax.xml.transform.Transformer;
39 import javax.xml.transform.TransformerException;
40 import javax.xml.transform.TransformerFactory;
41 import javax.xml.transform.dom.DOMSource;
42 import javax.xml.transform.stream.StreamResult;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.w3c.dom.Document;
47 import org.w3c.dom.Element;
48
49 import waffle.util.WaffleInfo;
50
51
52
53
54 public class WaffleInfoServlet extends HttpServlet {
55
56
57 private static final long serialVersionUID = 1L;
58
59
60 private static final Logger logger = LoggerFactory.getLogger(WaffleInfoServlet.class);
61
62 @Override
63 public void doGet(final HttpServletRequest request, final HttpServletResponse response) {
64 this.getWaffleInfoResponse(request, response);
65 }
66
67 @Override
68 public void doPost(final HttpServletRequest request, final HttpServletResponse response) {
69 this.getWaffleInfoResponse(request, response);
70 }
71
72
73
74
75
76
77
78
79
80 public void getWaffleInfoResponse(final HttpServletRequest request, final HttpServletResponse response) {
81 final WaffleInfo info = new WaffleInfo();
82 try {
83 final Document doc = info.getWaffleInfo();
84 final Element root = doc.getDocumentElement();
85
86
87 final Element http = this.getRequestInfo(doc, request);
88 root.insertBefore(http, root.getFirstChild());
89
90
91 final String[] lookup = request.getParameterValues("lookup");
92 if (lookup != null) {
93 for (final String name : lookup) {
94 root.appendChild(info.getLookupInfo(doc, name));
95 }
96 }
97
98
99 final TransformerFactory transfac = TransformerFactory.newInstance();
100 transfac.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
101 transfac.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
102
103 final Transformer trans = transfac.newTransformer();
104 trans.setOutputProperty(OutputKeys.INDENT, "yes");
105
106 final StreamResult result = new StreamResult(response.getWriter());
107 final DOMSource source = new DOMSource(doc);
108 trans.transform(source, result);
109 response.setContentType("application/xml");
110 } catch (final ParserConfigurationException | TransformerException | IOException e) {
111 WaffleInfoServlet.logger.error("", e);
112 throw new RuntimeException("See logs for underlying error condition");
113 }
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 List<String> headers = Collections.list(request.getHeaderNames());
150 if (!headers.isEmpty()) {
151 final Element child = doc.createElement("headers");
152 for (String header : headers) {
153 value = doc.createElement(header);
154 value.setTextContent(request.getHeader(header));
155 child.appendChild(value);
156 }
157 node.appendChild(child);
158 }
159 return node;
160 }
161
162 }