| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.servlet; |
| 34 | | | |
| 35 | | | import java.io.IOException; |
| 36 | | | import java.io.PrintWriter; |
| 37 | | | |
| 38 | | | import javax.servlet.http.HttpServlet; |
| 39 | | | import javax.servlet.http.HttpServletRequest; |
| 40 | | | import javax.servlet.http.HttpServletResponse; |
| 41 | | | |
| 42 | | | import org.jcoderz.commons.util.Base64Util; |
| 43 | | | import org.jcoderz.commons.util.Constants; |
| 44 | | | import org.jcoderz.commons.util.HexUtil; |
| 45 | | | import org.jcoderz.commons.util.XmlUtil; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | @author |
| 55 | | | |
| 56 | 0 | | public class Base64DecoderServlet |
| 57 | | | extends HttpServlet |
| 58 | | | { |
| 59 | | | private static final long serialVersionUID = 1L; |
| 60 | | | private static final String ENCODED_PARAMETER_NAME = "encoded"; |
| 61 | | | |
| 62 | | | {@inheritDoc} |
| 63 | | | protected void doPost (HttpServletRequest request, |
| 64 | | | HttpServletResponse response) |
| 65 | | | throws IOException |
| 66 | | | |
| 67 | | | { |
| 68 | 0 | | doGet(request, response); |
| 69 | 0 | | } |
| 70 | | | |
| 71 | | | {@inheritDoc} |
| 72 | | | protected void doGet (HttpServletRequest request, |
| 73 | | | HttpServletResponse response) |
| 74 | | | throws IOException |
| 75 | | | { |
| 76 | 0 | | final PrintWriter out = response.getWriter(); |
| 77 | 0 | | final String encodedData = request.getParameter(ENCODED_PARAMETER_NAME); |
| 78 | 0 | | final byte[] data = Base64Util.decode(encodedData); |
| 79 | | | |
| 80 | 0 | | response.setContentType("text/html"); |
| 81 | 0 | | out.println("<html><head><title>Simple base64 decoder</title>"); |
| 82 | 0 | | out.println("</head>"); |
| 83 | 0 | | out.println("<body>"); |
| 84 | 0 | | out.println("<form method='post'>"); |
| 85 | 0 | | out.println("<textarea tabindex='1' name='" + ENCODED_PARAMETER_NAME |
| 86 | | | + "' rows='5' cols='100' wrap='soft'>"); |
| 87 | 0 | (1) | if (encodedData != null && data == null) |
| 88 | | | { |
| 89 | 0 | | out.println(XmlUtil.escape(encodedData)); |
| 90 | | | } |
| 91 | 0 | | out.println("</textarea>"); |
| 92 | 0 | | out.println("<input tabindex='2' type='submit' value='Decode' " |
| 93 | | | + "name='Decode' accesskey='d' title='Decode message [alt-d]'/>"); |
| 94 | 0 | | out.println("</form>"); |
| 95 | | | |
| 96 | 0 | | if (encodedData != null) |
| 97 | | | { |
| 98 | 0 | | if (data != null) |
| 99 | | | { |
| 100 | 0 | | dumpResult(out, data); |
| 101 | | | } |
| 102 | | | else |
| 103 | | | { |
| 104 | 0 | (2) | out.println("<hr />"); |
| 105 | 0 | | out.println("<h2>Invalid base64 data!</h2>"); |
| 106 | 0 | | out.println("<hr />"); |
| 107 | | | } |
| 108 | | | } |
| 109 | | | |
| 110 | 0 | | out.println("</body>"); |
| 111 | 0 | | out.println("</html>"); |
| 112 | 0 | | } |
| 113 | | | |
| 114 | | | private void dumpResult (PrintWriter out, byte[] data) |
| 115 | | | throws IOException |
| 116 | | | { |
| 117 | 0 | | out.println("<hr />"); |
| 118 | 0 | | final String xml |
| 119 | | | = XmlUtil.formatXml(new String(data, Constants.ENCODING_UTF8)); |
| 120 | 0 | | if (xml != null) |
| 121 | | | { |
| 122 | 0 | | out.println("<pre>"); |
| 123 | 0 | | out.println(XmlUtil.escape(xml)); |
| 124 | 0 | | out.println("</pre>"); |
| 125 | 0 | | out.println("<hr />"); |
| 126 | | | } |
| 127 | | | |
| 128 | | | |
| 129 | 0 | | final String hexDump = HexUtil.dump(data); |
| 130 | 0 | | out.println("<pre>"); |
| 131 | 0 | | out.println(XmlUtil.escape(hexDump)); |
| 132 | 0 | | out.println("</pre>"); |
| 133 | 0 | | out.println("<hr />"); |
| 134 | 0 | | } |
| 135 | | | |
| 136 | | | {@inheritDoc} |
| 137 | | | public String getServletInfo () |
| 138 | | | { |
| 139 | 0 | | return "Simple base64 decoder servlet."; |
| 140 | | | } |
| 141 | | | |
| 142 | | | } |