/** * Reinitializes all the settings including, relationships, token bindings, resource factories, * to their initial, unmodified state. This method is useful for testing when multiple servers * are created one after another and must have clean state at startup. */ public static void reset() { INSTANCE._initialize(); }
/** * @param factoryStrategy * @param contentType */ private void _registerResourceFactoryStrategy(ResourceFactoryStrategy factoryStrategy, String contentType) { resourceFactory.addFactoryStrategy(factoryStrategy, contentType); }
/** * Return the type of the concrete Resource implementation that is created for the * given contentType. * * @param contentType * @return the Class of the concrete implementation */ public static Class<? extends Resource> getResourceType(String contentType) { return INSTANCE._getResourceType(contentType); }
/** * Define a relationship for the given rel name to a URL. * * @param rel the relationship name (rel name). * @param href the URL, possibly templated. * @return this relationship definition instance to facilitate method chaining. */ public RelationshipDefinition rel(String rel, String href) { return rel(rel, factory.newLinkBuilder(href)); }
private TokenResolver _bindToken(String token, String value) { return _acquireTokenResolver().bind(token, value); }
/** * Define a relationship for the given rel name to a URL. If supported, * the output rendering (serialization) will be an array (or list) instead * of a single object for this rel name. * * @param name the relationship name (rel name). * @param href the URL, possibly templated. * @return this relationship definition instance to facilitate method chaining. */ public RelationshipDefinition rels(String name, String href) { return rels(name, factory.newLinkBuilder(href)); }
private TokenResolver _acquireTokenResolver() { TokenResolver tr = _getTokenResolver(); if (tr == null) { tr = builderFactory.newTokenResolver(); tokenResolver.set(tr); } return tr; }
/** * @param object * @param contentType * @return */ private Resource _createResource(Object object, String contentType) { Resource r = resourceFactory.createResource(object, contentType); _assignResourceLinks(r, object, (object == null ? null : object.getClass())); return r; }
/** * Creates a collection resource, embedding the components in a rel name derived from the component type * simple class name. The class name is lower-cased and pluralized in order to create the rel name. * * @param components * @param componentType * @param contentType * @return a new Resource instance with the collection embedded (as Resources). */ private Resource _createCollectionResource(Collection<?> components, Class<?> componentType, String contentType) { String componentRel = relationshipDefinition.getCollectionRelFor(componentType); return _createCollectionResource(components, componentType, componentRel, contentType); }
private <T> TokenResolver _addTokenBinder(TokenBinder<T> callback) { return _acquireTokenResolver().binder(callback); }
/** * Creates a collection resource, embedding the components in a rel name derived from the component type * simple class name. The class name is lower-cased and pluralized in order to create the rel name. * * @param components the objects to embed. They will be converted to Resource instances also. * @param componentType the object type of the components. * @param contentType the desired content type of the resource (e.g. "application/hal+json") * @return a new Resource instance with the collection embedded (as Resources). */ public static Resource createCollectionResource(Collection<?> components, Class<?> componentType, String contentType) { return INSTANCE._createCollectionResource(components, componentType, contentType); }
/** * Remove all the token substitution bindings and TokenBinder callbacks from * this thread's TokenResolver instance. * <p/> * It is recommended to call clearTokenBindings() after each request is complete * to prevent TokenResolver and TokenBinder instance buildup. Otherwise, they * are additive. */ public static void clearTokenBindings() { INSTANCE._clearTokenBindings(); }
/** * Bind a TokenBinder to the elements in a collection resource. * When a collection resource is created via createCollectionResource(), * the TokenBinder is called for each element in the collection to bind * URL tokens to individual properties within the element, if necessary. * <p/> * The TokenBinder is specific to the current thread. * * @param callback a TokenBinder */ public static <T> void tokenBinder(TokenBinder<T> callback) { INSTANCE._addTokenBinder(callback); }
/** * Create a resource instance from the object for the given content type. Properties from * the object are copied into the resulting Resource. Also, links are injected for appropriate * relationships defined via HyperExpress.relationships(), using any HyperExpress.bind() or * HyperExpress.tokenBinder() settings to populate the tokens in the URLs. * * @param object * @param contentType * @return */ public static Resource createResource(Object object, String contentType) { return INSTANCE._createResource(object, contentType); }
private Class<? extends Resource> _getResourceType(String contentType) { return resourceFactory.getResourceType(contentType); }
/** * Create a collection of resource instances from a collection of objects for the given content type. * A new resource is created for each component in the collection unless the component is already * a resource. In this case, links are applied and the resource is added directly to the resuling * collection. * * @param components * @param componentType the Class (type) of each component in the collection. * @param contentType * @return */ public static Collection<Resource> createResources(Collection<?> components, Class<?> componentType, String contentType) { return INSTANCE._createResources(components, componentType, contentType); }
/** * Bind a URL token to a string value. During resource creation, any URL * tokens matching the given token string are replace with the provided * value. * <p/> * The TokenResolver bindings are specific to the current thread. * * @param token a URL token name. * @param value the substitution value. * @return the underlying TokenResolver for this binding. */ public static TokenResolver bind(String token, String value) { return INSTANCE._bindToken(token, value); }
/** * Entry-point into the deep-copy functionality. * * @param from an Object instance, never null. * @param to a presumably-empty Resource instance. */ protected void copyProperties(Object from, Resource to) { copyProperties0(from.getClass(), from, to); }
private HyperExpress() { _initialize(); }
/** * Creates a collection resource, embedding the components in the given componentRel name. * * @param components the objects to embed. They will be converted to Resource instances also. * @param componentType the object type of the components. * @param componentRel the 'rel' name to use when embedding the resources. * @param contentType the desired content type of the resource (e.g. "application/hal+json") * @return a new Resource instance with the collection embedded (as Resources). */ public static Resource createCollectionResource(Collection<?> components, Class<?> componentType, String componentRel, String contentType) { return INSTANCE._createCollectionResource(components, componentType, componentRel, contentType); }