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 */ 016 017package org.forgerock.http.header; 018 019import static org.forgerock.http.header.HeaderUtil.parseSingleValuedHeader; 020 021import java.util.Collections; 022import java.util.List; 023 024import org.forgerock.http.protocol.Header; 025import org.forgerock.http.protocol.Request; 026import org.forgerock.services.TransactionId; 027 028/** 029 * Processes the transactionId header used mainly for audit purpose. 030 */ 031public final class TransactionIdHeader extends Header { 032 033 /** 034 * Constructs a new header, initialized from the specified request. 035 * 036 * @param request 037 * The request to initialize the header from. 038 * @return The parsed header 039 * @throws MalformedHeaderException 040 * if the value is not acceptable for a {@code TransactionId} 041 */ 042 public static TransactionIdHeader valueOf(final Request request) throws MalformedHeaderException { 043 return valueOf(parseSingleValuedHeader(request, NAME)); 044 } 045 046 /** 047 * Constructs a new header, initialized from the specified string value. 048 * 049 * @param value 050 * The value to initialize the header from. 051 * @return The parsed header. 052 * @throws MalformedHeaderException 053 * if the value is not acceptable for a {@code TransactionId} 054 */ 055 public static TransactionIdHeader valueOf(final String value) throws MalformedHeaderException { 056 return new TransactionIdHeader(value); 057 } 058 059 /** The name of this header. */ 060 public static final String NAME = "X-ForgeRock-TransactionId"; 061 062 /** 063 * The TransactionId built from the header value. 064 */ 065 private TransactionId transactionId = null; 066 067 /** 068 * Constructs a new header with the provided value for the transaction id. The transactionId will be null if either 069 * the value is null or empty. 070 * 071 * @param value 072 * The value for the transaction id. 073 * @throws MalformedHeaderException 074 * if the value is not acceptable for a {@code TransactionId} 075 */ 076 public TransactionIdHeader(String value) throws MalformedHeaderException { 077 try { 078 this.transactionId = new TransactionId(value); 079 } catch (IllegalArgumentException e) { 080 throw new MalformedHeaderException(e); 081 } 082 } 083 084 /** 085 * Returns the transaction id. 086 * 087 * @return The transaction id 088 */ 089 public TransactionId getTransactionId() { 090 return transactionId; 091 } 092 093 @Override 094 public String getName() { 095 return NAME; 096 } 097 098 @Override 099 public List<String> getValues() { 100 return transactionId != null 101 ? Collections.singletonList(transactionId.getValue()) 102 : Collections.<String>emptyList(); 103 } 104 105 static class Factory extends AbstractSingleValuedHeaderFactory<TransactionIdHeader> { 106 107 @Override 108 public TransactionIdHeader parse(String value) throws MalformedHeaderException { 109 return valueOf(value); 110 } 111 } 112}