@POST @Path("blueprint/{name}") public Response createBlueprint( @Context UriInfo uri, @PathParam("name") String name, ConfigManifest blueprint) { Response res; try { ZooKeeper zk = Controller.getInstance().getZKInstance(); blueprint.setUrl(uri.getAbsolutePath().toURL()); byte[] data = JAXBUtil.write(blueprint); Stat stat = zk.exists( CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + name, false); if (stat == null) { zk.create( CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + name, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else { throw new WebApplicationException(409); } res = Response.created(uri.getAbsolutePath()).build(); return res; } catch (WebApplicationException e) { throw e; } catch (Exception e) { LOG.error(ExceptionUtil.getStackTrace(e)); throw new WebApplicationException(500); } }
@PUT @Path("blueprint/{name}") public Response updateBlueprint( @Context UriInfo uri, @PathParam("name") String oldName, ConfigManifest blueprint) { Response res; try { ZooKeeper zk = Controller.getInstance().getZKInstance(); String newName = oldName; if (blueprint.getUrl() != null) { newName = ZookeeperUtil.getBaseURL(blueprint.getUrl().toString()); } else { blueprint.setUrl(uri.getAbsolutePath().toURL()); } byte[] data = JAXBUtil.write(blueprint); Stat stat = zk.exists( CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName, false); if (stat != null && oldName.equals(newName)) { // Update existing blueprint String path = CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + oldName; zk.delete(path, stat.getVersion()); zk.create( CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } else if (stat != null) { // Conflict in name change throw new WebApplicationException(409); } else { // Create new blueprint try { zk.create( CommonConfigurationKeys.ZOOKEEPER_CONFIG_BLUEPRINT_PATH_DEFAULT + '/' + newName, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException e) { throw new WebApplicationException(409); } } res = Response.noContent().build(); return res; } catch (WebApplicationException e) { throw e; } catch (Exception e) { LOG.error(ExceptionUtil.getStackTrace(e)); throw new WebApplicationException(500); } }