Content Application Developer Manual / Version 2512.0
Table Of ContentsSimilar to handler interceptors, it is also possible to post process generated links. A common use case is to prepend a prefix (such as context and servlet path) to the URI when the link schemes are used to generate the link suffixes only.
package com.mycompany; import com.coremedia.objectserver.request.RequestUtils; import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponents; ... @LinkPostProcessor public class MyLinkPostProcessor { @LinkPostProcessor public UriComponentsBuilder prependPrefix(UriComponents originalUri, HttpServletRequest request) { String baseUri = RequestUtils.getBaseUri(request); UriComponents encodedUri = originalUri.encode(); return UriComponentsBuilder.newInstance() .uriComponents(encodedUri) .replacePath(baseUri) .pathSegment(encodedUri.getPathSegments().toArray(new String[]{})) .build(true); } }
@Configuration(proxyBeanMethods = false) public class BlueprintLinksPostprocessorsConfiguration { @Bean public MyLinkPostProcessor myLinkPostProcessor() { return new MyLinkPostProcessor(); } }
This example demonstrates how the base URI (context path and the servlet path)
is prepended to a URI that has been built by an annotated link scheme.
Writing a post processor is quite similar to writing a link scheme.
The main difference is that the original link needs to be passed to the post processor method as a
parameter of type UriComponents or UriComponentsBuilder.
All other parameters bindings as well as the possible return types are the same. Just like
the @Link annotation, the @LinkPostProcessor supports an optional
type element which restricts the post-processor to links for the particular bean types.


