@Override public void handleGET(CoapExchange exchange) { StringBuilder builder = new StringBuilder(); builder.append("Available commands:\n - GET: show cached values\n - DELETE: empty the cache\n - POST: enable/disable caching\n"); // get cache values builder.append("\nCached values:\n"); for (CacheKey cachedRequest : responseCache.asMap().keySet()) { Response response = responseCache.asMap().get(cachedRequest); builder.append(cachedRequest.getProxyUri()).append(" (").append( MediaTypeRegistry.toString(cachedRequest.getMediaType())).append(") > ").append(getRemainingLifetime(response)).append(" seconds | (").append(cachedRequest.getMediaType()).append(")\n"); } exchange.respond(ResponseCode.CONTENT, builder.toString()); }
/** * Formats a {@link Request} into a readable String representation. * * @param r the Request * @return the pretty print */ public static String prettyPrint(Request r) { StringBuilder sb = new StringBuilder(); sb.append("==[ CoAP Request ]=============================================").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("Method : %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(); }
coapContentTypes = MediaTypeRegistry.parseWildcard(headerFragment); } else { coapContentTypes[0] = MediaTypeRegistry.parse(headerFragment);
int mediaType = MediaTypeRegistry.parse(ct); CoapResponse response = null; boolean pingResponse = false; 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());
coapContentTypeString = MediaTypeRegistry.toString(coapContentType); if (MediaTypeRegistry.isPrintable(coapContentType)) { coapContentTypeString += "; charset=UTF-8";
coapContentType = MediaTypeRegistry.parse(httpContentTypeString);
for (Integer acceptType : MediaTypeRegistry.getAllMediaTypes()) { cacheKeys.add(new CacheKey(proxyUri, acceptType, payload));
String mt = MediaTypeRegistry.toString(options.getContentFormat()); camelExchange.getIn().setHeader(org.apache.camel.Exchange.CONTENT_TYPE, mt); Message target = camelExchange.hasOut() ? camelExchange.getOut() : camelExchange.getIn(); int format = MediaTypeRegistry.parse(target.getHeader(org.apache.camel.Exchange.CONTENT_TYPE, String.class)); cexchange.respond(ResponseCode.CONTENT, target.getBody(byte[].class), format);
coapContentTypeString = MediaTypeRegistry.toString(coapContentType); if (MediaTypeRegistry.isPrintable(coapContentType)) { coapContentTypeString += "; charset=UTF-8";
coapContentType = MediaTypeRegistry.parse(httpContentTypeString);
for (Integer acceptType : MediaTypeRegistry.getAllMediaTypes()) { cacheKeys.add(new CacheKey(proxyUri, acceptType, payload));
@Override public void handleGET(CoapExchange exchange) { StringBuilder builder = new StringBuilder(); builder.append("Available commands:\n - GET: show cached values\n - DELETE: empty the cache\n - POST: enable/disable caching\n"); // get cache values builder.append("\nCached values:\n"); for (CacheKey cachedRequest : responseCache.asMap().keySet()) { Response response = responseCache.asMap().get(cachedRequest); builder.append(cachedRequest.getProxyUri()).append(" (").append( MediaTypeRegistry.toString(cachedRequest.getMediaType())).append(") > ").append(getRemainingLifetime(response)).append(" seconds | (").append(cachedRequest.getMediaType()).append(")\n"); } exchange.respond(ResponseCode.CONTENT, builder.toString()); }
coapContentTypes = MediaTypeRegistry.parseWildcard(headerFragment); } else { coapContentTypes[0] = MediaTypeRegistry.parse(headerFragment);
/** * Formats a {@link Request} into a readable String representation. * * @param r the Request * @return the pretty print */ public static String prettyPrint(Request r) { StringBuilder sb = new StringBuilder(); sb.append("==[ CoAP Request ]=============================================").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("Method : %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(); }
/** * Forwards the body of a CoAP request to the south bound Event API of the AMQP 1.0 Messaging Network. * * @param exchange coap message exchange * @param authenticatedDevice authenticated device * @param originDevice message's origin device * @throws NullPointerException if any of the parameters is {@code null}. */ public final void uploadEventMessage(final CoapExchange exchange, final Device authenticatedDevice, final Device originDevice) { doUploadMessage( Objects.requireNonNull(exchange), Objects.requireNonNull(authenticatedDevice), Objects.requireNonNull(originDevice), true, Buffer.buffer(exchange.getRequestPayload()), MediaTypeRegistry.toString(exchange.getRequestOptions().getContentFormat()), getEventSender(authenticatedDevice.getTenantId()), EventConstants.EVENT_ENDPOINT); }
/** * 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(); } }
/** * Forwards the body of a CoAP request to the south bound Telemetry API of the AMQP 1.0 Messaging Network. * * @param exchange coap message exchange * @param authenticatedDevice authenticated device * @param originDevice message's origin device * @param waitForOutcome {@code true} to send the message waiting for the outcome, {@code false}, to wait just for * the sent. * @throws NullPointerException if any of the parameters is {@code null}. */ public final void uploadTelemetryMessage(final CoapExchange exchange, final Device authenticatedDevice, final Device originDevice, final boolean waitForOutcome) { doUploadMessage( Objects.requireNonNull(exchange), Objects.requireNonNull(authenticatedDevice), Objects.requireNonNull(originDevice), waitForOutcome, Buffer.buffer(exchange.getRequestPayload()), MediaTypeRegistry.toString(exchange.getRequestOptions().getContentFormat()), getTelemetrySender(authenticatedDevice.getTenantId()), TelemetryConstants.TELEMETRY_ENDPOINT); }
/** * 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; }
@Override public void handleGET(CoapExchange exchange) { if (exchange.getRequestOptions().hasAccept() && exchange.getRequestOptions().getAccept() != dataCt) { exchange.respond(NOT_ACCEPTABLE, MediaTypeRegistry.toString(dataCt) + " only"); } else { exchange.respond(CONTENT, data, dataCt); } }