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