/** * Gets the request payload as byte array. * * @return the request payload */ public byte[] getRequestPayload() { return exchange.getRequest().getPayload(); }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
/** * Gets the request payload as byte array. * * @return the request payload */ public byte[] getRequestPayload() { return exchange.getRequest().getPayload(); }
private static Request getNextRequestBlock(final Request request, final BlockwiseStatus status) { int num = status.getCurrentNum(); int szx = status.getCurrentSzx(); Request block = new Request(request.getCode()); // do not enforce CON, since NON could make sense over SMS or similar transports block.setType(request.getType()); block.setDestination(request.getDestination()); block.setDestinationPort(request.getDestinationPort()); // copy options block.setOptions(new OptionSet(request.getOptions())); // copy message observers so that a failing blockwise request also notifies observers registered with // the original request block.addMessageObservers(request.getMessageObservers()); int currentSize = 1 << (4 + szx); int from = num * currentSize; int to = Math.min((num + 1) * currentSize, request.getPayloadSize()); int length = to - from; byte[] blockPayload = new byte[length]; System.arraycopy(request.getPayload(), from, blockPayload, 0, length); block.setPayload(blockPayload); boolean m = (to < request.getPayloadSize()); block.getOptions().setBlock1(szx, m, num); status.setComplete(!m); return block; }
/** * Handle device registration request. * * @param tenant * @param deviceToken * @param exchange */ protected void handleDeviceRegistration(ITenant tenant, String deviceToken, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(CoapJsonDecoder.META_EVENT_TYPE, Type.RegisterDevice.name()); metadata.put(CoapJsonDecoder.META_DEVICE_TOKEN, deviceToken); getEventReceiver().onEventPayloadReceived(exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device registration submitted successfully.", exchange); }
/** * Handle add device measurement. * * @param tenant * @param deviceToken * @param paths * @param exchange */ protected void handleAddDeviceMeasurement(ITenant tenant, String deviceToken, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(CoapJsonDecoder.META_EVENT_TYPE, Type.DeviceMeasurement.name()); metadata.put(CoapJsonDecoder.META_DEVICE_TOKEN, deviceToken); getEventReceiver().onEventPayloadReceived(exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device measurement submitted successfully.", exchange); }
/** * Handle add device alert. * * @param tenant * @param deviceToken * @param paths * @param exchange */ protected void handleAddDeviceAlert(ITenant tenant, String deviceToken, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(CoapJsonDecoder.META_EVENT_TYPE, Type.DeviceAlert.name()); metadata.put(CoapJsonDecoder.META_DEVICE_TOKEN, deviceToken); getEventReceiver().onEventPayloadReceived(exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device alert submitted successfully.", exchange); }
/** * Handle add device location. * * @param tenant * @param deviceToken * @param paths * @param exchange */ protected void handleAddDeviceLocation(ITenant tenant, String deviceToken, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(CoapJsonDecoder.META_EVENT_TYPE, Type.DeviceLocation.name()); metadata.put(CoapJsonDecoder.META_DEVICE_TOKEN, deviceToken); getEventReceiver().onEventPayloadReceived(exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device location submitted successfully.", exchange); }
/** * Handle add device acknowledgement. * * @param tenant * @param deviceToken * @param paths * @param exchange */ protected void handleAddDeviceAck(ITenant tenant, String deviceToken, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(CoapJsonDecoder.META_EVENT_TYPE, Type.Acknowledge.name()); metadata.put(CoapJsonDecoder.META_DEVICE_TOKEN, deviceToken); getEventReceiver().onEventPayloadReceived(exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device acknowledgement submitted successfully.", exchange); }
LOGGER.severe("ISO-8859-1 encoding not supported: " + e.getMessage()); byte[] payload = request.getPayload();
LOGGER.severe("ISO-8859-1 encoding not supported: " + e.getMessage()); byte[] payload = request.getPayload();
if (request.getPayload() != null && request.getPayload().length > 0) { objectLinks = Link.parse(request.getPayload());
if (request.getPayload() != null && request.getPayload().length > 0) { objectLinks = LinkObject.parse(request.getPayload());
/** * Handle operations related to device locations. * * @param tenant * @param device * @param paths * @param exchange */ protected void handleDeviceLocations(ITenant tenant, IDevice device, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(META_EVENT_TYPE, Type.DeviceLocation.name()); metadata.put(META_HARDWARE_ID, device.getHardwareId()); switch (exchange.getRequest().getCode()) { case POST: { try { EventProcessingLogic.processRawPayloadWithExceptionHandling(getEventReceiver(), exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device location created successfully.", exchange); } catch (EventDecodeException e) { LOGGER.error("Unable to decode CoAP location payload.", e); createAndSendResponse(ResponseCode.BAD_REQUEST, "Unable to parse payload.", exchange); } break; } default: { createAndSendResponse(ResponseCode.BAD_REQUEST, "Device location operation not available.", exchange); } } }
public CoapMessage(Request request, boolean incoming) { this(incoming, request.getType(), request.getMID(), request.getTokenString(), request.getOptions(), request .getPayload()); this.code = request.getCode().toString(); }
/** * Handle operations related to device measurements. * * @param tenant * @param device * @param paths * @param exchange */ protected void handleDeviceMeasurements(ITenant tenant, IDevice device, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(META_EVENT_TYPE, Type.DeviceMeasurements.name()); metadata.put(META_HARDWARE_ID, device.getHardwareId()); switch (exchange.getRequest().getCode()) { case POST: { try { EventProcessingLogic.processRawPayloadWithExceptionHandling(getEventReceiver(), exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device measurements created successfully.", exchange); } catch (EventDecodeException e) { LOGGER.error("Unable to decode CoAP measurements payload.", e); createAndSendResponse(ResponseCode.BAD_REQUEST, "Unable to parse payload.", exchange); } break; } default: { createAndSendResponse(ResponseCode.BAD_REQUEST, "Device measurements operation not available.", exchange); } } }
/** * Handle operations related to device alerts. * * @param tenant * @param device * @param paths * @param exchange */ protected void handleDeviceAlerts(ITenant tenant, IDevice device, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(META_EVENT_TYPE, Type.DeviceAlert.name()); metadata.put(META_HARDWARE_ID, device.getHardwareId()); switch (exchange.getRequest().getCode()) { case POST: { try { EventProcessingLogic.processRawPayloadWithExceptionHandling(getEventReceiver(), exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device alert created successfully.", exchange); } catch (EventDecodeException e) { LOGGER.error("Unable to decode CoAP alert payload.", e); createAndSendResponse(ResponseCode.BAD_REQUEST, "Unable to parse payload.", exchange); } break; } default: { createAndSendResponse(ResponseCode.BAD_REQUEST, "Device alert operation not available.", exchange); } } }
/** * Handle operations related to device command acknowledgements. * * @param tenant * @param device * @param paths * @param exchange */ protected void handleDeviceAcks(ITenant tenant, IDevice device, List<String> paths, Exchange exchange) { Map<String, Object> metadata = new HashMap<String, Object>(); metadata.put(META_EVENT_TYPE, Type.Acknowledge.name()); metadata.put(META_HARDWARE_ID, device.getHardwareId()); switch (exchange.getRequest().getCode()) { case POST: { try { EventProcessingLogic.processRawPayloadWithExceptionHandling(getEventReceiver(), exchange.getRequest().getPayload(), metadata); createAndSendResponse(ResponseCode.CONTENT, "Device acknowledgement created successfully.", exchange); } catch (EventDecodeException e) { LOGGER.error("Unable to decode CoAP acknowledgement payload.", e); createAndSendResponse(ResponseCode.BAD_REQUEST, "Unable to parse payload.", exchange); } break; } default: { createAndSendResponse(ResponseCode.BAD_REQUEST, "Device location operation not available.", exchange); } } }
/** * Create a key for the cache starting from a request and the * content-type of the corresponding response. * * @param request * @return * @throws URISyntaxException */ private static CacheKey fromContentTypeOption(Request request) throws URISyntaxException { if (request == null) { throw new IllegalArgumentException("request == null"); } Response response = request.getResponse(); if (response == null) { return fromAcceptOptions(request).get(0); } String proxyUri = request.getOptions().getProxyUri(); int mediaType = response.getOptions().getContentFormat(); if (mediaType < 0) { // content-format option not set, use default mediaType = MediaTypeRegistry.TEXT_PLAIN; } byte[] payload = request.getPayload(); // create the new cacheKey CacheKey cacheKey = new CacheKey(proxyUri, mediaType, payload); cacheKey.setResponse(response); return cacheKey; }
/** * Create a key for the cache starting from a request and the * content-type of the corresponding response. * * @param request * @return * @throws URISyntaxException */ private static CacheKey fromContentTypeOption(Request request) throws URISyntaxException { if (request == null) { throw new IllegalArgumentException("request == null"); } Response response = request.getResponse(); if (response == null) { return fromAcceptOptions(request).get(0); } String proxyUri = request.getOptions().getProxyUri(); int mediaType = response.getOptions().getContentFormat(); if (mediaType < 0) { // content-format option not set, use default mediaType = MediaTypeRegistry.TEXT_PLAIN; } byte[] payload = request.getPayload(); // create the new cacheKey CacheKey cacheKey = new CacheKey(proxyUri, mediaType, payload); cacheKey.setResponse(response); return cacheKey; }