if (ResponseCode.isSuccess(code)) {
coapCode = ResponseCode.valueOf(Integer.parseInt(coapCodeString.trim())); } catch (NumberFormatException e) { LOGGER.warning("Cannot convert the status code in number: " + e.getMessage());
String mt = MediaTypeRegistry.toString(response.getOptions().getContentFormat()); resp.setHeader(org.apache.camel.Exchange.CONTENT_TYPE, mt); resp.setHeader(CoAPConstants.COAP_RESPONSE_CODE, response.getCode().toString()); resp.setBody(response.getPayload());
&& (ResponseCode.isClientError(coapCode) || ResponseCode.isServerError(coapCode))) { LOGGER.info("Set contenttype to TEXT_PLAIN"); coapResponse.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
coapCode = ResponseCode.valueOf(Integer.parseInt(coapCodeString.trim())); } catch (NumberFormatException e) { LOGGER.warning("Cannot convert the status code in number: " + e.getMessage());
if (ResponseCode.isSuccess(code)) {
&& (ResponseCode.isClientError(coapCode) || ResponseCode.isServerError(coapCode))) { LOGGER.info("Set contenttype to TEXT_PLAIN"); coapResponse.getOptions().setContentFormat(MediaTypeRegistry.TEXT_PLAIN);
if (!ResponseCode.isSuccess(response.getCode())) { LOGGER.log(Level.FINE, "Response has error code {0} and must be sent as CON", response.getCode()); response.setType(Type.CON);
if (!ResponseCode.isSuccess(response.getCode())) { LOGGER.fine("Response has error code "+response.getCode()+" and must be sent as CON"); response.setType(Type.CON);
if (relation == null) return; // because request did not try to establish a relation if (CoAP.ResponseCode.isSuccess(response.getCode())) { response.getOptions().setObserve(notificationOrderer.getCurrent());
if (relation == null) return; // because request did not try to establish a relation if (CoAP.ResponseCode.isSuccess(response.getCode())) { response.getOptions().setObserve(notificationOrderer.getCurrent());
/** * Parses a byte array into a CoAP Message. * * @param msg the byte array to parse. * @return the message. * @throws MessageFormatException if the array cannot be parsed into a message. */ public final Message parseMessage(final byte[] msg) { String message = "illegal message code"; DatagramReader reader = new DatagramReader(msg); MessageHeader header = parseHeader(reader); try { if (CoAP.isRequest(header.getCode())) { return parseMessage(reader, header, new Request(CoAP.Code.valueOf(header.getCode()))); } else if (CoAP.isResponse(header.getCode())) { return parseMessage(reader, header, new Response(CoAP.ResponseCode.valueOf(header.getCode()))); } else if (CoAP.isEmptyMessage(header.getCode())) { return parseMessage(reader, header, new EmptyMessage(header.getType())); } } catch (MessageFormatException e) { /** use message to add CoAP message specific information */ message = e.getMessage(); } throw new CoAPMessageFormatException(message, header.getMID(), header.getCode(), CoAP.Type.CON == header.getType()); }
/** * Formats a {@link Response} into a readable String representation. * * @param r the Response * @return the pretty print */ public static String prettyPrint(Response r) { StringBuilder sb = new StringBuilder(); sb.append("==[ CoAP Response ]============================================").append(System.lineSeparator()); sb.append(String.format("MID : %d", r.getMID())).append(System.lineSeparator()); sb.append(String.format("Token : %s", r.getTokenString())).append(System.lineSeparator()); sb.append(String.format("Type : %s", r.getType().toString())).append(System.lineSeparator()); sb.append(String.format("Status : %s", r.getCode().toString())).append(System.lineSeparator()); sb.append(String.format("Options: %s", r.getOptions().toString())).append(System.lineSeparator()); sb.append(String.format("Payload: %d Bytes", r.getPayloadSize())).append(System.lineSeparator()); if (r.getPayloadSize() > 0 && MediaTypeRegistry.isPrintable(r.getOptions().getContentFormat())) { sb.append("---------------------------------------------------------------").append(System.lineSeparator()); sb.append(r.getPayloadString()); sb.append(System.lineSeparator()); } sb.append("==============================================================="); return sb.toString(); }
/** * Formats a {@link Response} into a readable String representation. * * @param r the Response * @return the pretty print */ public static String prettyPrint(Response r) { StringBuilder sb = new StringBuilder(); sb.append("==[ CoAP Response ]============================================").append(System.lineSeparator()); sb.append(String.format("MID : %d", r.getMID())).append(System.lineSeparator()); sb.append(String.format("Token : %s", r.getTokenString())).append(System.lineSeparator()); sb.append(String.format("Type : %s", r.getType().toString())).append(System.lineSeparator()); sb.append(String.format("Status : %s", r.getCode().toString())).append(System.lineSeparator()); sb.append(String.format("Options: %s", r.getOptions().toString())).append(System.lineSeparator()); sb.append(String.format("Payload: %d Bytes", r.getPayloadSize())).append(System.lineSeparator()); if (r.getPayloadSize() > 0 && MediaTypeRegistry.isPrintable(r.getOptions().getContentFormat())) { sb.append("---------------------------------------------------------------").append(System.lineSeparator()); sb.append(r.getPayloadString()); sb.append(System.lineSeparator()); } sb.append("==============================================================="); return sb.toString(); } }
/** * Checks for Content-Type option. * * @param response * the response * @return true, if successful */ protected boolean hasContentType(Response response) { boolean success = response.getOptions().hasContentFormat() || response.getPayloadSize()==0 || !CoAP.ResponseCode.isSuccess(response.getCode()); if (!success) { System.out.println("FAIL: Response without Content-Type"); } else { System.out.printf("PASS: Content-Type (%s)\n", MediaTypeRegistry.toString(response.getOptions() .getContentFormat())); } return success; }
/** * Convert http-code into coap-code. * * @param httpCode http-code * @param defaultCode default response code, if http-code could not be mapped. * @return coap-code */ public static ResponseCode toCoapCode(final int httpCode, final ResponseCode defaultCode) { final int codeClass = httpCode / 100; final int codeDetail = httpCode % 100; if (0 < codeClass && codeClass < 8 && 0 <= codeDetail && codeDetail < 32) { try { return ResponseCode.valueOf(codeClass << 5 | codeDetail); } catch (MessageFormatException e) { } } return defaultCode; } }
public Response parseResponse() { assert(isResponse()); Response response = new Response(ResponseCode.valueOf(code)); parseMessage(response); return response; }
/** * Checks if the response code is a successful code. * * @return true, if is success */ public boolean isSuccess() { return CoAP.ResponseCode.isSuccess(response.getCode()); }
/** * Checks if the response code is a successful code. * * @return true, if is success */ public boolean isSuccess() { return CoAP.ResponseCode.isSuccess(response.getCode()); }
private void buildResponse() { if (coapResponse == null) { lwM2mresponse = OperationResponse.failure(ResponseCode.GATEWAY_TIMEOUT, "Timed Out Waiting For Response."); } else if (ResponseCode.isSuccess(coapResponse.getCode())) { lwM2mresponse = OperationResponse .of(coapResponse, californiumClientIdentifierBuilder.getClientIdentifier()); } else { lwM2mresponse = OperationResponse.failure(coapResponse.getCode(), "Request Failed on Server " + coapResponse.getOptions()); } }