/** * Removes all Uri-Path options. * Returns the current OptionSet object for a fluent API. * @return this OptionSet */ public OptionSet clearUriPath() { getUriPath().clear(); return this; }
/** * Returns the number of Uri-Path options (i.e., path segments). * @return the count */ public int getURIPathCount() { return getUriPath().size(); }
/** * Removes all Uri-Path options. * Returns the current OptionSet object for a fluent API. * @return this OptionSet */ public OptionSet clearUriPath() { getUriPath().clear(); return this; }
/** * Returns the number of Uri-Path options (i.e., path segments). * @return the count */ public int getURIPathCount() { return getUriPath().size(); }
/** * Returns the Uri-Path options as relative URI string. * To ease splitting, it omits the leading slash. * @return the Uri-Path as string */ public String getUriPathString() { StringBuilder buffer = new StringBuilder(); for (String element:getUriPath()) buffer.append(element).append("/"); if (buffer.length()==0) return ""; else return buffer.substring(0, buffer.length()-1); }
/** * Returns the Uri-Path options as relative URI string. * To ease splitting, it omits the leading slash. * @return the Uri-Path as string */ public String getUriPathString() { StringBuilder buffer = new StringBuilder(); for (String element:getUriPath()) buffer.append(element).append("/"); if (buffer.length()==0) return ""; else return buffer.substring(0, buffer.length()-1); }
/** * Adds a path segment to the Uri-Path options. * Returns the current OptionSet object for a fluent API. * @param segment the path segment to add * @return this OptionSet */ public OptionSet addUriPath(String segment) { if (segment == null) throw new IllegalArgumentException("URI path option must not be null"); if (segment.getBytes(CoAP.UTF8_CHARSET).length > 255) throw new IllegalArgumentException("Uri-Path option must be smaller or euqal to 255 bytes (UTF-8 encoded): " + segment); getUriPath().add(segment); return this; }
/** * Adds a path segment to the Uri-Path options. * Returns the current OptionSet object for a fluent API. * @param segment the path segment to add * @return this OptionSet */ public OptionSet addUriPath(String segment) { if (segment == null) throw new IllegalArgumentException("URI path option must not be null"); if (segment.getBytes(CoAP.UTF8_CHARSET).length > 255) throw new IllegalArgumentException("Uri-Path option must be smaller or euqal to 255 bytes (UTF-8 encoded): " + segment); getUriPath().add(segment); return this; }
private List<String> getUriPaths() { return exchange.getRequestOptions().getUriPath(); }
private List<String> getUriPaths() { return exchange.getRequestOptions().getUriPath(); }
@Override public void handleDELETE(CoapExchange exchange) { LOG.debug("DELETE received : {}", exchange.advanced().getRequest()); List<String> uri = exchange.getRequestOptions().getUriPath(); if (uri != null && uri.size() == 2 && RESOURCE_NAME.equals(uri.get(0))) { handleDeregister(exchange, uri.get(1)); } else { LOG.debug("Invalid deregistration"); exchange.respond(ResponseCode.NOT_FOUND); } }
@Override public void handlePUT(CoapExchange exchange) { Request request = exchange.advanced().getRequest(); LOG.debug("UPDATE received : {}", request); if (!Type.CON.equals(request.getType())) { exchange.respond(ResponseCode.BAD_REQUEST); return; } List<String> uri = exchange.getRequestOptions().getUriPath(); if (uri == null || uri.size() != 2 || !RESOURCE_NAME.equals(uri.get(0))) { exchange.respond(ResponseCode.BAD_REQUEST); return; } LOG.debug( "Warning a client made a registration update using a CoAP PUT, a POST must be used since version V1_0-20150615-D of the specification. Request: {}", request); handleUpdate(exchange, request, uri.get(1)); }
@Override public void handlePOST(CoapExchange exchange) { Request request = exchange.advanced().getRequest(); LOG.debug("POST received : {}", request); // The LWM2M spec (section 8.2) mandates the usage of confirmable messages if (!Type.CON.equals(request.getType())) { exchange.respond(ResponseCode.BAD_REQUEST); return; } List<String> uri = exchange.getRequestOptions().getUriPath(); if (uri == null || uri.size() == 0 || !RESOURCE_NAME.equals(uri.get(0))) { exchange.respond(ResponseCode.BAD_REQUEST); return; } if (uri.size() == 1) { handleRegister(exchange, request); return; } else if (uri.size() == 2) { handleUpdate(exchange, request, uri.get(1)); return; } else { exchange.respond(ResponseCode.BAD_REQUEST); return; } }
@Override public void deliverRequest(Exchange exchange) { OptionSet options = exchange.getRequest().getOptions(); List<String> paths = options.getUriPath(); handleTenantRequest(getEventReceiver().getTenant(), paths, exchange); }
@Override public void deliverRequest(final Exchange exchange) { final Request request = exchange.getRequest(); final List<String> path = request.getOptions().getUriPath(); final Code code = request.getCode(); final Resource resource = findResource(path, code); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request final Executor executor = resource.getExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
@Override public void deliverRequest(Exchange exchange) { OptionSet options = exchange.getRequest().getOptions(); List<String> paths = options.getUriPath(); handleRequest(getEventReceiver().getTenantEngine().getTenant(), paths, exchange); }
@Override public void deliverRequest(final Exchange exchange) { final Request request = exchange.getRequest(); final List<String> path = request.getOptions().getUriPath(); final Code code = request.getCode(); final Resource resource = findResource(path, code); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request final Executor executor = resource.getExecutor(); if (executor != null) { executor.execute(new Runnable() { @Override public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
@Override public void deliverRequest(final Exchange exchange) { Request request = exchange.getRequest(); List<String> path = request.getOptions().getUriPath(); final Resource resource = findResource(path); if (resource != null) { checkForObserveOption(exchange, resource); // Get the executor and let it process the request Executor executor = resource.getExecutor(); if (executor != null) { exchange.setCustomExecutor(); executor.execute(new Runnable() { public void run() { resource.handleRequest(exchange); } }); } else { resource.handleRequest(exchange); } } else { LOGGER.info("Did not find resource " + path.toString() + " requested by " + request.getSource()+":"+request.getSourcePort()); exchange.sendResponse(new Response(ResponseCode.NOT_FOUND)); } }
@Override public void handleDELETE(CoapExchange exchange) { LOG.debug("DELETE received : {}", exchange.advanced().getRequest()); List<String> uri = exchange.getRequestOptions().getUriPath(); if (uri != null && uri.size() == 2 && RESOURCE_NAME.equals(uri.get(0))) { DeregisterRequest deregisterRequest = new DeregisterRequest(uri.get(1)); LwM2mResponse deregisterResponse = registrationHandler.deregister(deregisterRequest); exchange.respond(fromLwM2mCode(deregisterResponse.getCode())); if (exchange.advanced().getEndpoint() instanceof SecureEndpoint && deregisterResponse.getCode().equals(org.eclipse.leshan.ResponseCode.DELETED)) { // clean the DTLS Session Request request = exchange.advanced().getRequest(); ((SecureEndpoint) exchange.advanced().getEndpoint()).getDTLSConnector().close( new InetSocketAddress(request.getSource(), request.getSourcePort())); } } else { LOG.debug("Invalid deregistration"); exchange.respond(ResponseCode.NOT_FOUND); } }
/** * Verifies that non-ASCII characters in the URI components are * not escaped when being put to Uri options. */ @Test public void testSetUriStringDoesNotEscapeNonUsAsciiChars() { String nonUsAsciiPath = "äöüß"; // german "Umlaute String nonUsAsciiQuery = "ä=öß"; Request req = Request.newGet().setURI(String.format("coap://127.0.0.1/%s?%s", nonUsAsciiPath, nonUsAsciiQuery)); List<String> path = req.getOptions().getUriPath(); assertThat(path, is(notNullValue())); assertThat(path.get(0), is(nonUsAsciiPath)); List<String> query = req.getOptions().getUriQuery(); assertThat(query, is(notNullValue())); assertThat(query.get(0), is(nonUsAsciiQuery)); }