[DOCU-1907] client filter example Created: 06/May/19  Updated: 30/Jul/19  Resolved: 30/Jul/19

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Low
Reporter: Richard Gange Assignee: Richard Gange
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: 0d
Time Spent: 1h
Original Estimate: Not Specified

Issue Links:
relation
is related to MGNLRESTCL-1 Allow registering of custom filters e... Closed

 Description   

It would be really helpful to have a page/section which shows how to use this feature. For example, an Authorization token use case.



 Comments   
Comment by Richard Gange [ 30/Jul/19 ]

Added this example (5.5+):

package org.example.filters;
 
import info.magnolia.context.Context;
import info.magnolia.context.MgnlContext;
import java.io.IOException;
 
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.ext.Provider;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * Interceptor for rest easy to handle authentication with an access token.
 */
@Provider
public class AuthenticationInterceptor implements ClientRequestFilter {
     
    private static final Logger log = LoggerFactory.getLogger(SalesforceAuthenticationInterceptor.class);
     
    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        if (MgnlContext.hasInstance() && MgnlContext.hasAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE)) {
            requestContext.getHeaders().putSingle("Authorization",
                "OAuth " + MgnlContext.getAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE));
 
            log.trace("Authorization : {}",
                (String) MgnlContext.getAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE));
        }
    }
}

Also updated the configuration example to use it.

Generated at Mon Feb 12 01:22:21 CET 2024 using Jira 9.4.2#940002-sha1:46d1a51de284217efdcb32434eab47a99af2938b.