/** * Updates the endpoint parameters from POST and PUT requests. * * @param request A POST or PUT request with a {?et,lt,con} URI Template query and a Link Format * payload. */ public boolean setParameters(Request request) { LinkAttribute attr; String newEndpointType = ""; int newLifeTime = NetworkConfig.getStandard().getInt("RD_DEFAULT_LIFETIME"); String newContext = ""; /* * get lifetime from option query - only for PUT request. */ List<String> query = request.getOptions().getURIQueries(); for (String q : query) { // FIXME Do not use Link attributes for URI template variables attr = LinkAttribute.parse(q); if (attr.getName().equals(LinkFormat.END_POINT_TYPE)) { newEndpointType = attr.getValue(); } if (attr.getName().equals(LinkFormat.LIFE_TIME)) { newLifeTime = attr.getIntValue(); if (newLifeTime < 60) { LOGGER.warning("Enforcing minimal RD lifetime of 60 seconds (was " + newLifeTime + ")"); newLifeTime = 60; } } if (attr.getName().equals(LinkFormat.CONTEXT)) { newContext = attr.getValue(); } } setEndpointType(newEndpointType); setLifeTime(newLifeTime); // TODO check with draft authors if update should be atomic if (newContext.equals("")) { context = "coap://" + request.getSource() + ":" + request.getSourcePort(); } else { Request checkRequest = Request.newGet(); try { checkRequest.setURI(context); } catch (Exception e) { LOGGER.warning(e.toString()); return false; } } return updateEndpointResources(request.getPayloadString()); }
/** * Creates a new subResource for each resource the node wants register. Each resource is separated * by ",". E.g. A node can register a resource for reading the temperature and another one for * reading the humidity. */ private boolean updateEndpointResources(String linkFormat) { Scanner scanner = new Scanner(linkFormat); scanner.useDelimiter(","); List<String> pathResources = new ArrayList<String>(); while (scanner.hasNext()) { pathResources.add(scanner.next()); } for (String p : pathResources) { scanner = new Scanner(p); /* * get the path of the endpoint's resource. E.g. from * </readings/temp> it will select /readings/temp. */ String path = "", pathTemp = ""; if ((pathTemp = scanner.findInLine("</.*?>")) != null) { path = pathTemp.substring(1, pathTemp.length() - 1); } else { scanner.close(); return false; } ResourceBase resource = addNodeResource(path); /* * Since created the subResource, get all the attributes from * the payload. Each parameter is separated by a ";". */ scanner.useDelimiter(";"); while (scanner.hasNext()) { LinkAttribute attr = LinkAttribute.parse(scanner.next()); if (attr.getValue() == null) resource.getAttributes().addAttribute(attr.getName()); else resource.getAttributes().addAttribute(attr.getName(), attr.getValue()); } resource.getAttributes().addAttribute(LinkFormat.END_POINT, getEndpointIdentifier()); } scanner.close(); return true; }