View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
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   * A servlet that returns WaffleInfo as XML.
40   */
41  public class WaffleInfoServlet extends HttpServlet {
42  
43      /** The Constant serialVersionUID. */
44      private static final long serialVersionUID = 1L;
45  
46      /*
47       * (non-Javadoc)
48       * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
49       * javax.servlet.http.HttpServletResponse)
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       * (non-Javadoc)
59       * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
60       * javax.servlet.http.HttpServletResponse)
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       * Gets the waffle info response.
70       *
71       * @param request
72       *            the request
73       * @param response
74       *            the response
75       * @throws IOException
76       *             Signals that an I/O exception has occurred.
77       * @throws ServletException
78       *             the servlet exception
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              // Add the Request Information Here
88              final Element http = this.getRequestInfo(doc, request);
89              root.insertBefore(http, root.getFirstChild());
90  
91              // Lookup Accounts By Name
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              // Write the XML Response
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      * Gets the request info.
119      *
120      * @param doc
121      *            the doc
122      * @param request
123      *            the request
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 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 }