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.opends.server.controls;
017
018import static org.opends.server.util.ServerConstants.OID_TRANSACTION_ID_CONTROL;
019
020import java.io.IOException;
021
022import org.forgerock.opendj.io.ASN1Writer;
023import org.forgerock.opendj.ldap.ByteString;
024import org.forgerock.opendj.ldap.ResultCode;
025import org.opends.messages.ProtocolMessages;
026import org.opends.server.types.Control;
027import org.opends.server.types.DirectoryException;
028
029/**
030 * Control that provides a transaction ID.
031 * <p>
032 * The transaction ID is related to Common Audit : it is used for tracking the
033 * processing of a user-interaction as it passes through the Forgerock stack
034 * <p>
035 * The control's value is the UTF-8 encoding of the transaction ID.
036 */
037public class TransactionIdControl extends Control
038{
039  /** ControlDecoder implementation to decode this control from a ByteString. */
040  private static final class Decoder implements ControlDecoder<TransactionIdControl>
041  {
042    @Override
043    public TransactionIdControl decode(boolean isCritical, ByteString value) throws DirectoryException
044    {
045      if (value == null)
046      {
047        throw new DirectoryException(ResultCode.PROTOCOL_ERROR,
048            ProtocolMessages.ERR_TRANSACTION_ID_CONTROL_HAS_NO_VALUE.get());
049      }
050      return new TransactionIdControl(isCritical, value.toString());
051    }
052
053
054    @Override
055    public String getOID()
056    {
057      return OID_TRANSACTION_ID_CONTROL;
058    }
059
060  }
061
062  /** The Control Decoder that can be used to decode this control. */
063  public static final ControlDecoder<TransactionIdControl> DECODER = new Decoder();
064
065  /** The id value of this control. */
066  private final String transactionId;
067
068  /**
069   * Creates a new Transaction Id Control.
070   *
071   * @param  isCritical  Indicates whether this control should be considered
072   *                     critical to the operation processing.
073   * @param  transactionId The id to pass through this control.
074   */
075  public TransactionIdControl(boolean isCritical, String transactionId)
076  {
077    super(OID_TRANSACTION_ID_CONTROL, isCritical);
078    this.transactionId = transactionId;
079  }
080
081  /**
082   * Writes this control's value to an ASN.1 writer. The value (if any) must be
083   * written as an ASN1OctetString.
084   *
085   * @param writer
086   *          The ASN.1 output stream to write to.
087   * @throws IOException
088   *           If a problem occurs while writing to the stream.
089   */
090  @Override
091  public void writeValue(ASN1Writer writer) throws IOException
092  {
093    writer.writeOctetString(transactionId);
094  }
095
096  /**
097   * Retrieves the transaction id associated with this control.
098   *
099   * @return  The transaction id associated with this control.
100   */
101  public String getTransactionId()
102  {
103    return transactionId;
104  }
105
106  /**
107   * Appends a string representation of this control to the provided buffer.
108   *
109   * @param buffer
110   *          The buffer to which the information should be appended.
111   */
112  @Override
113  public void toString(StringBuilder buffer)
114  {
115    buffer.append("TransactionIdControl(id=");
116    buffer.append(transactionId);
117    buffer.append(")");
118  }
119}