Skip to content

Commit

Permalink
Fix wrong byte[] type and NPE in JsonDeviceManagementRequests
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Dec 10, 2018
1 parent 0a34044 commit 726cfcf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
Expand Up @@ -25,6 +25,8 @@
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Map;

import io.swagger.annotations.ApiModelProperty;

/**
* Kapua {@link Message} {@link Payload} definition.
*
Expand Down Expand Up @@ -60,6 +62,7 @@ public interface KapuaPayload extends Payload {
*/
@XmlElement(name = "body")
@XmlJavaTypeAdapter(BinaryXmlAdapter.class)
@ApiModelProperty(dataType = "string")
public byte[] getBody();

/**
Expand Down
Expand Up @@ -15,6 +15,7 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;

import org.eclipse.kapua.app.api.resources.v1.resources.marker.JsonSerializationFixed;
import org.eclipse.kapua.app.api.resources.v1.resources.model.EntityId;
import org.eclipse.kapua.app.api.resources.v1.resources.model.ScopeId;
Expand All @@ -41,7 +42,7 @@
/**
* @see JsonSerializationFixed
*/
@Api(value = "Devices", authorizations = {@Authorization(value = "kapuaAccessToken")})
@Api(value = "Devices", authorizations = { @Authorization(value = "kapuaAccessToken") })
@Path("{scopeId}/devices/{deviceId}/requests")
public class DeviceManagementRequestsJson extends AbstractKapuaResource implements JsonSerializationFixed {

Expand All @@ -60,8 +61,8 @@ public class DeviceManagementRequestsJson extends AbstractKapuaResource implemen
* @throws Exception Whenever something bad happens. See specific {@link KapuaService} exceptions.
*/
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_JSON })
@ApiOperation(nickname = "deviceRequestSend", value = "Sends a request", notes = "Sends a request message to a device", response = DeviceCommandOutput.class)
public JsonGenericResponseMessage sendRequest(
@ApiParam(value = "The ScopeId of the device", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
Expand All @@ -82,15 +83,18 @@ public JsonGenericResponseMessage sendRequest(
genericRequestMessage.setChannel(jsonGenericRequestMessage.getChannel());

GenericRequestPayload kapuaDataPayload = new GenericRequestPayloadImpl();
kapuaDataPayload.setBody(jsonGenericRequestMessage.getPayload().getBody());

jsonGenericRequestMessage.getPayload().getMetrics().forEach(
jsonMetric -> {
String name = jsonMetric.getName();
Object value = ObjectValueConverter.fromString(jsonMetric.getValue(), jsonMetric.getValueType());
if (jsonGenericRequestMessage.getPayload() != null) {
kapuaDataPayload.setBody(jsonGenericRequestMessage.getPayload().getBody());

jsonGenericRequestMessage.getPayload().getMetrics().forEach(
jsonMetric -> {
String name = jsonMetric.getName();
Object value = ObjectValueConverter.fromString(jsonMetric.getValue(), jsonMetric.getValueType());

kapuaDataPayload.getMetrics().put(name, value);
});
kapuaDataPayload.getMetrics().put(name, value);
});
}

genericRequestMessage.setPayload(kapuaDataPayload);

Expand Down
Expand Up @@ -15,6 +15,7 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;

import org.eclipse.kapua.app.api.resources.v1.resources.marker.JsonSerializationFixed;
import org.eclipse.kapua.app.api.resources.v1.resources.model.ScopeId;
import org.eclipse.kapua.app.api.resources.v1.resources.model.data.JsonKapuaDataMessage;
Expand All @@ -35,7 +36,7 @@
/**
* @see JsonSerializationFixed
*/
@Api(value = "Streams", authorizations = {@Authorization(value = "kapuaAccessToken")})
@Api(value = "Streams", authorizations = { @Authorization(value = "kapuaAccessToken") })
@Path("{scopeId}/streams")
public class StreamsJson extends AbstractKapuaResource implements JsonSerializationFixed {

Expand Down Expand Up @@ -90,7 +91,7 @@ public class StreamsJson extends AbstractKapuaResource implements JsonSerializat
*/
@POST
@Path("messages")
@Consumes({MediaType.APPLICATION_JSON})
@Consumes({ MediaType.APPLICATION_JSON })
@ApiOperation(nickname = "streamPublish", value = "Publishes a fire-and-forget message", notes = "Publishes a fire-and-forget message to a topic composed of [account-name] / [client-id] / [semtantic-parts]")
public Response publish(
@ApiParam(value = "The ScopeId of the device", required = true, defaultValue = DEFAULT_SCOPE_ID) @PathParam("scopeId") ScopeId scopeId,
Expand All @@ -109,15 +110,18 @@ public Response publish(
kapuaDataMessage.setChannel(jsonKapuaDataMessage.getChannel());

KapuaDataPayload kapuaDataPayload = new KapuaDataPayloadImpl();
kapuaDataPayload.setBody(jsonKapuaDataMessage.getPayload().getBody());

jsonKapuaDataMessage.getPayload().getMetrics().forEach(
jsonMetric -> {
String name = jsonMetric.getName();
Object value = ObjectValueConverter.fromString(jsonMetric.getValue(), jsonMetric.getValueType());
if (jsonKapuaDataMessage.getPayload() != null) {
kapuaDataPayload.setBody(jsonKapuaDataMessage.getPayload().getBody());

jsonKapuaDataMessage.getPayload().getMetrics().forEach(
jsonMetric -> {
String name = jsonMetric.getName();
Object value = ObjectValueConverter.fromString(jsonMetric.getValue(), jsonMetric.getValueType());

kapuaDataPayload.getMetrics().put(name, value);
});
kapuaDataPayload.getMetrics().put(name, value);
});
}

kapuaDataMessage.setPayload(kapuaDataPayload);

Expand Down
Expand Up @@ -25,6 +25,8 @@
import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.ApiModelProperty;

@XmlRootElement(name = "payload")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class JsonKapuaPayload {
Expand Down Expand Up @@ -69,6 +71,7 @@ public void setMetrics(List<XmlAdaptedMetric> metrics) {

@XmlElement(name = "body")
@XmlJavaTypeAdapter(BinaryXmlAdapter.class)
@ApiModelProperty(dataType = "string")
public byte[] getBody() {
return body;
}
Expand Down
Expand Up @@ -64,6 +64,7 @@ default String getType() {

@XmlElement(name = "signature")
@XmlJavaTypeAdapter(BinaryXmlAdapter.class)
@ApiModelProperty(dataType = "string")
byte[] getSignature();

void setSignature(byte[] signature);
Expand Down

0 comments on commit 726cfcf

Please sign in to comment.