loading table of contents...

4.3.2.3. Post Processing Links

Similar 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.view.ViewUtils;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import com.coremedia.objectserver.web.links.UriComponentsHelper;
...
@LinkPostProcessor
public class MyLinkPostProcessor {
  @LinkPostProcessor
  public UriComponentsBuilder prependPrefix(UriComponents originalUri,
                                            HttpServletRequest request) {

    String baseUri = ViewUtils.getBaseUri(request);
    return UriComponentsHelper.prependPath(baseUri, originalUri);
  }
}
<beans xmlns="http://www.springframework.org/schema/beans">
  <import resource="classpath:/com/coremedia/cae/link-services.xml"/>
  <bean id="myLinkPostProcessor"
        class="com.mycompany.MyLinkPostProcessor"/>
</beans>

This example demonstrates how the base URI (context path and the servlet path) is prepended to an 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.