Smile CDR v2022.11.PRE
On this page:

11.6Subscription Examples

 

This page contains example interceptors that can be registered with Subscription.

11.6.1Example: Adding Authorization header

 

The following interceptor can be used to add any authorization header, like Basic Auth or Bearer token, before calling a REST hook deliverer for each resource delivery message.

/**
 * This class is a Hook Interceptor that is invoked immediately before REST HOOK
 * deliveries. It injects a header into the subscription object at runtime, which
 * is useful in cases where an Auth header is needed in subscriptions and this header
 * is not known beforehand.
 */
@Interceptor
public class SubscriptionRestHookDynamicHeaderInterceptor {

	/**
	 * The {@link Pointcut#SUBSCRIPTION_BEFORE_REST_HOOK_DELIVERY} is invoked immediately before each
	 * REST HOOK subscription delivery.
	 *
	 * @param theDeliveryMessage This object contains details about the subscription invocation, including
	 *                           the contents of the resource that will be delivered.
	 * @param theSubscription    This object contains details about the subscription, including the delivery endpoint,
	 *                           headers to add, etc. A new copy of this object is created for every individual delivery
	 *                           so it is fine to modify it, and this will not affect other deliveries or
	 *                           subscriptions.
	 * @return Return <code>true</code> if the subscription should proceed, or <code>false</code> if it
	 * should be aborted.
	 */
	@Hook(Pointcut.SUBSCRIPTION_BEFORE_REST_HOOK_DELIVERY)
	public boolean beforeRestHookDelivery(ResourceDeliveryMessage theDeliveryMessage, CanonicalSubscription theSubscription) {

		// In this example, we're just hardcoding an auth header, but we could just as easily be
		// looking it up somewhere
		String header = "Authorization: Bearer 1234567";

		// Add the header to the subscription, so that it will be appended during delivery
		theSubscription.addHeader(header);

		// Delivery should proceed
		return true;
	}


}