/** * {@inheritDoc} */ @Override public final String path() { return this.endpoint.path(); }
protected final <T extends Resource> T resolveAs(Class<? extends T> clazz) { if(this.resource==null) { this.resource = this.applicationContext.resolveResource(this.endpoint); if(this.resource==null) { throw new IllegalStateException("Expected resource at "+endpoint().path()); } } if(!clazz.isInstance(this.resource)) { throw new IllegalStateException("Expected resource "+this.resource.id()+" to be an instance of "+clazz.getCanonicalName()); } return clazz.cast(this.resource); }
/** * {@inheritDoc} */ @Override public void add(Endpoint endpoint) { this.writeLock.lock(); try { if(this.endpointsByPath.containsKey(endpoint.path())) { throw new IllegalArgumentException("An endpoint with path '"+endpoint.path()+"' already exists"); } Endpoint other=endpointOfResource(endpoint.resourceId()); if(other!=null) { throw new IllegalArgumentException("An endpoint with resource name '"+endpoint.resourceId()+"' already exists ("+other+")"); } this.endpointsByPath.put(endpoint.path(), endpoint); this.endpointsByResourceName.put(endpoint.resourceId(),endpoint); } finally { this.writeLock.unlock(); } }
private ResourceTemplate resolveTemplate(Endpoint endpoint) { Resource resource = this.applicationContext.resolveResource(endpoint); checkState(resource!=null,"Could not resolve endpoint %s",endpoint.path()); ResourceTemplate template = this.applicationContext.resourceTemplate(resource); checkState(template!=null,"Could not find template for resource %s",resource.id()); return template; }
@Override public void execute(EndpointRepository sut) { Endpoint result = sut.endpointOfPath(ep1.path()); System.out.println(result); } }
private Endpoint verifyEndpoint(String path, Resource resource) { Endpoint endpoint = findEndpoint(resource); assertThat(endpoint,notNullValue()); assertThat(endpoint.path(),equalTo(path)); assertThat(endpoint.deleted(),nullValue()); return endpoint; }
@Override public void execute(EndpointRepository sut) { Endpoint endpoint = sut.endpointOfPath(ep1.path()); endpoint.delete(deleted); } }
@Override public URI resolve(ResourceSnapshot resource) { ResourceId resourceId = ResourceId.createId( resource.name(), resource.templateId()); try { Endpoint endpoint = context(). resolveResource( resourceId); URI result = null; if(endpoint!=null && endpoint.deleted()==null) { result=this.canonicalBase.resolve(endpoint.path()); } return result; } catch (ApplicationEngineException e) { throw new SnapshotResolutionException("Could not resolve endpoint for resource '"+resourceId+"'",e); } }
@Override public void execute(EndpointRepository sut) { Endpoint endpointByPath = sut.endpointOfPath(ep1.path()); debug("Retrieving endpoint by path {%s} : %s",ep1.path(),endpointByPath); Endpoint endpointByResource = sut.endpointOfResource(resource.id()); debug("Retrieving endpoint by resource {%s} : %s",resource.id(),endpointByResource); } }
@Override public void execute(EndpointRepository sut) { Endpoint legacyEndpointByPath = sut.endpointOfPath(ep1.path()); debug("Retrieving endpoint by path {%s} : %s",ep1.path(),legacyEndpointByPath); Endpoint endpointByPath = sut.endpointOfPath(path); debug("Retrieving endpoint by path {%s} : %s",path,endpointByPath); Endpoint endpointByResource = sut.endpointOfResource(resource.id()); debug("Retrieving endpoint by resource {%s} : %s",resource.id(),endpointByResource); } }
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; }
private void publish(RootResource rootResource, Date creationDate) throws ApplicationConfigurationException { ResourceId resourceId = rootResource.resourceId(); String path = rootResource.path(); Resource prevResource = this.resourceRepository.resourceById(resourceId,Resource.class); Endpoint prevEndpoint = this.endpointRepository.endpointOfPath(path); if(prevEndpoint!=null && !prevEndpoint.resourceId().equals(resourceId)) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as that path is already in use by a resource %s",toString(resourceId),path,toString(prevEndpoint.resourceId()))); } if(prevEndpoint==null) { if(prevResource!=null) { throw new ApplicationConfigurationException(String.format("Resource %s cannot be published at '%s' as it is already published at '%s'",toString(resourceId),path,this.endpointRepository.endpointOfResource(resourceId).path())); } else { Resource resource=this.modelFactory.createResource(rootResource.template(),rootResource.name()); this.resourceRepository.add(resource); Endpoint endpoint=this.modelFactory.createEndpoint(path,resource,creationDate,EntityTag.createStrong(path)); this.endpointRepository.add(endpoint); } } }
@Test public void testResolverSnapshotResolver$resolveSnapshot$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() {{ snapshot.name();result=resourceId.name(); snapshot.templateId();result=resourceId.templateId(); engine.unwrap(DefaultApplicationEngine.class);result=engine; engine.activeContext();result=context; context.resolveResource(resourceId);result=endpoint; endpoint.deleted();result=null; endpoint.path();result=path; }}; new MockUp<ApplicationEngine>() { @Mock public ApplicationEngine engine() throws ApplicationEngineException { return engine; } }; CoreRuntimeDelegate sut=new CoreRuntimeDelegate(); ResourceSnapshotResolver resolver = sut.createResourceResolver(base, session); assertThat(resolver.resolve(snapshot),equalTo(base.resolve(path))); }
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(); }