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-2016 ForgeRock AS. 016 */ 017package org.opends.server.extensions; 018 019import org.forgerock.opendj.server.config.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 @Override 056 public void initializeExtendedOperationHandler( 057 GetConnectionIdExtendedOperationHandlerCfg config) 058 throws ConfigException, InitializationException 059 { 060 super.initializeExtendedOperationHandler(config); 061 } 062 063 @Override 064 public void processExtendedOperation(ExtendedOperation operation) 065 { 066 operation.setResponseOID(OID_GET_CONNECTION_ID_EXTOP); 067 operation.setResponseValue( 068 encodeResponseValue(operation.getConnectionID())); 069 operation.setResultCode(ResultCode.SUCCESS); 070 } 071 072 /** 073 * Encodes the provided connection ID in an octet string suitable for use as 074 * the value for this extended operation. 075 * 076 * @param connectionID The connection ID to be encoded. 077 * 078 * @return The ASN.1 octet string containing the encoded connection ID. 079 */ 080 public static ByteString encodeResponseValue(long connectionID) 081 { 082 ByteStringBuilder builder = new ByteStringBuilder(8); 083 ASN1Writer writer = ASN1.getWriter(builder); 084 085 try 086 { 087 writer.writeInteger(connectionID); 088 } 089 catch(Exception e) 090 { 091 logger.traceException(e); 092 } 093 094 return builder.toByteString(); 095 } 096 097 /** 098 * Decodes the provided ASN.1 octet string to extract the connection ID. 099 * 100 * @param responseValue The response value to be decoded. 101 * 102 * @return The connection ID decoded from the provided response value. 103 * 104 * @throws DecodeException If an error occurs while trying to decode the 105 * response value. 106 */ 107 public static long decodeResponseValue(ByteString responseValue) 108 throws DecodeException 109 { 110 ASN1Reader reader = ASN1.getReader(responseValue); 111 try 112 { 113 return reader.readInteger(); 114 } 115 catch(Exception e) 116 { 117 // TODO: DO something 118 return 0; 119 } 120 } 121 122 @Override 123 public String getExtendedOperationOID() 124 { 125 return OID_GET_CONNECTION_ID_EXTOP; 126 } 127 128 @Override 129 public String getExtendedOperationName() 130 { 131 return "Get Connection ID"; 132 } 133}