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 2008-2009 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2015 ForgeRock AS.
016 */
017package org.opends.server.extensions;
018
019import org.opends.server.admin.std.server.
020            GetConnectionIdExtendedOperationHandlerCfg;
021import org.opends.server.api.ExtendedOperationHandler;
022import org.forgerock.opendj.config.server.ConfigException;
023import org.opends.server.core.ExtendedOperation;
024import org.forgerock.i18n.slf4j.LocalizedLogger;
025import org.forgerock.opendj.io.ASN1;
026import org.forgerock.opendj.ldap.DecodeException;
027import org.forgerock.opendj.io.ASN1Reader;
028import org.forgerock.opendj.io.ASN1Writer;
029import org.opends.server.types.*;
030import org.forgerock.opendj.ldap.ResultCode;
031import org.forgerock.opendj.ldap.ByteString;
032import org.forgerock.opendj.ldap.ByteStringBuilder;
033import static org.opends.server.util.ServerConstants.*;
034
035/**
036 * This class implements the "Get Connection ID" extended operation that can be
037 * used to get the connection ID of the associated client connection.
038 */
039public class GetConnectionIDExtendedOperation
040       extends ExtendedOperationHandler<
041                    GetConnectionIdExtendedOperationHandlerCfg>
042{
043  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
044
045  /**
046   * Create an instance of this "Get Connection ID" extended operation.  All
047   * initialization should be performed in the
048   * {@code initializeExtendedOperationHandler} method.
049   */
050  public GetConnectionIDExtendedOperation()
051  {
052    super();
053  }
054
055  /** {@inheritDoc} */
056  @Override
057  public void initializeExtendedOperationHandler(
058                   GetConnectionIdExtendedOperationHandlerCfg config)
059         throws ConfigException, InitializationException
060  {
061    super.initializeExtendedOperationHandler(config);
062  }
063
064  /** {@inheritDoc} */
065  @Override
066  public void processExtendedOperation(ExtendedOperation operation)
067  {
068    operation.setResponseOID(OID_GET_CONNECTION_ID_EXTOP);
069    operation.setResponseValue(
070         encodeResponseValue(operation.getConnectionID()));
071    operation.setResultCode(ResultCode.SUCCESS);
072  }
073
074
075
076  /**
077   * Encodes the provided connection ID in an octet string suitable for use as
078   * the value for this extended operation.
079   *
080   * @param  connectionID  The connection ID to be encoded.
081   *
082   * @return  The ASN.1 octet string containing the encoded connection ID.
083   */
084  public static ByteString encodeResponseValue(long connectionID)
085  {
086    ByteStringBuilder builder = new ByteStringBuilder(8);
087    ASN1Writer writer = ASN1.getWriter(builder);
088
089    try
090    {
091      writer.writeInteger(connectionID);
092    }
093    catch(Exception e)
094    {
095      logger.traceException(e);
096    }
097
098    return builder.toByteString();
099  }
100
101
102
103  /**
104   * Decodes the provided ASN.1 octet string to extract the connection ID.
105   *
106   * @param  responseValue  The response value to be decoded.
107   *
108   * @return  The connection ID decoded from the provided response value.
109   *
110   * @throws DecodeException  If an error occurs while trying to decode the
111   *                         response value.
112   */
113  public static long decodeResponseValue(ByteString responseValue)
114         throws DecodeException
115  {
116    ASN1Reader reader = ASN1.getReader(responseValue);
117    try
118    {
119      return reader.readInteger();
120    }
121    catch(Exception e)
122    {
123      // TODO: DO something
124      return 0;
125    }
126  }
127
128  /** {@inheritDoc} */
129  @Override
130  public String getExtendedOperationOID()
131  {
132    return OID_GET_CONNECTION_ID_EXTOP;
133  }
134
135  /** {@inheritDoc} */
136  @Override
137  public String getExtendedOperationName()
138  {
139    return "Get Connection ID";
140  }
141}