builder.append(variable.getName());
/** * Expands the {@link UriTemplate} using the given parameters. * * @param parameters must not be {@literal null}. * @return */ public URI expand(Map<String, ? extends Object> parameters) { if (TemplateVariables.NONE.equals(variables)) { return URI.create(baseUri); } Assert.notNull(parameters, "Parameters must not be null!"); org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri); UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters)); for (TemplateVariable variable : getOptionalVariables()) { appendToBuilder(builder, variable, parameters.get(variable.getName())); } return builder.build().toUri(); }
/** * Creates a new {@link UriTemplate} with the current {@link TemplateVariable}s augmented with the given ones. * * @param variables can be {@literal null}. * @return will never be {@literal null}. */ public UriTemplate with(TemplateVariables variables) { if (variables == null) { return this; } UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build(); List<TemplateVariable> result = new ArrayList<>(); for (TemplateVariable variable : variables) { boolean isRequestParam = variable.isRequestParameterVariable(); boolean alreadyPresent = components.getQueryParams().containsKey(variable.getName()); if (isRequestParam && alreadyPresent) { continue; } if (variable.isFragment() && StringUtils.hasText(components.getFragment())) { continue; } result.add(variable); } return new UriTemplate(baseUri, this.variables.concat(result)); }
/** * Creates the {@link Descriptor}s for pagination parameters. * * @param type * @return */ private List<Descriptor> getPaginationDescriptors(Class<?> type, HttpMethod method) { RepositoryInformation information = repositories.getRequiredRepositoryInformation(type); if (!information.isPagingRepository() || !getType(method).equals(Type.SAFE)) { return Collections.emptyList(); } Link linkToCollectionResource = entityLinks.linkToCollectionResource(type); List<TemplateVariable> variables = linkToCollectionResource.getVariables(); List<Descriptor> descriptors = new ArrayList<Descriptor>(variables.size()); ProjectionDefinitionConfiguration projectionConfiguration = configuration.getProjectionConfiguration(); for (TemplateVariable variable : variables) { // Skip projection parameter if (projectionConfiguration.getParameterName().equals(variable.getName())) { continue; } ResourceDescription description = SimpleResourceDescription.defaultFor(variable.getDescription()); descriptors.add(// descriptor().// name(variable.getName()).// type(Type.SEMANTIC).// doc(getDocFor(description)).// build()); } return descriptors; }
/** * Returns the names of the variables discovered. * * @return */ public List<String> getVariableNames() { List<String> names = new ArrayList<String>(); for (TemplateVariable variable : variables) { names.add(variable.getName()); } return names; }
builder.append(variable.getName());
/** * Expands the {@link UriTemplate} using the given parameters. * * @param parameters must not be {@literal null}. * @return */ public URI expand(Map<String, ? extends Object> parameters) { if (TemplateVariables.NONE.equals(variables)) { return URI.create(baseUri); } Assert.notNull(parameters, "Parameters must not be null!"); org.springframework.web.util.UriTemplate baseTemplate = new org.springframework.web.util.UriTemplate(baseUri); UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseTemplate.expand(parameters)); for (TemplateVariable variable : getOptionalVariables()) { appendToBuilder(builder, variable, parameters.get(variable.getName())); } return builder.build().toUri(); }
for (Integer variableInPart : variablesInPart) { final TemplateVariable variable = variables.get(variableInPart); final Object value = parameters.get(variable.getName()); if (value == null) { switch (variable.getType()) { case REQUEST_PARAM: case REQUEST_PARAM_CONTINUED: if (requiredArgs.isEmpty() || requiredArgs.contains(variable.getName())) { queryTail.append(variable.getName()); queryHead.append('&'); queryHead.append(variable.getName()) .append('=') .append(urlEncode(value.toString()));
for (Integer variableInPart : variablesInPart) { final TemplateVariable variable = variables.get(variableInPart); final Object value = parameters.get(variable.getName()); if (value == null) { switch (variable.getType()) { case REQUEST_PARAM: case REQUEST_PARAM_CONTINUED: if (requiredArgs.isEmpty() || requiredArgs.contains(variable.getName())) { queryTail.append(variable.getName()); queryHead.append('&'); queryHead.append(variable.getName()) .append('=') .append(urlEncode(value.toString()));
/** * Creates a new {@link UriTemplate} with the current {@link TemplateVariable}s augmented with the given ones. * * @param variables can be {@literal null}. * @return will never be {@literal null}. */ public UriTemplate with(TemplateVariables variables) { if (variables == null) { return this; } UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build(); List<TemplateVariable> result = new ArrayList<TemplateVariable>(); for (TemplateVariable variable : variables) { boolean isRequestParam = variable.isRequestParameterVariable(); boolean alreadyPresent = components.getQueryParams().containsKey(variable.getName()); if (isRequestParam && alreadyPresent) { continue; } if (variable.isFragment() && StringUtils.hasText(components.getFragment())) { continue; } result.add(variable); } return new UriTemplate(baseUri, this.variables.concat(result)); }
public final class VariableResolver extends TemplateVariableResolver { public VariableResolver() { super("var", "some description"); } @Override public void resolve(TemplateVariable variable, TemplateContext context) { final String name = variable.getName(); /* bar or baz */ final List params = variable.getVariableType().getParams(); /* ["boolean"] */ variable.setValues(computeSuggestions(name, params)); variable.setResolved(true); } private String[] computeSuggestions(String name, List params) { return new String[] {"true", "false"}; // TODO: more sophisticated proposals } // overwrite other methods! }
/** * Creates the {@link Descriptor}s for pagination parameters. * * @param type * @return */ private List<Descriptor> getPaginationDescriptors(Class<?> type, HttpMethod method) { RepositoryInformation information = repositories.getRequiredRepositoryInformation(type); if (!information.isPagingRepository() || !getType(method).equals(Type.SAFE)) { return Collections.emptyList(); } Link linkToCollectionResource = entityLinks.linkToCollectionResource(type); List<TemplateVariable> variables = linkToCollectionResource.getVariables(); List<Descriptor> descriptors = new ArrayList<Descriptor>(variables.size()); ProjectionDefinitionConfiguration projectionConfiguration = configuration.getProjectionConfiguration(); for (TemplateVariable variable : variables) { // Skip projection parameter if (projectionConfiguration.getParameterName().equals(variable.getName())) { continue; } ResourceDescription description = SimpleResourceDescription.defaultFor(variable.getDescription()); descriptors.add(// descriptor().// name(variable.getName()).// type(Type.SEMANTIC).// doc(getDocFor(description)).// build()); } return descriptors; }
break; fields.add(new SirenField(variable.getName(), "text", (String) null, variable.getDescription(), null));
break; fields.add(new SirenField(variable.getName(), "text", (String) null, variable.getDescription(), null));
private void appendLinkWithoutActionDescriptor(Link link) throws IOException { if (link.isTemplated()) { // TODO ensure that template expansion takes place for base uri Link expanded = link.expand(); // remove query variables beginForm(OptionalAttributes.attr("action", expanded.getHref()) .and("method", "GET")); List<TemplateVariable> variables = link.getVariables(); for (TemplateVariable variable : variables) { String variableName = variable.getName(); String label = variable.hasDescription() ? variable.getDescription() : variableName; writeLabelWithDoc(label, variableName, null); // no documentation url input(variableName, Type.TEXT); } } else { String rel = link.getRel(); String title = (rel != null ? rel : link.getHref()); // TODO: write html <link> instead of anchor <a> here? writeAnchor(OptionalAttributes.attr("href", link.getHref()) .and("rel", link.getRel()), title); } }
private void appendLinkWithoutActionDescriptor(Link link) throws IOException { if (link.isTemplated()) { // TODO ensure that template expansion takes place for base uri Link expanded = link.expand(); // remove query variables beginForm(OptionalAttributes.attr("action", expanded.getHref()) .and("method", "GET")); List<TemplateVariable> variables = link.getVariables(); for (TemplateVariable variable : variables) { String variableName = variable.getName(); String label = variable.hasDescription() ? variable.getDescription() : variableName; writeLabelWithDoc(label, variableName, null); // no documentation url input(variableName, Type.TEXT); } } else { String rel = link.getRel(); String title = (rel != null ? rel : link.getHref()); // TODO: write html <link> instead of anchor <a> here? writeAnchor(OptionalAttributes.attr("href", link.getHref()) .and("rel", link.getRel()), title); } }