@Override public TokenResolver newTokenResolver() { return new DefaultTokenResolver(useAnnotationsResolver); } }
public DefaultTokenResolver bind(String tokenName, List<String> multiValues) { if (multiValues == null || multiValues.isEmpty()) { remove(tokenName); } else { bind(tokenName, multiValues.get(0)); bindExtras(tokenName, multiValues); } return this; }
/** * Resolve the tokens in a collection of string patterns, binding additional * token values from the given Object first. Any TokenBinder callbacks are * called for the object before resolving the tokens. If object is null, no * token binders are called. The resulting strings may still contain tokens if * they do not have values bound. * * @param patterns a collection of string patterns optionally containing tokens. * @param object an instance for which to call TokenBinders. * @return a collection of strings with bound tokens substituted for values. */ public Collection<String> resolve(Collection<String> patterns, Object object) { if (object != null) { callTokenBinders(object); } return resolve(patterns); }
/** * Removes all bound tokens and token binder callbacks from this * TokenResolver, making it essentially empty. After reset() this * TokenResolver's state is as if it was newly instantiated. */ public void reset() { clear(); clearBinders(); }
/** * When bindAnnotations is true, inserts a TokenBinder that will bind an object's properties * to tokens via the {@link BindToken} annotation. * * @param bindAnnotations */ public DefaultTokenResolver(boolean bindAnnotations) { super(); this.bindAnnotations = bindAnnotations; initialize(); }
/** * Bind a token to a value. During resolve(), any token names matching * the given token name here will be replaced with the given value. * * Set a value to be substituted for a token in the string pattern. While * tokens in the pattern are delimited with curly-braces, the token name * does not contain the braces. The value is any string value. * * @param tokenName the name of a token in the string pattern. * @param value the string value to substitute for the token name in the URL pattern. * @return this TokenResolver instance to facilitate method chaining. */ public DefaultTokenResolver bind(String tokenName, String value) { if (value == null) { remove(tokenName); } else { values.put(tokenName, value); } return this; }
/** * Resolve the tokens in the collection of string patterns, returning a * collection of resolved strings. The resulting strings may still contain tokens * if they do not have token values bound. * * @param patterns a list of string patterns * @return a collection of strings with bound tokens substituted for values. */ public Collection<String> resolve(Collection<String> patterns) { List<String> resolved = new ArrayList<String>(patterns.size()); for (String pattern : patterns) { resolved.add(resolve(pattern)); } return resolved; }
private void initialize() { if (bindAnnotations) { binder(AnnotationTokenBinder.instance()); } }
/** * Resolve the tokens in a string pattern, binding additional token values from * the given Object first. Any TokenBinder callbacks are called for the * object before resolving the tokens. If object is null, no token binders * are called. * <p/> * <Strong>LIMITATION:</strong> As TokenBinder is typed, calling binder() * with TokenBinder instances for different generic types will cause * a {@link ClassCastException} during resolve(). * * @param pattern a pattern string optionally containing tokens. * @param object an instance for which to call TokenBinders. * @return a string with bound tokens substituted for values. * @throws ClassCastException if binder() called with TokenBinder instances for different generic types. */ public String resolve(String pattern, Object object) { if (object != null) { callTokenBinders(object); } return FORMATTER.format(pattern, values); }
/** * Removes all user set token binder callbacks from this TokenResolver. * If the internal setting 'bindAnnotations' is true, the annotation * token binder is not removed. */ public void clearBinders() { binders.clear(); initialize(); }
public Blog create(Request request, Response response) { Blog blog = request.getBodyAs(Blog.class, "Blog details not provided"); ValidationEngine.validateAndThrow(blog); Blog saved = blogs.create(blog); // Construct the response for create... response.setResponseCreated(); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.BLOG_ENTRY_READ_ROUTE); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, new DefaultTokenResolver())); // Return the newly-created item... return saved; }
public BlogEntry create(Request request, Response response) { String blogId = request.getHeader(Constants.Url.BLOG_ID_PARAMETER, "No Blog ID provided"); BlogEntry blogEntry = request.getBodyAs(BlogEntry.class, "BlogEntry details not provided"); Blog blog = blogs.read(UUID.parse(blogId)); blogEntry.setBlogId(blog.getUuid()); ValidationEngine.validateAndThrow(blogEntry); BlogEntry saved = blogEntries.create(blogEntry); // Construct the response for create... response.setResponseCreated(); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.BLOG_ENTRY_READ_ROUTE); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, new DefaultTokenResolver())); // Return the newly-created item... return saved; }