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 java.io.IOException;
27  import java.security.Principal;
28  import java.util.Collections;
29  import java.util.List;
30  
31  import javax.servlet.http.HttpServlet;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.xml.XMLConstants;
35  import javax.xml.parsers.ParserConfigurationException;
36  import javax.xml.transform.OutputKeys;
37  import javax.xml.transform.Transformer;
38  import javax.xml.transform.TransformerException;
39  import javax.xml.transform.TransformerFactory;
40  import javax.xml.transform.dom.DOMSource;
41  import javax.xml.transform.stream.StreamResult;
42  
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  import org.w3c.dom.Document;
46  import org.w3c.dom.Element;
47  
48  import waffle.util.WaffleInfo;
49  
50  /**
51   * A servlet that returns WaffleInfo as XML.
52   */
53  public class WaffleInfoServlet extends HttpServlet {
54  
55      /** The Constant serialVersionUID. */
56      private static final long serialVersionUID = 1L;
57  
58      /** The Constant Logger. */
59      private static final Logger logger = LoggerFactory.getLogger(WaffleInfoServlet.class);
60  
61      @Override
62      public void doGet(final HttpServletRequest request, final HttpServletResponse response) {
63          this.getWaffleInfoResponse(request, response);
64      }
65  
66      @Override
67      public void doPost(final HttpServletRequest request, final HttpServletResponse response) {
68          this.getWaffleInfoResponse(request, response);
69      }
70  
71      /**
72       * Gets the waffle info response.
73       *
74       * @param request
75       *            the request
76       * @param response
77       *            the response
78       */
79      public void getWaffleInfoResponse(final HttpServletRequest request, final HttpServletResponse response) {
80          final WaffleInfo info = new WaffleInfo();
81          try {
82              final Document doc = info.getWaffleInfo();
83              final Element root = doc.getDocumentElement();
84  
85              // Add the Request Information Here
86              final Element http = this.getRequestInfo(doc, request);
87              root.insertBefore(http, root.getFirstChild());
88  
89              // Lookup Accounts By Name
90              final String[] lookup = request.getParameterValues("lookup");
91              if (lookup != null) {
92                  for (final String name : lookup) {
93                      root.appendChild(info.getLookupInfo(doc, name));
94                  }
95              }
96  
97              // Write the XML Response
98              final TransformerFactory transfac = TransformerFactory.newInstance();
99              transfac.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
100             transfac.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
101 
102             final Transformer trans = transfac.newTransformer();
103             trans.setOutputProperty(OutputKeys.INDENT, "yes");
104 
105             final StreamResult result = new StreamResult(response.getWriter());
106             final DOMSource source = new DOMSource(doc);
107             trans.transform(source, result);
108             response.setContentType("application/xml");
109         } catch (final ParserConfigurationException | TransformerException | IOException e) {
110             WaffleInfoServlet.logger.error("", e);
111             throw new RuntimeException("See logs for underlying error condition");
112         }
113     }
114 
115     /**
116      * Gets the request info.
117      *
118      * @param doc
119      *            the doc
120      * @param request
121      *            the request
122      *
123      * @return the request info
124      */
125     private Element getRequestInfo(final Document doc, final HttpServletRequest request) {
126         final Element node = doc.createElement("request");
127 
128         Element value = doc.createElement("AuthType");
129         value.setTextContent(request.getAuthType());
130         node.appendChild(value);
131 
132         final Principal p = request.getUserPrincipal();
133         if (p != null) {
134             final Element child = doc.createElement("principal");
135             child.setAttribute("class", p.getClass().getName());
136 
137             value = doc.createElement("name");
138             value.setTextContent(p.getName());
139             child.appendChild(value);
140 
141             value = doc.createElement("string");
142             value.setTextContent(p.toString());
143             child.appendChild(value);
144 
145             node.appendChild(child);
146         }
147 
148         final List<String> headers = Collections.list(request.getHeaderNames());
149         if (!headers.isEmpty()) {
150             final Element child = doc.createElement("headers");
151             for (String header : headers) {
152                 value = doc.createElement(header);
153                 value.setTextContent(request.getHeader(header));
154                 child.appendChild(value);
155             }
156             node.appendChild(child);
157         }
158         return node;
159     }
160 
161 }