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 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.protocols.ldap; 018 019 020import java.io.IOException; 021 022import org.forgerock.opendj.io.*; 023import org.forgerock.opendj.ldap.ByteString; 024 025import org.forgerock.i18n.slf4j.LocalizedLogger; 026import static org.opends.server.protocols.ldap.LDAPConstants.*; 027import static org.opends.server.util.ServerConstants.*; 028 029 030/** 031 * This class defines the structures and methods for an LDAP intermediate 032 * response protocol op, which is used to provide information to a client before 033 * the final response for an operation. 034 */ 035public class IntermediateResponseProtocolOp 036 extends ProtocolOp 037{ 038 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 039 040 /** The value for this intermediate response. */ 041 private ByteString value; 042 043 /** The OID for this intermediate response. */ 044 private String oid; 045 046 047 048 /** 049 * Creates a new intermediate protocol op with the specified OID and no 050 * value. 051 * 052 * @param oid The OID for this intermediate response. 053 */ 054 public IntermediateResponseProtocolOp(String oid) 055 { 056 this.oid = oid; 057 this.value = null; 058 } 059 060 061 062 /** 063 * Creates a new intermediate response protocol op with the specified OID and 064 * value. 065 * 066 * @param oid The OID for this intermediate response. 067 * @param value The value for this intermediate response. 068 */ 069 public IntermediateResponseProtocolOp(String oid, ByteString value) 070 { 071 this.oid = oid; 072 this.value = value; 073 } 074 075 076 077 /** 078 * Retrieves the OID for this intermediate response. 079 * 080 * @return The OID for this intermediate response, or <CODE>null</CODE> if 081 * there is no OID. 082 */ 083 public String getOID() 084 { 085 return oid; 086 } 087 088 089 /** 090 * Retrieves the value for this intermediate response. 091 * 092 * @return The value for this intermediate response, or <CODE>null</CODE> if 093 * there is no value. 094 */ 095 public ByteString getValue() 096 { 097 return value; 098 } 099 100 101 102 /** 103 * Retrieves the BER type for this protocol op. 104 * 105 * @return The BER type for this protocol op. 106 */ 107 @Override 108 public byte getType() 109 { 110 return OP_TYPE_INTERMEDIATE_RESPONSE; 111 } 112 113 114 115 /** 116 * Retrieves the name for this protocol op type. 117 * 118 * @return The name for this protocol op type. 119 */ 120 @Override 121 public String getProtocolOpName() 122 { 123 return "Intermediate Response"; 124 } 125 126 /** 127 * Writes this protocol op to an ASN.1 output stream. 128 * 129 * @param stream The ASN.1 output stream to write to. 130 * @throws IOException If a problem occurs while writing to the stream. 131 */ 132 @Override 133 public void write(ASN1Writer stream) throws IOException 134 { 135 stream.writeStartSequence(OP_TYPE_INTERMEDIATE_RESPONSE); 136 137 if (oid != null) 138 { 139 stream.writeOctetString(TYPE_INTERMEDIATE_RESPONSE_OID, oid); 140 } 141 142 if (value != null) 143 { 144 stream.writeOctetString(TYPE_INTERMEDIATE_RESPONSE_VALUE, value); 145 } 146 147 stream.writeEndSequence(); 148 } 149 150 151 152 /** 153 * Appends a string representation of this LDAP protocol op to the provided 154 * buffer. 155 * 156 * @param buffer The buffer to which the string should be appended. 157 */ 158 @Override 159 public void toString(StringBuilder buffer) 160 { 161 buffer.append("IntermediateResponse(oid=").append(oid); 162 if (value != null) 163 { 164 buffer.append(", value=").append(value); 165 } 166 buffer.append(")"); 167 } 168 169 170 171 /** 172 * Appends a multi-line string representation of this LDAP protocol op to the 173 * provided buffer. 174 * 175 * @param buffer The buffer to which the information should be appended. 176 * @param indent The number of spaces from the margin that the lines should 177 * be indented. 178 */ 179 @Override 180 public void toString(StringBuilder buffer, int indent) 181 { 182 StringBuilder indentBuf = new StringBuilder(indent); 183 for (int i=0 ; i < indent; i++) 184 { 185 indentBuf.append(' '); 186 } 187 188 buffer.append(indentBuf); 189 buffer.append("Intermediate Response"); 190 buffer.append(EOL); 191 192 if (oid != null) 193 { 194 buffer.append(indentBuf); 195 buffer.append(" OID: "); 196 buffer.append(oid); 197 buffer.append(EOL); 198 } 199 200 if (value != null) 201 { 202 buffer.append(indentBuf); 203 buffer.append(" Value:"); 204 buffer.append(EOL); 205 buffer.append(value.toHexPlusAsciiString(indent+4)); 206 } 207 } 208} 209