View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2022 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * A servlet that returns WaffleInfo as XML.
53   */
54  public class WaffleInfoServlet extends HttpServlet {
55  
56      /** The Constant serialVersionUID. */
57      private static final long serialVersionUID = 1L;
58  
59      /** The Constant Logger. */
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       * Gets the waffle info response.
74       *
75       * @param request
76       *            the request
77       * @param response
78       *            the response
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              // Add the Request Information Here
87              final Element http = this.getRequestInfo(doc, request);
88              root.insertBefore(http, root.getFirstChild());
89  
90              // Lookup Accounts By Name
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              // Write the XML Response
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      * Gets the request info.
118      *
119      * @param doc
120      *            the doc
121      * @param request
122      *            the request
123      *
124      * @return the request info
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 }