private ResourceLink generateResourceLink(Resource source, String id, UriInfo uriInfo) { UriBuilder builder = uriInfo.getBaseUriBuilder(); URI uri = builder.path(BookmarkResource.class) .path(getResourceTypeName(source)) .path(id) .build(); return new ResourceLink("bookmark", uri, source.getResourceMediaType()); }
@GET @Produces({BASE_JSON_MEDIA_TYPE, BASE_XML_MEDIA_TYPE}) public Response listAllResources() { RootRepresentation root = new RootRepresentation(REP_TYPE, uriInfo); for (TopLevelResource resource : resources) { root.addLink( new ResourceLink( toResourceName(resource.getResourceClass()), uriInfo.getAbsolutePathBuilder().path(resource.getResourceClass()).build(), resource.getResourceMediaType())); } String selected = SUB_XML_MEDIA_TYPE; for(MediaType mt : headers.getAcceptableMediaTypes()) { if(mt.isCompatible(MediaType.valueOf(SUB_JSON_MEDIA_TYPE))) { selected = SUB_JSON_MEDIA_TYPE; break; } } return Response.ok(root).type(selected).build(); }
public List<ResourceLink> getLinks() { List<ResourceLink> links = super.getLinks(); if (getUriInfo() != null) { if(doesNotContainRel("self") && id != null) { links.add( new ResourceLink( "self", getUriInfo().getBaseUriBuilder() .path(SessionResource.class) .segment("{id}") .build(id), SessionResource.SESSION_XML_MEDIA_TYPE)); } if(doesNotContainRel("parent") && parentId != null) { links.add( new ResourceLink( "parent", getUriInfo().getBaseUriBuilder() .path(ConferenceResource.class) .segment("{id}") .build(parentId), ConferenceResource.CONFERENCE_XML_MEDIA_TYPE)); } } return links; } }
public List<ResourceLink> getLinks() { List<ResourceLink> links = super.getLinks(); if (getUriInfo() != null) { if(doesNotContainRel("self") && id != null) { links.add( new ResourceLink( "self", getUriInfo().getBaseUriBuilder().clone() .path(ConferenceResource.class) .segment("{id}") .build(id))); } if(doesNotContainRel("session")) { links.add( new ResourceLink( "session", getUriInfo().getAbsolutePathBuilder().clone() .path("session") .build())); } } return links; } }
@Override public List<ResourceLink> getLinks() { List<ResourceLink> links = super.getLinks(); if (getUriInfo() != null) { if(doesNotContainRel("self") && handle != null) { links.add( new ResourceLink( "self", getUriInfo().getBaseUriBuilder().clone() .path(UserResource.class) .segment("{id}") .build(handle), UserResource.USER_XML_MEDIA_TYPE)); } } return links; } }
@Override public List<ResourceLink> getLinks() { List<ResourceLink> links = super.getLinks(); if (getUriInfo() != null) { if(doesNotContainRel("self") && id != null) { links.add( new ResourceLink( "self", getUriInfo().getBaseUriBuilder().clone() .path(AttachmentResource.class) .segment("{id}") .build(id), AttachmentResource.ATTACHMENT_XML_MEDIA_TYPE)); } } return links; } }
@Override public void perform() { given(). contentType(BASE_JSON_MEDIA_TYPE). body(new ResourceLink("test", URI.create("http://test.org/api/test/" + TARGET_ID + "/"))). then(). contentType(BASE_JSON_MEDIA_TYPE). statusCode(Status.OK.getStatusCode()). when(). patch(baseURL + "api/rel/{sourceObj}/{source}/{rel}/{targetObj}/", "sourceobject", SOURCE_ID, type, "targetobject"); } }).inspect(new ValueInjectionInspection(target, source));
@Test @InSequence(3) public void shouldBeAbleToRemoveRelation() throws Exception { given(). contentType(BASE_JSON_MEDIA_TYPE). body(new ResourceLink("test", URI.create("http://test.org/api/test/" + TARGET_ID + "/"))). then(). statusCode(Status.NO_CONTENT.getStatusCode()). when(). delete(baseURL + "api/rel/{sourceObj}/{source}/{rel}/{targetObj}/", "sourceobject", SOURCE_ID, type, "targetobject"); }
@Test @InSequence(1) public void shouldBeAbleToAddRelation() throws Exception { Warp.initiate(new Activity() { @Override public void perform() { given(). contentType(BASE_JSON_MEDIA_TYPE). body(new ResourceLink("test", URI.create("http://test.org/api/test/" + TARGET_ID + "/"))). then(). contentType(BASE_JSON_MEDIA_TYPE). statusCode(Status.OK.getStatusCode()). when(). patch(baseURL + "api/rel/{sourceObj}/{source}/{rel}/{targetObj}/", "sourceobject", SOURCE_ID, type, "targetobject"); } }).inspect(new ValueInjectionInspection(target, source)); }
@Override public void appendLinks(LinkableRepresentation<?> representation) { if(!(representation instanceof Identifiable)) { return; } Identifiable sourceIdentifiable = (Identifiable)representation; // TODO: Remove need for cast Collection<RelationMatch> matches = service.getMatchingRelations(representation.getSourceType()); for(RelationMatch target: matches) { UriBuilder buidler = representation.getUriInfo().getBaseUriBuilder().segment( "rel", getTypeName(target.getSourceModel()), sourceIdentifiable.getId(), target.getSource().getType(), getTypeName(target.getTargetModel())); // TODO: Dynamically lookup MediaType representation.addLink( new ResourceLink( target.getSource().getName(), buidler.build(), RelationResource.BASE_JSON_MEDIA_TYPE + "; type=" + getMediaTypeName(target.getTargetModel()))); } }