/** * Builds a URL but does not perform any token replacement. * Equivalent to (and short-hand for) build(null). * * @return */ public String build() { return build(null); }
@Override public UrlBuilder newUrlBuilder() { return new DefaultUrlBuilder(); }
/** * Build a URL, resolving any tokens using the given TokenResolver. Additionally, * will call any TokenBinders for the given Object instance to bind values from * it into the URL. * * @param object an object of which to bind properties into the URL parameters. * @param tokenResolver a TokenResolver to perform token substitution. * @return a URL string. */ public String build(Object object, TokenResolver tokenResolver) { return build(buildFullUrlPattern(), object, tokenResolver); }
/** * Add an optional query-string segment to this UrlBuilder. If all of the * tokens in the query-string are bound, the segment is included in the * generated URL string during build(). However, if there are unbound * tokens in the resulting query-string segment, it is not included in the * generated URL string. * <p/> * Do not include any question mark ("?") or ampersand ("&") in the query-string * segment. * * @param query a query-string segment to optionally include. * @return this UrlBuilder instance to facilitate method chaining. */ public DefaultUrlBuilder withQuery(String query) { queries().add(query); return this; }
@Override public String toString() { StringBuilder s = new StringBuilder(); s.append("baseUrl: "); s.append(baseUrl == null ? "null" : baseUrl); s.append(", urlPattern: "); s.append(urlPattern == null ? "null" : urlPattern); printQueryStrings(s); return s.toString(); }
/** * Build a URL, resolving any tokens using the given TokenResolver. Additionally, * will call any TokenBinders for the given Object instance to bind values from * it into the URL. * * @param urlPattern a URL pattern, with optional tokens. * @param object an object of which to bind properties into the URL parameters. * @param tokenResolver a TokenResolver to perform token substitution. * @return a URL string. */ public String build(String urlPattern, Object object, TokenResolver tokenResolver) { if (tokenResolver == null) { return appendQueryString(urlPattern, null); } String url = tokenResolver.resolve(urlPattern, object); return appendQueryString(url, tokenResolver); }
/** * Build a new URL from the given URL pattern. Optional query-string parameters * may be appended. Tokens are resolved using the passed-in TokenResolver. * * @param urlPattern a URL pattern to bind, instead of using the one associated * with this builder. May not be null. * @param tokenResolver a TokenResolver instance set with bound token values. * @return a URL string. */ public String build(String urlPattern, TokenResolver tokenResolver) { return build(urlPattern, null, tokenResolver); }
/** * Build a URL, resolving any tokens using the given TokenResolver. * * @param tokenResolver a TokenResolver to perform token substitution. * @return a URL string. */ public String build(TokenResolver tokenResolver) { return build(buildFullUrlPattern(), null, tokenResolver); }
/** * Create an empty LinkBuilder, with no URL pattern. Using this constructor * mandates that you MUST use the build(String) form of build instead of the * parameterless, build() or other forms, as the latter will throw * IllegalStateException in this state, due to not having a URL in the * generated link. */ public DefaultLinkBuilder() { super(); urlBuilder = new DefaultUrlBuilder(); }
public Record create(Request request, Response response) { if (AuthenticateJwt.authenticateJwt(request, baseUrl) != true) { response.setResponseStatus(HttpResponseStatus.UNAUTHORIZED); return null; } Record entity = request.getBodyAs(Record.class, "Resource details not provided"); Record saved = service.create(entity); // Construct the response for create... response.setResponseCreated(); // Bind the resource with link URL tokens, etc. here... TokenResolver resolver = HyperExpress.bind(Constants.Url.RECORD_ID, Identifiers.MONGOID.format(saved.getId())); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.SINGLE_RECORD); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, resolver)); LOG.info("maintenance record created: " + Identifiers.MONGOID.format(saved.getId())); // Return the newly-created resource... return saved; }
/** * Create a new LinkBuilder, passing in the URL pattern in which to * substitute tokens. This URL pattern may represent the entire URL or just * the path portion (relative path). * <p/> * The URL pattern is templated, in that it contains tokens to be later * substituted for actual values. The tokens are delimited with beginning * and trailing curly-braces (e.g. '{token}'). * <p/> * If used in conjunction with baseUrl(), this URL pattern must be just the * path portion of the URL and should be prefixed with a leading slash * ('/'). * <p/> * For example: '/users/{userId}' or * 'http://www.example.com/api/users/{userId}' * * @param urlPattern a URL path with optional tokens of the form '{tokenName}' */ public DefaultLinkBuilder(String urlPattern) { super(); urlBuilder = new DefaultUrlBuilder(urlPattern); }
/** * * @param request * @param response * @return */ public Vehicle create(Request request, Response response) { if (jwtImpl.authenticateJwt(request, baseUrl) != true) { response.setResponseStatus(HttpResponseStatus.UNAUTHORIZED); return null; } Vehicle entity = request.getBodyAs(Vehicle.class, "Resource details not provided"); Vehicle saved = service.create(entity); // Construct the response for create... response.setResponseCreated(); // Bind the resource with link URL tokens, etc. here... TokenResolver resolver = HyperExpress.bind(Constants.Url.VEHICLE_ID, Identifiers.MONGOID.format(saved.getId())); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.SINGLE_VEHICLE); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, resolver)); LOG.info("vehicle created: " + Identifiers.MONGOID.format(saved.getId())); // Return the newly-created resource... return saved; }
@Override public DefaultUrlBuilder clone() { DefaultUrlBuilder b = new DefaultUrlBuilder(this.urlPattern); b.baseUrl = this.baseUrl; b.queries = (this.queries == null ? null : new ArrayList<String>(this.queries)); return b; }
/** * * @param request * @param response * @return */ public Transaction create(Request request, Response response) { if (AuthenticateJwt.authenticateJwt(request, baseUrl) != true) { response.setResponseStatus(HttpResponseStatus.UNAUTHORIZED); return null; } Transaction entity = request.getBodyAs(Transaction.class, "Resource details not provided"); Transaction saved = service.create(entity); // Construct the response for create... response.setResponseCreated(); // Bind the resource with link URL tokens, etc. here... TokenResolver resolver = HyperExpress.bind(Constants.Url.TRANSACTION_ID, Identifiers.MONGOID.format(saved.getId())); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.SINGLE_TRANSACTION); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, resolver)); LOG.info("valet record created: " + Identifiers.MONGOID.format(saved.getId())); // Return the newly-created resource... return saved; }
/** * * @param request * @param response * @return */ public Client create(Request request, Response response) { Client entity = request.getBodyAs(Client.class, "Resource details not provided"); String api_key = RandomStringUtils.randomAlphanumeric(24); entity.setApiKey(api_key); Client saved = service.create(entity); // Construct the response for create... response.setResponseCreated(); // Bind the resource with link URL tokens, etc. here... TokenResolver resolver = HyperExpress.bind(Constants.Url.CLIENT_ID, Identifiers.MONGOID.format(saved.getId())); // Include the Location header... String locationPattern = request.getNamedUrl(HttpMethod.GET, Constants.Routes.SINGLE_CLIENT); response.addLocationHeader(LOCATION_BUILDER.build(locationPattern, resolver)); LOG.info("client created: " + Identifiers.MONGOID.format(saved.getId())); // Return the newly-created resource... return saved; }