@Override public EndpointManagementService build() { return new EndpointManagementService( super.service(TemplateManagementService.class)); }
public Endpoint createEndpointForResource(Resource resource, String relativePath, EntityTag entityTag, Date lastModified) throws EndpointCreationException { checkNotNull(resource,RESOURCE_CANNOT_BE_NULL); checkNotNull(entityTag,ENTITY_TAG_CANNOT_BE_NULL); checkNotNull(lastModified,LAST_MODIFIED_CANNOT_BE_NULL); Endpoint newEndpoint = createEndpoint(resource, relativePath, entityTag, lastModified); this.listenerManager.notify(new EndpointCreationNotification(newEndpoint)); return newEndpoint; }
private void createResource(final Resource resource, final Date lastModified, final String relativePath, final URI indirectId) { try { resource.setIndirectId(indirectId); this.resourceRepository.add(resource); final Endpoint newEndpoint= this.endpointManagementService. createEndpointForResource( resource, relativePath, generateEntityTag(resource), lastModified); if(LOGGER.isTraceEnabled()) { LOGGER.trace("Created "+resource); LOGGER.trace("Created "+newEndpoint); } } catch (final EndpointCreationException e) { throw new IllegalStateException(e); } }
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.resourceById(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 PublicResource resolveResource(final String path) { checkNotNull(path,"Endpoint path cannot be null"); Endpoint endpoint= engine(). endpointManagementService(). resolveEndpoint(path); return this.factory.createResource(endpoint); }
private String generatePathForMember(Resource child, Container parent, String desiredPath) throws EndpointNotFoundException { Member member = parent.findMember(child.id()); if(member!=null) { Endpoint endpoint=getResourceEndpoint(parent.id()); ContainerTemplate parentTemplate=this.templateManagementService.templateOfId(parent.id().templateId(),ContainerTemplate.class); if(parentTemplate==null) { throw new IllegalStateException("Could not find template resource '"+parent+"'"); } String slugPath=getSlugPath(parent, desiredPath); return PathBuilder. create(). addSegment(endpoint.path()). addSegment(parentTemplate.memberPath().or("")). addSegment(member.number()). addSegment(slugPath). build(); } return null; }
public Endpoint modifyResourceEndpoint(Resource resource, EntityTag entityTag, Date lastModified) throws EndpointNotFoundException { checkNotNull(resource,RESOURCE_CANNOT_BE_NULL); checkNotNull(entityTag,ENTITY_TAG_CANNOT_BE_NULL); checkNotNull(lastModified,LAST_MODIFIED_CANNOT_BE_NULL); Endpoint endpoint = this.endpointRepository.endpointOfResource(resource.id()); if(endpoint==null) { throw new EndpointNotFoundException(resource.id()); } endpoint. modify( entityTag, getModificationDate(endpoint,lastModified)); return endpoint; }
private void deleteResource(final Resource resource, final Date lastModified) { try { this.resourceRepository.remove(resource); final Endpoint endpoint = this.endpointManagementService. deleteResourceEndpoint( resource, lastModified); if(LOGGER.isTraceEnabled()) { LOGGER.trace("Deleted "+resource); LOGGER.trace("Deleted "+endpoint); } } catch (final EndpointNotFoundException e) { throw new IllegalStateException(e); } }
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) { LOGGER.debug("({}) Creating endpoint for {} [{},{},{}]",repetitions,resource.id(),entityTag,lastModified,relativePath); try { String resourcePath = calculateResourcePath(resource,candidatePath); LOGGER.debug("({}) Trying resource path {} ",repetitions,resourcePath); Endpoint newEndpoint = this.modelFactory.createEndpoint(resourcePath, resource, lastModified, entityTag); this.endpointRepository.add(newEndpoint); return newEndpoint; } catch (EndpointNotFoundException e) { throw new EndpointCreationException("Could not calculate path for resource '"+resource.id()+"'",e); } catch (IllegalArgumentException e) { LOGGER.debug("Could not create endpoint ({} --> {})",repetitions,candidatePath,e); repetitions++; candidatePath=null; } } throw new EndpointCreationException("Could not create endpoint for resource '"+resource.id()+"' after "+MAX_ENDPOINT_CREATION_FAILURE+" tries"); }
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.templateOfId(parent.id().templateId()); AttachedTemplate attachedTemplate = parentTemplate.attachedTemplate(attachment.id()); return PathBuilder. create(). addSegment(endpoint.path()). addSegment(attachedTemplate.path()). addSegment(attachment.version()>0?attachment.version():null). build(); }
private ResourceSnapshot resolveManagedResource(URI path) { try { Endpoint endpoint= applicationEngine(). endpointManagementService(). resolveEndpoint(path.toString()); ResourceSnapshot result=null; if(endpoint!=null && endpoint.deleted()==null) { org.ldp4j.application.kernel.resource.Resource resource = context(). resolveResource(endpoint); if(resource!=null) { ResourceId resourceId = resource.id(); result= this.session. find( ResourceSnapshot.class, resourceId.name(), context().resourceTemplate(resource).handlerClass()); } } return result; } catch (Exception e) { throw new SnapshotResolutionException("Could not resolve resource for endpoint '"+this.canonicalBase.resolve(path)+"'",e); } } }
public Endpoint deleteResourceEndpoint(Resource resource, Date deletionDate) throws EndpointNotFoundException { checkNotNull(resource,RESOURCE_CANNOT_BE_NULL); Endpoint endpoint = this.endpointRepository.endpointOfResource(resource.id()); if(endpoint==null) { throw new EndpointNotFoundException(resource.id()); } endpoint.delete(getModificationDate(endpoint,deletionDate)); this.listenerManager.notify(new EndpointDeletionNotification(endpoint)); return endpoint; }
@Test public void testResolverSnapshotResolver$resolveEndpoint$endpointNotFound() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.endpointManagementService();result=endpointManagementService; endpointManagementService.resolveEndpoint(path);result=null; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(base.resolve(path)),nullValue()); }
@Test public void testResolverSnapshotResolver$resolveEndpoint$endpointGone() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.endpointManagementService();result=endpointManagementService; endpointManagementService.resolveEndpoint(path);result=endpoint; endpoint.deleted();result=new Date(); }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(base.resolve(path)),nullValue()); }
@Test public void testResolverSnapshotResolver$resolveEndpoint$kernelResourceNotFound() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.endpointManagementService();result=endpointManagementService; endpointManagementService.resolveEndpoint(path);result=endpoint; endpoint.deleted();result=null; engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(endpoint);result=null; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(base.resolve(path)),nullValue()); }
@Test public void testResolverSnapshotResolver$resolveEndpoint$happyPath() throws Exception { final URI base = URI.create("http://www.examples.org/base/"); final String path="resource/path/"; final ResourceId resourceId = ResourceId.createId(NamingScheme.getDefault().name("name"),"template"); new Expectations() {{ engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.endpointManagementService();result=endpointManagementService; endpointManagementService.resolveEndpoint(path);result=endpoint; endpoint.deleted();result=null; engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(endpoint);result=resource; resource.id();result=resourceId; engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resourceTemplate(resource);result=template; template.handlerClass();result=ResourceHandler.class; session.find(ResourceSnapshot.class,resourceId.name(),ResourceHandler.class);result=snapshot; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(base.resolve(path)),sameInstance(snapshot)); }