/
ServerServlet.java
110 lines (95 loc) · 4.54 KB
/
ServerServlet.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
* Copyright (c) 2017 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
* Orange - keep one JSON dependency
*******************************************************************************/
package org.eclipse.leshan.server.bootstrap.demo.servlet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.eclipse.leshan.server.bootstrap.LeshanBootstrapServer;
import org.eclipse.leshan.server.bootstrap.endpoint.LwM2mBootstrapServerEndpoint;
import org.eclipse.leshan.server.core.demo.json.PublicKeySerDes;
import org.eclipse.leshan.server.core.demo.json.X509CertificateSerDes;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
public class ServerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final X509CertificateSerDes certificateSerDes;
private final PublicKeySerDes publicKeySerDes;
private final LeshanBootstrapServer server;
private final PublicKey publicKey;
private final X509Certificate serverCertificate;
public ServerServlet(LeshanBootstrapServer server, X509Certificate serverCertificate) {
this.server = server;
certificateSerDes = new X509CertificateSerDes();
publicKeySerDes = new PublicKeySerDes();
this.publicKey = null;
this.serverCertificate = serverCertificate;
}
public ServerServlet(LeshanBootstrapServer server, PublicKey serverPublicKey) {
this.server = server;
certificateSerDes = new X509CertificateSerDes();
publicKeySerDes = new PublicKeySerDes();
this.publicKey = serverPublicKey;
this.serverCertificate = null;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String[] path = StringUtils.split(req.getPathInfo(), '/');
if (path.length != 1) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if ("security".equals(path[0])) {
ObjectNode security = JsonNodeFactory.instance.objectNode();
if (publicKey != null) {
security.set("pubkey", publicKeySerDes.jSerialize(publicKey));
} else if (serverCertificate != null) {
security.set("certificate", certificateSerDes.jSerialize(serverCertificate));
}
resp.setContentType("application/json");
resp.getOutputStream().write(security.toString().getBytes(StandardCharsets.UTF_8));
resp.setStatus(HttpServletResponse.SC_OK);
return;
}
// search coap and coaps port
if ("endpoint".equals(path[0])) {
ArrayNode endpoints = JsonNodeFactory.instance.arrayNode();
for (LwM2mBootstrapServerEndpoint endpoint : server.getEndpoints()) {
ObjectNode ep = JsonNodeFactory.instance.objectNode();
ObjectNode uri = JsonNodeFactory.instance.objectNode();
ep.set("uri", uri);
uri.put("full", endpoint.getURI().toString());
uri.put("scheme", endpoint.getURI().getScheme());
uri.put("host", endpoint.getURI().getHost());
uri.put("port", endpoint.getURI().getPort());
ep.put("description", endpoint.getDescription());
endpoints.add(ep);
}
resp.setContentType("application/json");
resp.getOutputStream().write(endpoints.toString().getBytes(StandardCharsets.UTF_8));
resp.setStatus(HttpServletResponse.SC_OK);
return;
}
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
}
}