-
Notifications
You must be signed in to change notification settings - Fork 402
/
DefaultBootstrapSession.java
214 lines (185 loc) · 7.29 KB
/
DefaultBootstrapSession.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*******************************************************************************
* 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
*******************************************************************************/
package org.eclipse.leshan.server.bootstrap;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.leshan.core.model.LwM2mModel;
import org.eclipse.leshan.core.request.BootstrapDownlinkRequest;
import org.eclipse.leshan.core.request.BootstrapRequest;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.request.Identity;
import org.eclipse.leshan.core.response.LwM2mResponse;
import org.eclipse.leshan.core.util.RandomStringUtils;
import org.eclipse.leshan.core.util.Validate;
/**
* A default implementation for {@link BootstrapSession}
*/
public class DefaultBootstrapSession implements BootstrapSession {
private final String id;
private final String endpoint;
private final Identity identity;
private final boolean authorized;
private final ContentFormat contentFormat;
private final Map<String, String> applicationData;
private final long creationTime;
private final URI endpointUsed;
private final BootstrapRequest request;
private volatile LwM2mModel model;
private volatile List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests;
private volatile List<LwM2mResponse> responses;
private volatile boolean moreTasks = false;
private volatile boolean cancelled = false;
/**
* Create a {@link DefaultBootstrapSession} using default client preferred content format (see
* {@link BootstrapRequest#getPreferredContentFormat()} or {@link ContentFormat#TLV} content format if there is no
* preferences and using <code>System.currentTimeMillis()</code> to set the creation time.
*
* @param request The bootstrap request which initiate the session.
* @param identity The transport layer identity of the device.
* @param authorized True if device is authorized to bootstrap.
* @param applicationData Data that could be attached to a session.
*/
public DefaultBootstrapSession(BootstrapRequest request, Identity identity, boolean authorized,
Map<String, String> applicationData, URI endpointUsed) {
this(request, identity, authorized, null, applicationData, endpointUsed);
}
/**
* Create a {@link DefaultBootstrapSession} using <code>System.currentTimeMillis()</code> to set the creation time.
*
* @param request The bootstrap request which initiate the session.
* @param identity The transport layer identity of the device.
* @param authorized True if device is authorized to bootstrap.
* @param contentFormat The content format to use to write object.
* @param applicationData Data that could be attached to a session.
*/
public DefaultBootstrapSession(BootstrapRequest request, Identity identity, boolean authorized,
ContentFormat contentFormat, Map<String, String> applicationData, URI endpointUsed) {
this(request, identity, authorized, contentFormat, applicationData, System.currentTimeMillis(), endpointUsed);
}
/**
* Create a {@link DefaultBootstrapSession}.
*
* @param request The bootstrap request which initiate the session.
* @param identity The transport layer identity of the device.
* @param authorized True if device is authorized to bootstrap.
* @param contentFormat The content format to use to write object.
* @param applicationData Data that could be attached to a session.
* @param creationTime The creation time of this session in ms.
*/
public DefaultBootstrapSession(BootstrapRequest request, Identity identity, boolean authorized,
ContentFormat contentFormat, Map<String, String> applicationData, long creationTime, URI endpointUsed) {
Validate.notNull(request);
this.id = RandomStringUtils.random(10, true, true);
this.request = request;
this.endpoint = request.getEndpointName();
this.identity = identity;
this.endpointUsed = endpointUsed;
this.authorized = authorized;
if (contentFormat == null) {
if (request.getPreferredContentFormat() != null) {
this.contentFormat = request.getPreferredContentFormat();
} else {
this.contentFormat = ContentFormat.TLV;
}
} else {
this.contentFormat = contentFormat;
}
if (applicationData == null) {
this.applicationData = Collections.emptyMap();
} else {
this.applicationData = Collections.unmodifiableMap(new HashMap<>(applicationData));
}
this.creationTime = creationTime;
}
@Override
public String getId() {
return id;
}
@Override
public String getEndpoint() {
return endpoint;
}
@Override
public Identity getIdentity() {
return identity;
}
@Override
public URI getEndpointUsed() {
return endpointUsed;
}
@Override
public boolean isAuthorized() {
return authorized;
}
@Override
public ContentFormat getContentFormat() {
return contentFormat;
}
@Override
public Map<String, String> getApplicationData() {
return applicationData;
}
@Override
public long getCreationTime() {
return creationTime;
}
@Override
public BootstrapRequest getBootstrapRequest() {
return request;
}
public List<BootstrapDownlinkRequest<? extends LwM2mResponse>> getRequests() {
return requests;
}
public void setRequests(List<BootstrapDownlinkRequest<? extends LwM2mResponse>> requests) {
this.requests = requests;
}
public List<LwM2mResponse> getResponses() {
return responses;
}
public void setResponses(List<LwM2mResponse> responses) {
this.responses = responses;
}
@Override
public LwM2mModel getModel() {
return model;
}
public void setModel(LwM2mModel model) {
this.model = model;
}
public void setMoreTasks(boolean moreTasks) {
this.moreTasks = moreTasks;
}
public boolean hasMoreTasks() {
return moreTasks;
}
@Override
public void cancel() {
cancelled = true;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public String toString() {
return String.format(
"DefaultBootstrapSession [id=%s, endpoint=%s, identity=%s, authorized=%s, contentFormat=%s, applicationData=%s, creationTime=%s, request=%s, cancelled=%s]",
id, endpoint, identity, authorized, contentFormat, applicationData, creationTime, request, cancelled);
}
}