Java Code Examples for org.eclipse.californium.core.coap.Response#setPayload()

The following examples show how to use org.eclipse.californium.core.coap.Response#setPayload() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonCoapAdaptor.java    From Groza with Apache License 2.0 6 votes vote down vote up
private Response convertToRuleEngineErrorResponse(CoapSessionCtx ctx, RuleEngineErrorMsg msg) {
    CoAP.ResponseCode status = CoAP.ResponseCode.INTERNAL_SERVER_ERROR;
    switch (msg.getError()) {
        case QUEUE_PUT_TIMEOUT:
            status = CoAP.ResponseCode.GATEWAY_TIMEOUT;
            break;
        default:
            if (msg.getInSessionMsgType() == SessionMsgType.TO_SERVER_RPC_REQUEST) {
                status = CoAP.ResponseCode.BAD_REQUEST;
            }
            break;
    }
    Response response = new Response(status);
    response.setPayload(JsonConverter.toErrorJson(msg.getErrorMsg()).toString());
    return response;
}
 
Example 2
Source File: JsonCoapAdaptor.java    From Groza with Apache License 2.0 5 votes vote down vote up
private Response convertToServerRpcResponse(SessionContext ctx, ToServerRpcResponseMsg msg) {
    if (msg.isSuccess()) {
        Response response = new Response(CoAP.ResponseCode.CONTENT);
        JsonElement result = JsonConverter.toJson(msg);
        response.setPayload(result.toString());
        return response;
    } else {
        return convertError(Optional.of(new RuntimeException("Server RPC response is empty!")));
    }
}
 
Example 3
Source File: JsonCoapAdaptor.java    From Groza with Apache License 2.0 5 votes vote down vote up
private Response convertGetAttributesResponse(GetAttributesResponse msg) {
    if (msg.isSuccess()) {
        Optional<AttributesKVMsg> payload = msg.getData();
        if (!payload.isPresent() || (payload.get().getClientAttributes().isEmpty() && payload.get().getSharedAttributes().isEmpty())) {
            return new Response(CoAP.ResponseCode.NOT_FOUND);
        } else {
            Response response = new Response(CoAP.ResponseCode.CONTENT);
            JsonObject result = JsonConverter.toJson(payload.get(), false);
            response.setPayload(result.toString());
            return response;
        }
    } else {
        return convertError(msg.getError());
    }
}
 
Example 4
Source File: JsonCoapAdaptor.java    From Groza with Apache License 2.0 4 votes vote down vote up
private Response getObserveNotification(CoapSessionCtx ctx, JsonObject json) {
    Response response = new Response(CoAP.ResponseCode.CONTENT);
    response.getOptions().setObserve(ctx.nextSeqNumber());
    response.setPayload(json.toString());
    return response;
}
 
Example 5
Source File: CoapResponseCodec.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Response encode(OneM2mResponse resMessage, CoapExchange exchange) throws Exception {
		Response resCoap = null;
		
		RESPONSE_STATUS resCode = resMessage.getResponseStatusCodeEnum();
		
		resCoap = Response.createResponse(exchange.advanced().getRequest(), getCoapResponseCode(resCode));
		
		if (resMessage.getContent() != null && resMessage.getContent().length > 0) {
			// Added to support application/json in 2017-05-23
			if(resMessage.getContentType().Name().toLowerCase().contains("cbor")) {
				String strContent = new String(resMessage.getContent(), "UTF-8");
				byte[] cbor = net.herit.iot.onem2m.core.util.Utils.encodeCBOR(strContent);
				resCoap.setPayload(cbor);
			} else {
				resCoap.setPayload(resMessage.getContent());
			}
			//resCoap.setPayload(resMessage.getContent());			// blocked in 2017-05-23
		}
		
		resCoap.getOptions().addOption(new Option(ONEM2M_RSC, resCode.Value()));
		if (resMessage.getRequestIdentifier() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_RQI, resMessage.getRequestIdentifier()));
		} else {
			throw new Exception("Not Setting RequestIdentifier");
		}
		if (resMessage.getFrom() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_FR, resMessage.getFrom()));
		}
		if (resMessage.getOriginatingTimestamp() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_OT, resMessage.getOriginatingTimestamp()));
		}
		if (resMessage.getResultExpirationTimestamp() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_RSET, resMessage.getResultExpirationTimestamp()));
		}
		if (resMessage.getEventCategory() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_EC, resMessage.getEventCategory()));
		}
		
		if(resMessage.getContentLocation() != null) {  // added 2016.05.13
			resCoap.getOptions().setLocationPath(resMessage.getContentLocation());
		}
		
		if (resMessage.getContentType() != null && resMessage.getContentType() != CONTENT_TYPE.NONE  &&
				resMessage.getContent() != null && resMessage.getContent().length > 0) {
//			System.out.println("RES>> Content-Type: " + resMessage.getContentType());
			COAP_CONTENT_TYPE contentType = COAP_CONTENT_TYPE.getCoapContentType(resMessage.getContentType());
			resCoap.getOptions().setContentFormat(contentType.Value());
		}
		
		return resCoap;
	}
 
Example 6
Source File: CoapResponseCodec.java    From SI with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static Response encode(OneM2mResponse resMessage, CoapExchange exchange) throws Exception {
		Response resCoap = null;
		
		RESPONSE_STATUS resCode = resMessage.getResponseStatusCodeEnum();
		
		resCoap = Response.createResponse(exchange.advanced().getRequest(), getCoapResponseCode(resCode));
		
		if (resMessage.getContent() != null && resMessage.getContent().length > 0) {
			// Added to support application/json in 2017-05-23
			if(resMessage.getContentType().Name().toLowerCase().contains("cbor")) {
				String strContent = new String(resMessage.getContent(), "UTF-8");
				byte[] cbor = net.herit.iot.onem2m.core.util.Utils.encodeCBOR(strContent);
				resCoap.setPayload(cbor);
			} else {
				resCoap.setPayload(resMessage.getContent());
			}
			//resCoap.setPayload(resMessage.getContent());			// blocked in 2017-05-23
		}
		
		resCoap.getOptions().addOption(new Option(ONEM2M_RSC, resCode.Value()));
		if (resMessage.getRequestIdentifier() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_RQI, resMessage.getRequestIdentifier()));
		} else {
			throw new Exception("Not Setting RequestIdentifier");
		}
		if (resMessage.getFrom() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_FR, resMessage.getFrom()));
		}
		if (resMessage.getOriginatingTimestamp() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_OT, resMessage.getOriginatingTimestamp()));
		}
		if (resMessage.getResultExpirationTimestamp() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_RSET, resMessage.getResultExpirationTimestamp()));
		}
		if (resMessage.getEventCategory() != null) {
			resCoap.getOptions().addOption(new Option(ONEM2M_EC, resMessage.getEventCategory()));
		}
		
		if(resMessage.getContentLocation() != null) {  // added 2016.05.13
			resCoap.getOptions().setLocationPath(resMessage.getContentLocation());
		}
		
		if (resMessage.getContentType() != null && resMessage.getContentType() != CONTENT_TYPE.NONE  &&
				resMessage.getContent() != null && resMessage.getContent().length > 0) {
//			System.out.println("RES>> Content-Type: " + resMessage.getContentType());
			COAP_CONTENT_TYPE contentType = COAP_CONTENT_TYPE.getCoapContentType(resMessage.getContentType());
			resCoap.getOptions().setContentFormat(contentType.Value());
		}
		
		return resCoap;
	}
 
Example 7
Source File: CoapErrorResponse.java    From hono with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Create response with provide response code.
 *
 * @param message error message sent as payload.
 * @param code response code.
 * @return The CoAP response.
 */
public static Response respond(final String message, final ResponseCode code) {
    final Response response = new Response(code);
    response.setPayload(message);
    response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
    return response;
}
 
Example 8
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 */
public void respond(ResponseCode code, String payload) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
	respond(response);
}
 
Example 9
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 * @param contentFormat the Content-Format of the payload
 */
public void respond(ResponseCode code, byte[] payload, int contentFormat) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(contentFormat);
	respond(response);
}
 
Example 10
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 * @param contentFormat the Content-Format of the payload
 */
public void respond(ResponseCode code, String payload, int contentFormat) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(contentFormat);
	respond(response);
}
 
Example 11
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 */
public void respond(ResponseCode code, String payload) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
	respond(response);
}
 
Example 12
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 * @param contentFormat the Content-Format of the payload
 */
public void respond(ResponseCode code, byte[] payload, int contentFormat) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(contentFormat);
	respond(response);
}
 
Example 13
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 * @param contentFormat the Content-Format of the payload
 */
public void respond(ResponseCode code, String payload, int contentFormat) {
	Response response = new Response(code);
	response.setPayload(payload);
	response.getOptions().setContentFormat(contentFormat);
	respond(response);
}
 
Example 14
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 */
public void respond(ResponseCode code, byte[] payload) {
	Response response = new Response(code);
	response.setPayload(payload);
	respond(response);
}
 
Example 15
Source File: CoapExchange.java    From SI with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * Respond with the specified response code and the specified payload.
 * <ul>
 *   <li>GET: Content (2.05), Valid (2.03)</li>
 *   <li>POST: Created (2.01), Changed (2.04), Deleted (2.02) </li>
 *   <li>PUT: Created (2.01), Changed (2.04)</li>
 *   <li>DELETE: Deleted (2.02)</li>
 * </ul>
 *
 * @param code the response code
 * @param payload the payload
 */
public void respond(ResponseCode code, byte[] payload) {
	Response response = new Response(code);
	response.setPayload(payload);
	respond(response);
}