private List<Link> getEmbeddedLinks(List<Link> links) { List<Link> ret = new ArrayList<Link>(); for (Link link : links) { if (!navigationalRels.contains(link.getRel())) { if (link instanceof Affordance) { Affordance affordance = (Affordance) link; List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors(); for (ActionDescriptor actionDescriptor : actionDescriptors) { if ("GET".equals(actionDescriptor.getHttpMethod()) && !affordance.isTemplated()) { ret.add(link); } } } else { // templated links are actions, not embedded links if(!link.isTemplated()) { ret.add(link); } } } } return ret; }
@Override public Affordance withRel(String rel) { linkParams.set(REL.paramName, rel); return new Affordance(this.getHref(), linkParams, actionDescriptors); }
"no rels or reverse rels found, call rel() or rev() before building the affordance"); final Affordance affordance; affordance = new Affordance(new PartialUriTemplate(this.toString()), actionDescriptors, rels.toArray(new String[rels.size()])); for (Map.Entry<String, List<String>> linkParamEntry : linkParams.entrySet()) { final List<String> values = linkParamEntry.getValue(); for (String value : values) { affordance.addLinkParam(linkParamEntry.getKey(), value); affordance.addRev(reverseRel); affordance.setCollectionHolder(collectionHolder); return affordance;
@Override public String toString() { return getHeaderName() + ": " + asHeader(); }
if (link instanceof Affordance) { final Affordance affordance = (Affordance) link; final List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors(); if (!actionDescriptors.isEmpty()) { if (affordance.getUriTemplateComponents() .hasVariables()) { if ("hydra:search".equals(affordance.getRel()) || Cardinality.SINGLE == affordance .getCardinality()) { templatedLinks.add(affordance); } else { if (!affordance.isSelfRel() && Cardinality.COLLECTION == affordance.getCardinality()) { collectionAffordances.add(affordance); } else { if (affordance.isTemplated()) { templatedLinks.add(affordance); } else { jgen.writeStringField(JsonLdKeywords.AT_TYPE, "hydra:Collection"); PartialUriTemplateComponents templateComponents = collectionAffordance.getUriTemplateComponents(); if (!collectionAffordance.isBaseUriTemplated() && !collectionAffordance.hasUnsatisfiedRequiredVariables()) { String collectionUri = templateComponents.getBaseUri() + templateComponents.getQueryHead();
List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors(); if (actionDescriptors.isEmpty()) { if (affordance.isTemplated()) { if (!affordance.isBaseUriTemplated()) { for (ActionDescriptor actionDescriptor : actionDescriptors) { RequestMethod httpMethod = RequestMethod.valueOf(actionDescriptor.getHttpMethod()); beginDiv(); writeAnchor(OptionalAttributes.attr("href", affordance.expand() .getHref()) .and("rel", affordance.getRel()), affordance.getRel()); endDiv(); } else {
@Test public void testConstructorWithSingleRel() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing"); assertEquals("http://localhost/things/{id}", affordance.getHref()); assertEquals("thing", affordance.getRel()); Assert.assertThat(affordance.getRels(), Matchers.contains("thing")); }
if (link instanceof Affordance) { Affordance affordance = (Affordance) link; List<ActionDescriptor> actionDescriptors = affordance.getActionDescriptors(); for (ActionDescriptor actionDescriptor : actionDescriptors) { List<SirenField> fields = toSirenFields(actionDescriptor); if (!"GET".equals(actionDescriptor.getHttpMethod()) || affordance.isTemplated()) { String href; if (affordance.isTemplated()) { href = affordance.getUriTemplateComponents() .getBaseUri(); } else { href = affordance.getHref();
@Test public void testLinkToMethodNoArgsBuild() throws Exception { final Method getEventMethod = ReflectionUtils.findMethod(EventControllerSample.class, "getEvent", String.class); final Affordance affordance = factory.linkTo(getEventMethod, new Object[0]) .rel("foo") .build(); assertEquals("http://example.com/events/{eventId}", affordance.getHref()); assertEquals("foo", affordance.getRel()); }
@Test public void testLinkToMethodInvocationNamedRequestParamWithValue() throws Exception { final Affordance affordance = factory.linkTo(AffordanceBuilder.methodOn(EventControllerSample.class) .simpleQueryEvent("foo", null)) .rel("foo") .build(); // href strips optional variables assertEquals("http://example.com/events/simplequery?q=foo", affordance.getHref()); // full uritemplate assertEquals("http://example.com/events/simplequery?q=foo{&offset}", affordance.getUriTemplateComponents().toString()); assertEquals("foo", affordance.getRel()); }
@Test public void testIsTemplated() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing", "http://example.net/relation/other"); assertEquals("http://localhost/things/{id}", affordance.getHref()); Assert.assertTrue("must recognize template", affordance.isTemplated()); }
@Test public void testExpand() { final Affordance affordance = new Affordance("http://localhost/things{/id}", "thing"); assertEquals("http://localhost/things/100", affordance.expand(100) .getHref()); }
@Test public void testLinkToMethodInvocationReverseRel() throws Exception { final Affordance affordance = factory.linkTo(AffordanceBuilder.methodOn(EventControllerSample.class) .getEvent((String) null)) .reverseRel("schema:parent", "ex:children") .build(); assertEquals("http://example.com/events/{eventId}", affordance.getHref()); assertEquals("schema:parent", affordance.getRev()); assertEquals("ex:children", affordance.getRel()); } }
@Test public void testLinkExtensionParams() { final Affordance affordance = new Affordance("http://example.com"); affordance.addLinkParam("name", "name-to-distinguish-links-with-same-rel"); affordance.addLinkParam("deprecation", "http://example.com/why/this/is/deprecated"); affordance.addLinkParam("type", "application/json"); Affordance.DynaBean linkExtensions = affordance.getLinkExtensions(); assertEquals("application/json", affordance.getType()); assertEquals("must only contain link extension params", "{name=name-to-distinguish-links-with-same-rel, " + "deprecation=http://example.com/why/this/is/deprecated}", linkExtensions.toString()); }
@Test public void expandsSimpleStringVariablesPartially() { final Affordance affordance = new Affordance("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/{ref}", "thing"); Map<String, Object> args = new HashMap<String, Object>(); args.put("ref", 1234567890); assertEquals("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/1234567890", affordance.expandPartially(args).getHref()); }
@Test public void testLinkToMethod() throws Exception { final Method getEventMethod = ReflectionUtils.findMethod(EventControllerSample.class, "getEvent", String.class); final Affordance affordance = factory.linkTo(getEventMethod, new Object[0]) .rel("foo") .build(); assertEquals("http://example.com/events/{eventId}", affordance.getHref()); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion, in the order they appear in the template * @return partially expanded affordance */ public Affordance expandPartially(Object... arguments) { return new Affordance(partialUriTemplate.expand(arguments) .toString(), linkParams, actionDescriptors); }
@Test public void testActionDescriptorForRequestBody() { final Affordance affordance = AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(DummyController.class) .updateThing(1, (Thing) null)) .rel("event") .build(); Assert.assertEquals("Link: <http://example.com/things/1>; rel=\"event\"", affordance.toString()); final ActionDescriptor actionDescriptor = affordance.getActionDescriptors() .get(0); final ActionInputParameter thingParameter = actionDescriptor.getRequestBody(); Assert.assertEquals("Thing", ((Class) thingParameter.getGenericParameterType()).getSimpleName()); Assert.assertThat(thingParameter.isRequestBody(), Matchers.is(true)); Assert.assertEquals("updateThing", actionDescriptor.getActionName()); }
@Test public void testAffordanceAsHeader() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing", "http://example.net/relation/other"); assertEquals("<http://localhost/things/{id}>; rel=\"thing http://example.net/relation/other\"", affordance.asHeader()); }