001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015 ForgeRock AS. 015 */ 016package org.forgerock.http.filter; 017 018import org.forgerock.http.Filter; 019import org.forgerock.http.Handler; 020import org.forgerock.http.header.MalformedHeaderException; 021import org.forgerock.http.header.TransactionIdHeader; 022import org.forgerock.http.protocol.Request; 023import org.forgerock.http.protocol.Response; 024import org.forgerock.services.context.Context; 025import org.forgerock.services.context.TransactionIdContext; 026import org.forgerock.util.promise.NeverThrowsException; 027import org.forgerock.util.promise.Promise; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * This filter aims to create a sub-transaction's id and inserts that value as a header of the request. 033 * 034 * Its main usage will be like this : 035 * <pre> 036 * {@code 037 * Handler handler = Handlers.chainOf(new HttpClientHandler(httpClient), new TransactionIdOutboundFilter()); 038 * } 039 * </pre> 040 * 041 */ 042public class TransactionIdOutboundFilter implements Filter { 043 044 private static final Logger logger = LoggerFactory.getLogger(TransactionIdOutboundFilter.class); 045 046 @Override 047 public Promise<Response, NeverThrowsException> filter(Context context, Request request, Handler next) { 048 if (context.containsContext(TransactionIdContext.class)) { 049 TransactionIdContext txContext = context.asContext(TransactionIdContext.class); 050 final String subTxId = txContext.getTransactionId().createSubTransactionId().getValue(); 051 try { 052 request.getHeaders().put(new TransactionIdHeader(subTxId)); 053 } catch (MalformedHeaderException ex) { 054 // Should not happen as the value is always valid. 055 logger.error("An error occured while building the TransactionIdHeader", ex); 056 } 057 } else { 058 logger.trace("Expecting to find an instance of TransactionIdContext in the chain, but there was none."); 059 } 060 061 return next.handle(context, request); 062 } 063 064}