/** * Creates a new {@link AffordanceBuilder} pointing to this server, but without ActionDescriptor. */ AffordanceBuilder() { this(new PartialUriTemplate(getBuilder().build() .toString()).expand(Collections.<String, Object>emptyMap()), Collections.<ActionDescriptor>emptyList()); }
@Override public AffordanceBuilder linkTo(Class<?> controller, Object... parameters) { Assert.notNull(controller); String mapping = MAPPING_DISCOVERER.getMapping(controller); PartialUriTemplate partialUriTemplate = new PartialUriTemplate(mapping == null ? "/" : mapping); Map<String, Object> values = new HashMap<String, Object>(); Iterator<String> names = partialUriTemplate.getVariableNames() .iterator(); // there may be more or less mapping variables than arguments for (Object parameter : parameters) { if (!names.hasNext()) { break; } values.put(names.next(), parameter); } return new AffordanceBuilder().slash(partialUriTemplate.expand(values)); }
/** * Gets parts of the uri template such as base uri, expanded query part, unexpanded query part etc. * * @return template component parts */ @JsonIgnore public PartialUriTemplateComponents getUriTemplateComponents() { return partialUriTemplate.asComponents(); }
/** * Strips all variables which are not required by any of the given action descriptors. If no action descriptors are * given, nothing will be stripped. * * @param actionDescriptors * to decide which variables are optional, may be empty * @return partial uri template components without optional variables, if actionDescriptors was not empty */ public PartialUriTemplateComponents stripOptionalVariables(List<ActionDescriptor> actionDescriptors) { return getUriTemplateComponents(Collections.<String, Object>emptyMap(), getRequiredArgNames(actionDescriptors)); }
/** * Expands the template using given parameters * * @param parameters * for expansion in the order of appearance in the template, must not be empty * @return expanded template */ public PartialUriTemplateComponents expand(Object... parameters) { List<String> variableNames = getVariableNames(); Map<String, Object> parameterMap = new LinkedHashMap<String, Object>(); int i = 0; for (String variableName : variableNames) { if (i < parameters.length) { parameterMap.put(variableName, parameters[i++]); } else { break; } } return getUriTemplateComponents(parameterMap, Collections.<String>emptyList()); }
@Test public void testToStringWithQueryVariablesContainingDot() throws Exception { PartialUriTemplate partialUriTemplateComponents = new PartialUriTemplate ("http://localhost/events/query{?foo1,foo2,bar.baz,bars.empty,offset,size,strings.empty}"); assertThat(partialUriTemplateComponents.getVariableNames(), contains("foo1", "foo2", "bar.baz", "bars.empty", "offset", "size", "strings.empty")); }
private Affordance(String uriTemplate, MultiValueMap<String, String> linkParams, List<ActionDescriptor> actionDescriptors) { this(new PartialUriTemplate(uriTemplate), actionDescriptors); // no rels to pass this.linkParams = linkParams; // takes care of rels }
@Override public URI toUri() { PartialUriTemplate partialUriTemplate = new PartialUriTemplate(partialUriTemplateComponents.toString()); final String actionLink = partialUriTemplate.stripOptionalVariables(actionDescriptors) .toString(); if (actionLink == null || actionLink.contains("{")) { throw new IllegalStateException("cannot convert template to URI"); } return UriComponentsBuilder.fromUriString(actionLink) .build() .toUri(); }
public static List<ActionDescriptor> getActionDescriptors(Link link) { List<ActionDescriptor> actionDescriptors; if (link instanceof Affordance) { actionDescriptors = ((Affordance) link).getActionDescriptors(); } else { SpringActionDescriptor actionDescriptor = new SpringActionDescriptor("get", RequestMethod.GET .name()); PartialUriTemplate partialUriTemplate = new PartialUriTemplate(link.getHref()); PartialUriTemplateComponents parts = partialUriTemplate.asComponents(); actionDescriptors = Arrays.asList((ActionDescriptor) actionDescriptor); } return actionDescriptors; }
/** * 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 * @return partially expanded affordance */ public Affordance expandPartially(Map<String, ? extends Object> arguments) { return new Affordance(partialUriTemplate.expand((Map<String, Object>) arguments) .toString(), linkParams, actionDescriptors); }
/** * Expands the template using given parameters * * @param parameters * for expansion, must not be empty * @return expanded template */ public PartialUriTemplateComponents expand(Map<String, ?> parameters) { return getUriTemplateComponents(parameters, Collections.<String>emptyList()); }
super(uriTemplate.stripOptionalVariables(actionDescriptors) .toString()); this.partialUriTemplate = uriTemplate;
private Affordance(String uriTemplate, MultiValueMap<String, String> linkParams, List<ActionDescriptor> actionDescriptors) { this(new PartialUriTemplate(uriTemplate), actionDescriptors); // no rels to pass this.linkParams = linkParams; // takes care of rels }
@Override public URI toUri() { PartialUriTemplate partialUriTemplate = new PartialUriTemplate(partialUriTemplateComponents.toString()); final String actionLink = partialUriTemplate.stripOptionalVariables(actionDescriptors) .toString(); if (actionLink == null || actionLink.contains("{")) { throw new IllegalStateException("cannot convert template to URI"); } return UriComponentsBuilder.fromUriString(actionLink) .build() .toUri(); }
public static List<ActionDescriptor> getActionDescriptors(Link link) { List<ActionDescriptor> actionDescriptors; if (link instanceof Affordance) { actionDescriptors = ((Affordance) link).getActionDescriptors(); } else { SpringActionDescriptor actionDescriptor = new SpringActionDescriptor("get", RequestMethod.GET .name()); PartialUriTemplate partialUriTemplate = new PartialUriTemplate(link.getHref()); PartialUriTemplateComponents parts = partialUriTemplate.asComponents(); actionDescriptors = Arrays.asList((ActionDescriptor) actionDescriptor); } return actionDescriptors; }
/** * Expands the template using given parameters * * @param parameters * for expansion in the order of appearance in the template, must not be empty * @return expanded template */ public PartialUriTemplateComponents expand(Object... parameters) { List<String> variableNames = getVariableNames(); Map<String, Object> parameterMap = new LinkedHashMap<String, Object>(); int i = 0; for (String variableName : variableNames) { if (i < parameters.length) { parameterMap.put(variableName, parameters[i++]); } else { break; } } return getUriTemplateComponents(parameterMap, Collections.<String>emptyList()); }
/** * 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); }
/** * Strips all variables which are not required by any of the given action descriptors. If no action descriptors are * given, nothing will be stripped. * * @param actionDescriptors * to decide which variables are optional, may be empty * @return partial uri template components without optional variables, if actionDescriptors was not empty */ public PartialUriTemplateComponents stripOptionalVariables(List<ActionDescriptor> actionDescriptors) { return getUriTemplateComponents(Collections.<String, Object>emptyMap(), getRequiredArgNames(actionDescriptors)); }
/** * Expands the template using given parameters * * @param parameters * for expansion, must not be empty * @return expanded template */ public PartialUriTemplateComponents expand(Map<String, ?> parameters) { return getUriTemplateComponents(parameters, Collections.<String>emptyList()); }