private Endpoint createEndpoint(
     Resource resource, String relativePath, EntityTag entityTag, Date lastModified)
     throws EndpointCreationException {
   String candidatePath = relativePath;
   int repetitions = 0;
   while (repetitions < MAX_ENDPOINT_CREATION_FAILURE) {
     try {
       String resourcePath = calculateResourcePath(resource, candidatePath);
       Endpoint newEndpoint =
           this.factoryService.createEndpoint(resource, resourcePath, entityTag, lastModified);
       this.endpointRepository.add(newEndpoint);
       return newEndpoint;
     } catch (EndpointNotFoundException e) {
       throw new EndpointCreationException(
           "Could not calculate path for resource '" + resource.id() + "'", e);
     } catch (IllegalArgumentException e) {
       // TODO: Define a proper exception
       if (candidatePath != null) {
         repetitions++;
       }
       candidatePath = null;
     }
   }
   throw new EndpointCreationException(
       "Could not create endpoint for resource '" + resource.id() + "'");
 }
 public Endpoint deleteResourceEndpoint(Resource resource) throws EndpointNotFoundException {
   checkNotNull(resource, "ResourceSnapshot cannot be null");
   Endpoint endpoint = this.endpointRepository.endpointOfResource(resource.id());
   if (endpoint == null) {
     throw new EndpointNotFoundException(resource.id());
   }
   this.endpointRepository.remove(endpoint);
   this.listenerManager.notify(new EndpointDeletionNotification(endpoint));
   return endpoint;
 }
 public Endpoint modifyResourceEndpoint(Resource resource, EntityTag entityTag, Date lastModified)
     throws EndpointNotFoundException {
   checkNotNull(resource, "ResourceSnapshot cannot be null");
   checkNotNull(entityTag, "Entity tag cannot be null");
   checkNotNull(lastModified, "Last modified cannot be null");
   Endpoint endpoint = endpointRepository.endpointOfResource(resource.id());
   if (endpoint == null) {
     throw new EndpointNotFoundException(resource.id());
   }
   endpoint.modify(entityTag, lastModified);
   return endpoint;
 }
 private String generatePathForAttachment(Resource child, Resource parent)
     throws EndpointNotFoundException {
   Attachment attachment = parent.findAttachment(child.id());
   if (attachment == null) {
     return null;
   }
   Endpoint endpoint = getResourceEndpoint(parent.id());
   ResourceTemplate parentTemplate =
       this.templateManagementService.findTemplateById(parent.id().templateId());
   AttachedTemplate attachedTemplate = parentTemplate.attachedTemplate(attachment.id());
   StringBuilder builder = new StringBuilder();
   addSegment(builder, endpoint.path());
   addSegment(builder, attachedTemplate.path());
   return builder.toString();
 }
  private String calculateResourcePath(Resource resource, String desiredPath)
      throws EndpointNotFoundException {
    if (resource.isRoot()) {
      throw new IllegalStateException("Cannot get path for root resource");
    }
    Resource parent = this.resourceRepository.find(resource.parentId(), Resource.class);
    if (parent == null) {
      throw new IllegalStateException(
          "Could not load resource '" + resource.parentId() + "' from the repository");
    }

    String result =
        parent instanceof Container
            ? generatePathForMember(resource, (Container) parent, desiredPath)
            : null;
    if (result == null) {
      result = generatePathForAttachment(resource, parent);
      if (result == null) {
        throw new IllegalStateException(
            "Could not determine path for resource '"
                + resource.id()
                + "' with parent '"
                + parent.id()
                + "'");
      }
    }
    return result;
  }
 private String generatePathForMember(Resource child, Container parent, String desiredPath)
     throws EndpointNotFoundException {
   if (parent.hasMember(child.id())) {
     Endpoint endpoint = getResourceEndpoint(parent.id());
     ContainerTemplate parentTemplate =
         templateManagementService.findTemplateById(
             parent.id().templateId(), ContainerTemplate.class);
     if (parentTemplate == null) {
       throw new IllegalStateException("Could not find template resource '" + parent + "'");
     }
     StringBuilder builder = new StringBuilder();
     addSegment(builder, endpoint.path());
     addSegment(builder, parentTemplate.memberPath().or(""));
     addSegment(builder, IdGenerator.nextMemberId(parent));
     addSegment(builder, desiredPath);
     //			Object lastSegment = desiredPath;
     //			if(lastSegment==null) {
     //				lastSegment=IdGenerator.nextMemberId(parent);
     //			}
     //			addSegment(builder,lastSegment);
     return builder.toString();
   }
   return null;
 }