@Override public boolean updateResource(Resource resource) { if (resource == null) return false; if (resources.containsKey(resource.getPath())) { addResource(resource); logger.info("updated ressource: " + resource.getPath()); return true; } else return false; }
/** * Builds the full URI path to this resource method. * * @return the full URI path to this resource method. */ public String getFullpath() { List<String> subpaths = new ArrayList<String>(); if (getSubpath() != null) { subpaths.add(0, getSubpath()); } Resource parent = getParent(); while (parent != null) { subpaths.add(0, parent.getPath()); parent = parent.getParent(); } StringBuilder builder = new StringBuilder(); for (String subpath : subpaths) { subpath = subpath.trim(); if (!subpath.startsWith("/")) { subpath = '/' + subpath; } while (subpath.endsWith("/")) { subpath = subpath.substring(0, subpath.length() - 1); } subpath = scrubParamNames(subpath); builder.append(subpath); } return builder.toString(); }
/*corresponding to the coap spec the put is an update or create (or error)*/ public CoapResponseCode CoapResponseCode(Resource resource) { Resource res = readResource(resource.getPath()); // TODO: check results if (res == null) { createResource(resource); return CoapResponseCode.Created_201; } else { updateResource(resource); return CoapResponseCode.Changed_204; } }
private List<RuntimeResource.Builder> getRuntimeResources(List<Resource> resources) { Map<String, List<Resource>> regexMap = Maps.newHashMap(); for (Resource resource : resources) { String path = resource.getPath(); String regex = null; if (path != null) { if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } regex = new PathTemplate(path).getPattern().getRegex(); } List<Resource> listFromMap = regexMap.get(regex); if (listFromMap == null) { listFromMap = Lists.newArrayList(); regexMap.put(regex, listFromMap); } listFromMap.add(resource); } List<RuntimeResource.Builder> runtimeResources = Lists.newArrayList(); for (Map.Entry<String, List<Resource>> entry : regexMap.entrySet()) { final List<Resource> resourcesWithSameRegex = entry.getValue(); List<Resource> childResources = Lists.newArrayList(); for (final Resource res : resourcesWithSameRegex) { childResources.addAll(res.getChildResources()); } List<RuntimeResource.Builder> childRuntimeResources = getRuntimeResources(childResources); runtimeResources.add( new RuntimeResource.Builder( resourcesWithSameRegex, childRuntimeResources, entry.getKey())); } return runtimeResources; }
private void addResource(Resource resource) { resource.registerServerListener(this); resources.put(resource.getPath(), resource); coreResource.registerResource(resource); }
@Override public void resourceChanged(Resource resource) { logger.info("Resource changed: " + resource.getPath()); }
public String addWadlToRegistry( RequestContext requestContext, Resource resource, String resourcePath, boolean skipValidation) throws RegistryException { String wadlName = RegistryUtils.getResourceName(resourcePath); String version = requestContext.getResource().getProperty(RegistryConstants.VERSION_PARAMETER_NAME); if (version == null) { version = CommonConstants.WADL_VERSION_DEFAULT_VALUE; requestContext.getResource().setProperty(RegistryConstants.VERSION_PARAMETER_NAME, version); } OMElement wadlElement; String wadlContent; Object resourceContent = resource.getContent(); if (resourceContent instanceof String) { wadlContent = (String) resourceContent; } else { wadlContent = new String((byte[]) resourceContent); } try { XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(wadlContent)); StAXOMBuilder builder = new StAXOMBuilder(reader); wadlElement = builder.getDocumentElement(); } catch (XMLStreamException e) { // This exception is unexpected because the WADL already validated String msg = "Unexpected error occured " + "while reading the WADL at " + resourcePath + "."; log.error(msg); throw new RegistryException(msg, e); } String wadlNamespace = wadlElement.getNamespace().getNamespaceURI(); String namespaceSegment = CommonUtil.derivePathFragmentFromNamespace(wadlNamespace).replace("//", "/"); String actualPath = getChrootedWadlLocation(requestContext.getRegistryContext()) + namespaceSegment + version + "/" + wadlName; OMElement grammarsElement = wadlElement.getFirstChildWithName(new QName(wadlNamespace, "grammars")); if (StringUtils.isNotBlank(requestContext.getSourceURL())) { String uri = requestContext.getSourceURL(); if (!skipValidation) { validateWADL(uri); } if (resource.getUUID() == null) { resource.setUUID(UUID.randomUUID().toString()); } String wadlBaseUri = uri.substring(0, uri.lastIndexOf("/") + 1); if (grammarsElement != null) { // This is to avoid evaluating the grammars import when building AST grammarsElement.detach(); wadlElement.addChild(resolveImports(grammarsElement, wadlBaseUri, version)); } } else { if (!skipValidation) { File tempFile = null; BufferedWriter bufferedWriter = null; try { tempFile = File.createTempFile(wadlName, null); bufferedWriter = new BufferedWriter(new FileWriter(tempFile)); bufferedWriter.write(wadlElement.toString()); bufferedWriter.flush(); } catch (IOException e) { String msg = "Error occurred while reading the WADL File"; log.error(msg, e); throw new RegistryException(msg, e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { String msg = "Error occurred while closing File writer"; log.warn(msg, e); } } } validateWADL(tempFile.toURI().toString()); try { delete(tempFile); } catch (IOException e) { String msg = "An error occurred while deleting the temporary files from local file system."; log.warn(msg, e); throw new RegistryException(msg, e); } } if (grammarsElement != null) { grammarsElement = resolveImports(grammarsElement, null, version); wadlElement.addChild(grammarsElement); } } requestContext.setResourcePath(new ResourcePath(actualPath)); if (resource.getProperty(CommonConstants.SOURCE_PROPERTY) == null) { resource.setProperty(CommonConstants.SOURCE_PROPERTY, CommonConstants.SOURCE_AUTO); } registry.put(actualPath, resource); addImportAssociations(actualPath); if (getCreateService()) { OMElement serviceElement = RESTServiceUtils.createRestServiceArtifact( wadlElement, wadlName, version, RegistryUtils.getRelativePath(requestContext.getRegistryContext(), actualPath)); String servicePath = RESTServiceUtils.addServiceToRegistry(requestContext, serviceElement); registry.addAssociation(servicePath, actualPath, CommonConstants.DEPENDS); registry.addAssociation(actualPath, servicePath, CommonConstants.USED_BY); String endpointPath = createEndpointElement(requestContext, wadlElement, version); if (endpointPath != null) { registry.addAssociation(servicePath, endpointPath, CommonConstants.DEPENDS); registry.addAssociation(endpointPath, servicePath, CommonConstants.USED_BY); } } return resource.getPath(); }