/
JavaScriptSignatureServlet.java
90 lines (77 loc) · 3.36 KB
/
JavaScriptSignatureServlet.java
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright 2006-2018 WebPKI.org (http://webpki.org).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.webpki.webapps.jsf;
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.webpki.json.JSONObjectReader;
import org.webpki.json.JSONObjectWriter;
import org.webpki.json.JSONOutputFormats;
import org.webpki.json.JSONParser;
import org.webpki.util.Base64URL;
import org.webpki.webutil.ServletUtil;
public class JavaScriptSignatureServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static Logger logger = Logger.getLogger(JavaScriptSignatureServlet.class
.getName());
static void error(HttpServletResponse response, String error_message)
throws IOException, ServletException {
HTML.errorPage(response, error_message);
}
void showSignature(HttpServletRequest request,
HttpServletResponse response, byte[] signed_json)
throws IOException, ServletException {
JSONObjectReader parsed_json = JSONParser.parse(signed_json);
HTML.printResultPage(
response,
"<table>"
+ "<tr><td align=\"center\" style=\"font-weight:bolder;font-size:10pt;font-family:arial,verdana\">Signed JavaScript Object<br> </td></tr>"
+ "<tr><td align=\"left\">"
+ HTML.fancyBox(
"verify",
new String(
new JSONObjectWriter(parsed_json)
.serializeToBytes(JSONOutputFormats.PRETTY_JS_NATIVE),
"UTF-8").replace("\n", "<br>").replace(" ", " "))
+ "</td></tr>"
+ "</table>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
byte[] data = null;
if (request.getContentType().startsWith(
"application/x-www-form-urlencoded")) {
data = Base64URL.decode(request.getParameter(RequestServlet.JWS_ARGUMENT));
} else {
if (!request.getContentType().startsWith("application/json")) {
error(response, "Request didn't have the proper mime-type: "
+ request.getContentType());
return;
}
data = ServletUtil.getData(request);
}
try {
showSignature(request, response, data);
} catch (IOException e) {
HTML.errorPage(response, e.getMessage());
return;
}
}
}