001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.api.ldap.extras.extended.ads_impl.whoAmI; 021 022 023import java.nio.ByteBuffer; 024 025import org.apache.directory.api.asn1.DecoderException; 026import org.apache.directory.api.asn1.EncoderException; 027import org.apache.directory.api.asn1.ber.tlv.BerValue; 028import org.apache.directory.api.asn1.ber.tlv.TLV; 029import org.apache.directory.api.i18n.I18n; 030import org.apache.directory.api.ldap.codec.api.ExtendedResponseDecorator; 031import org.apache.directory.api.ldap.codec.api.LdapApiService; 032import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponse; 033import org.apache.directory.api.ldap.extras.extended.whoAmI.WhoAmIResponseImpl; 034import org.apache.directory.api.ldap.model.name.Dn; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038 039/** 040 * A Decorator for WhoAmIResponse extended request. 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044public class WhoAmIResponseDecorator extends ExtendedResponseDecorator<WhoAmIResponse> 045 implements WhoAmIResponse 046{ 047 private static final Logger LOG = LoggerFactory.getLogger( WhoAmIResponseDecorator.class ); 048 049 private WhoAmIResponse whoAmIResponse; 050 051 052 /** 053 * Creates a new instance of WhoAmIResponseDecorator. 054 * 055 * @param codec The LDAP service instance 056 * @param decoratedMessage The decorated message 057 */ 058 public WhoAmIResponseDecorator( LdapApiService codec, WhoAmIResponse decoratedMessage ) 059 { 060 super( codec, decoratedMessage ); 061 whoAmIResponse = decoratedMessage; 062 } 063 064 065 /** 066 * {@inheritDoc} 067 */ 068 @Override 069 public void setResponseValue( byte[] responseValue ) 070 { 071 WhoAmIResponseDecoder decoder = new WhoAmIResponseDecoder(); 072 073 try 074 { 075 if ( responseValue != null ) 076 { 077 whoAmIResponse = decoder.decode( responseValue ); 078 079 this.responseValue = new byte[responseValue.length]; 080 System.arraycopy( responseValue, 0, this.responseValue, 0, responseValue.length ); 081 } 082 } 083 catch ( DecoderException e ) 084 { 085 LOG.error( I18n.err( I18n.ERR_04165 ), e ); 086 throw new RuntimeException( e ); 087 } 088 } 089 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public byte[] getResponseValue() 096 { 097 if ( responseValue == null ) 098 { 099 try 100 { 101 responseValue = encodeInternal().array(); 102 } 103 catch ( EncoderException e ) 104 { 105 LOG.error( I18n.err( I18n.ERR_04167 ), e ); 106 throw new RuntimeException( e ); 107 } 108 } 109 110 return responseValue; 111 } 112 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public byte[] getAuthzId() 119 { 120 return getDecorated().getAuthzId(); 121 } 122 123 124 /** 125 * {@inheritDoc} 126 */ 127 @Override 128 public void setAuthzId( byte[] authzId ) 129 { 130 ( ( WhoAmIResponseImpl ) getDecorated() ).setAuthzId( authzId ); 131 } 132 133 134 /** 135 * Set the userId 136 */ 137 /* no qualifier*/void setUserId( String userId ) 138 { 139 ( ( WhoAmIResponseImpl ) whoAmIResponse ).setUserId( userId ); 140 } 141 142 143 /** 144 * Set the DnId 145 */ 146 /* no qualifier*/void setDn( Dn dn ) 147 { 148 ( ( WhoAmIResponseImpl ) whoAmIResponse ).setDn( dn ); 149 } 150 151 152 /** 153 * {@inheritDoc} 154 */ 155 @Override 156 public boolean isDnAuthzId() 157 { 158 return whoAmIResponse.isDnAuthzId(); 159 } 160 161 162 /** 163 * {@inheritDoc} 164 */ 165 @Override 166 public boolean isUserAuthzId() 167 { 168 return whoAmIResponse.isUserAuthzId(); 169 } 170 171 172 /** 173 * {@inheritDoc} 174 */ 175 @Override 176 public String getAuthzIdString() 177 { 178 return whoAmIResponse.getAuthzIdString(); 179 } 180 181 182 /** 183 * {@inheritDoc} 184 */ 185 @Override 186 public String getUserId() 187 { 188 return whoAmIResponse.getUserId(); 189 } 190 191 192 /** 193 * {@inheritDoc} 194 */ 195 @Override 196 public Dn getDn() 197 { 198 return whoAmIResponse.getDn(); 199 } 200 201 202 /** 203 * Overload the parent's getResponseName method, as the WhoAmI response should not 204 * contain the responseName. 205 */ 206 @Override 207 public String getResponseName() 208 { 209 return null; 210 } 211 212 213 /** 214 * Compute the WhoAmIResponse extended operation length 215 * <pre> 216 * 0x04 L1 authzId 217 * </pre> 218 */ 219 /* no qualifier */int computeLengthInternal() 220 { 221 if ( whoAmIResponse.getAuthzId() != null ) 222 { 223 return 1 + TLV.getNbBytes( whoAmIResponse.getAuthzId().length ) 224 + whoAmIResponse.getAuthzId().length; 225 } 226 else 227 { 228 return 1 + 1; 229 } 230 } 231 232 233 /** 234 * Encodes the WhoAmIResponse extended operation. 235 * 236 * @return A ByteBuffer that contains the encoded PDU 237 * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong. 238 */ 239 /* no qualifier */ByteBuffer encodeInternal() throws EncoderException 240 { 241 ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() ); 242 243 BerValue.encode( bb, whoAmIResponse.getAuthzId() ); 244 245 return bb; 246 } 247}