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
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.extensions;
028    
029    
030    
031    import org.opends.server.admin.std.server.
032                GetConnectionIdExtendedOperationHandlerCfg;
033    import org.opends.server.api.ExtendedOperationHandler;
034    import org.opends.server.config.ConfigException;
035    import org.opends.server.core.DirectoryServer;
036    import org.opends.server.core.ExtendedOperation;
037    import org.opends.server.protocols.asn1.ASN1Exception;
038    import org.opends.server.protocols.asn1.ASN1Long;
039    import org.opends.server.protocols.asn1.ASN1OctetString;
040    import org.opends.server.types.InitializationException;
041    import org.opends.server.types.ResultCode;
042    
043    import static org.opends.messages.ExtensionMessages.*;
044    import static org.opends.server.util.ServerConstants.*;
045    
046    
047    /**
048     * This class implements the "Get Connection ID" extended operation that can be
049     * used to get the connection ID of the associated client connection.
050     */
051    public class GetConnectionIDExtendedOperation
052           extends ExtendedOperationHandler<
053                        GetConnectionIdExtendedOperationHandlerCfg>
054    {
055      /**
056       * Create an instance of this "Get Connection ID" extended operation.  All
057       * initialization should be performed in the
058       * {@code initializeExtendedOperationHandler} method.
059       */
060      public GetConnectionIDExtendedOperation()
061      {
062        super();
063      }
064    
065    
066    
067      /**
068       * {@inheritDoc}
069       */
070      public void initializeExtendedOperationHandler(
071                       GetConnectionIdExtendedOperationHandlerCfg config)
072             throws ConfigException, InitializationException
073      {
074        // No special configuration is required.
075    
076        DirectoryServer.registerSupportedExtension(OID_GET_CONNECTION_ID_EXTOP,
077                                                   this);
078    
079        registerControlsAndFeatures();
080      }
081    
082    
083    
084      /**
085       * {@inheritDoc}
086       */
087      public void finalizeExtendedOperationHandler()
088      {
089        DirectoryServer.deregisterSupportedExtension(OID_GET_CONNECTION_ID_EXTOP);
090    
091        deregisterControlsAndFeatures();
092      }
093    
094    
095    
096      /**
097       * {@inheritDoc}
098       */
099      public void processExtendedOperation(ExtendedOperation operation)
100      {
101        operation.setResponseOID(OID_GET_CONNECTION_ID_EXTOP);
102        operation.setResponseValue(
103             encodeResponseValue(operation.getConnectionID()));
104        operation.setResultCode(ResultCode.SUCCESS);
105      }
106    
107    
108    
109      /**
110       * Encodes the provided connection ID in an octet string suitable for use as
111       * the value for this extended operation.
112       *
113       * @param  connectionID  The connection ID to be encoded.
114       *
115       * @return  The ASN.1 octet string containing the encoded connection ID.
116       */
117      public static ASN1OctetString encodeResponseValue(long connectionID)
118      {
119        return new ASN1OctetString(new ASN1Long(connectionID).encode());
120      }
121    
122    
123    
124      /**
125       * Decodes the provided ASN.1 octet string to extract the connection ID.
126       *
127       * @param  responseValue  The response value to be decoded.
128       *
129       * @return  The connection ID decoded from the provided response value.
130       *
131       * @throws  ASN1Exception  If an error occurs while trying to decode the
132       *                         response value.
133       */
134      public static long decodeResponseValue(ASN1OctetString responseValue)
135             throws ASN1Exception
136      {
137        return ASN1Long.decodeAsLong(responseValue.value()).longValue();
138      }
139    }
140