/
BootstrapReadResponse.java
113 lines (93 loc) · 4.09 KB
/
BootstrapReadResponse.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
/*******************************************************************************
* Copyright (c) 2013-2015 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.core.response;
import org.eclipse.leshan.core.ResponseCode;
import org.eclipse.leshan.core.node.LwM2mChildNode;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.request.exception.InvalidResponseException;
public class BootstrapReadResponse extends AbstractLwM2mResponse {
protected final LwM2mChildNode content;
public BootstrapReadResponse(ResponseCode code, LwM2mNode content, String errorMessage) {
this(code, content, errorMessage, null);
}
public BootstrapReadResponse(ResponseCode code, LwM2mNode content, String errorMessage, Object coapResponse) {
super(code, errorMessage, coapResponse);
if (ResponseCode.CONTENT.equals(code)) {
if (content == null)
throw new InvalidResponseException("Content is mandatory for successful response");
if (!(content instanceof LwM2mChildNode))
throw new InvalidResponseException("Invalid Content : node should be a LwM2mChildNode not a %s",
content.getClass().getSimpleName());
}
this.content = (LwM2mChildNode) content;
}
@Override
public boolean isSuccess() {
return getCode() == ResponseCode.CONTENT;
}
@Override
public boolean isValid() {
switch (code.getCode()) {
case ResponseCode.CONTENT_CODE:
case ResponseCode.BAD_REQUEST_CODE:
case ResponseCode.UNAUTHORIZED_CODE:
case ResponseCode.NOT_FOUND_CODE:
case ResponseCode.METHOD_NOT_ALLOWED_CODE:
case ResponseCode.NOT_ACCEPTABLE_CODE:
case ResponseCode.INTERNAL_SERVER_ERROR_CODE:
return true;
default:
return false;
}
}
/**
* Get the {@link LwM2mNode} value returned as response payload.
*
* @return the value or <code>null</code> if the client returned an error response.
*/
public LwM2mChildNode getContent() {
return content;
}
@Override
public String toString() {
if (errorMessage != null)
return String.format("BootstrapReadResponse [code=%s, errormessage=%s]", code, errorMessage);
else
return String.format("BootstrapReadResponse [code=%s, content=%s]", code, content);
}
// Syntactic sugar static constructors :
public static BootstrapReadResponse success(LwM2mChildNode content) {
return new BootstrapReadResponse(ResponseCode.CONTENT, content, null, null);
}
public static BootstrapReadResponse notFound() {
return new BootstrapReadResponse(ResponseCode.NOT_FOUND, null, null);
}
public static BootstrapReadResponse unauthorized() {
return new BootstrapReadResponse(ResponseCode.UNAUTHORIZED, null, null);
}
public static BootstrapReadResponse methodNotAllowed() {
return new BootstrapReadResponse(ResponseCode.METHOD_NOT_ALLOWED, null, null);
}
public static BootstrapReadResponse notAcceptable() {
return new BootstrapReadResponse(ResponseCode.NOT_ACCEPTABLE, null, null);
}
public static BootstrapReadResponse badRequest(String errorMessage) {
return new BootstrapReadResponse(ResponseCode.BAD_REQUEST, null, errorMessage);
}
public static BootstrapReadResponse internalServerError(String errorMessage) {
return new BootstrapReadResponse(ResponseCode.INTERNAL_SERVER_ERROR, null, errorMessage);
}
}