001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS
026 */
027package org.opends.server.controls;
028
029import static org.opends.messages.ProtocolMessages.*;
030import static org.opends.server.util.ServerConstants.*;
031import static org.opends.server.util.StaticUtils.getExceptionMessage;
032
033import java.io.IOException;
034
035import org.forgerock.i18n.LocalizableMessage;
036import org.forgerock.opendj.io.ASN1;
037import org.forgerock.opendj.io.ASN1Reader;
038import org.forgerock.opendj.io.ASN1Writer;
039import org.forgerock.opendj.ldap.ByteString;
040import org.forgerock.opendj.ldap.ResultCode;
041import org.opends.server.replication.common.MultiDomainServerState;
042import org.opends.server.types.Control;
043import org.opends.server.types.DirectoryException;
044
045/** This class implements the control used to browse the external changelog. */
046public class ExternalChangelogRequestControl
047       extends Control
048{
049  private MultiDomainServerState cookie;
050
051  /** ControlDecoder implementation to decode this control from a ByteString. */
052  private static final class Decoder
053      implements ControlDecoder<ExternalChangelogRequestControl>
054  {
055    @Override
056    public ExternalChangelogRequestControl decode(boolean isCritical, ByteString value) throws DirectoryException
057    {
058      return new ExternalChangelogRequestControl(isCritical, decodeCookie(value));
059    }
060
061    private MultiDomainServerState decodeCookie(ByteString value) throws DirectoryException
062    {
063      if (value == null)
064      {
065        return new MultiDomainServerState();
066      }
067
068      ASN1Reader reader = ASN1.getReader(value);
069      String mdssValue = null;
070      try
071      {
072        mdssValue = reader.readOctetStringAsString();
073        return new MultiDomainServerState(mdssValue);
074      }
075      catch (Exception e)
076      {
077        try
078        {
079          mdssValue = value.toString();
080          return new MultiDomainServerState(mdssValue);
081        }
082        catch (Exception e2)
083        {
084          LocalizableMessage message = ERR_CANNOT_DECODE_CONTROL_VALUE.get(
085              getOID() + " x=" + value.toHexString() + " v=" + mdssValue, getExceptionMessage(e));
086          throw new DirectoryException(ResultCode.PROTOCOL_ERROR, message, e);
087        }
088      }
089    }
090
091    @Override
092    public String getOID()
093    {
094      return OID_ECL_COOKIE_EXCHANGE_CONTROL;
095    }
096  }
097
098  /** The Control Decoder that can be used to decode this control. */
099  public static final ControlDecoder<ExternalChangelogRequestControl> DECODER = new Decoder();
100
101  /**
102   * Create a new external change log request control to contain the cookie.
103   * @param isCritical Specifies whether the control is critical.
104   * @param cookie Specifies the cookie value.
105   */
106  public ExternalChangelogRequestControl(boolean isCritical, MultiDomainServerState cookie)
107  {
108    super(OID_ECL_COOKIE_EXCHANGE_CONTROL, isCritical);
109    this.cookie = cookie;
110  }
111
112  /**
113   * Returns a copy of the cookie value.
114   *
115   * @return a copy of the cookie value
116   */
117  public MultiDomainServerState getCookie()
118  {
119    return new MultiDomainServerState(cookie);
120  }
121
122  @Override
123  public void toString(StringBuilder buffer)
124  {
125    buffer.append("ExternalChangelogRequestControl(cookie=");
126    this.cookie.toString(buffer);
127    buffer.append(")");
128  }
129
130  @Override
131  protected void writeValue(ASN1Writer writer) throws IOException
132  {
133    writer.writeStartSequence(ASN1.UNIVERSAL_OCTET_STRING_TYPE);
134    writer.writeOctetString(this.cookie.toString());
135    writer.writeEndSequence();
136  }
137}