Index: snacc-provider/src/main/java/org/apache/ldap/common/berlib/snacc/ControlTransform.java =================================================================== --- snacc-provider/src/main/java/org/apache/ldap/common/berlib/snacc/ControlTransform.java (revision 349843) +++ snacc-provider/src/main/java/org/apache/ldap/common/berlib/snacc/ControlTransform.java (working copy) @@ -63,8 +63,8 @@ { l_control = ( org.apache.ldap.common.message.Control ) l_list.next() ; l_snaccControl = new Control() ; - l_snaccControl.controlType = l_control.getType().getBytes() ; - l_snaccControl.controlValue = l_control.getValue() ; + l_snaccControl.controlType = l_control.getID().getBytes() ; + l_snaccControl.controlValue = l_control.getEncodedValue() ; l_snaccControl.criticality = l_control.isCritical() ; l_snaccControls.add( l_snaccControl ) ; } @@ -101,8 +101,8 @@ } }; l_control.setCritical( l_snaccControl.criticality ) ; - l_control.setType( new String( l_snaccControl.controlType ) ) ; - l_control.setValue( l_snaccControl.controlValue ) ; + l_control.setID( new String( l_snaccControl.controlType ) ) ; + l_control.setEncodedValue( l_snaccControl.controlValue ) ; a_parent.add( l_control ) ; } } Index: common/src/test/org/apache/ldap/common/message/AbstractMessageTest.java =================================================================== --- common/src/test/org/apache/ldap/common/message/AbstractMessageTest.java (revision 349843) +++ common/src/test/org/apache/ldap/common/message/AbstractMessageTest.java (working copy) @@ -125,42 +125,20 @@ return false; } - public String getType() + public String getID() { return null; } - public void setType( String a_oid ) + public byte[] getEncodedValue() { - } - - public byte[] getValue() - { return new byte[0]; } - public void setValue( byte[] a_value ) - { - } - public boolean isCritical() { return false; } - - public void setCritical( boolean a_isCritical ) - { - } - - public byte[] getEncodedValue() - { - return null; - } - - public String getID() - { - return null; - } }); msg1 = new AbstractMessage( 5, MessageTypeEnum.BINDREQUEST ) { private static final long serialVersionUID = 1L;}; Index: common/src/test/org/apache/ldap/common/message/AbstractResultResponseTest.java =================================================================== --- common/src/test/org/apache/ldap/common/message/AbstractResultResponseTest.java (revision 349843) +++ common/src/test/org/apache/ldap/common/message/AbstractResultResponseTest.java (working copy) @@ -186,42 +186,21 @@ return false; } - public String getType() + public String getID() { return null; } - public void setType( String a_oid ) + public byte[] getEncodedValue() { - } - - public byte[] getValue() - { return new byte[0]; } - public void setValue( byte[] a_value ) - { - } - public boolean isCritical() { return false; } - public void setCritical( boolean a_isCritical ) - { - } - - public byte[] getEncodedValue() - { - return null; - } - - public String getID() - { - return null; - } }); msg1 = new AbstractResultResponse( 5, MessageTypeEnum.BINDREQUEST ) { private static final long serialVersionUID = 1L;}; Index: common/src/main/java/org/apache/ldap/common/message/ControlImpl.java =================================================================== --- common/src/main/java/org/apache/ldap/common/message/ControlImpl.java (revision 349843) +++ common/src/main/java/org/apache/ldap/common/message/ControlImpl.java (working copy) @@ -27,7 +27,7 @@ * @author Apache Directory Project * @version $Rev$ */ -public abstract class ControlImpl extends AbstractLockable implements Control +public class ControlImpl extends AbstractLockable implements Control { /** Unique object identifier for this control */ private String m_oid ; @@ -35,7 +35,6 @@ private byte [] m_value ; /** Flag for control criticality */ private boolean m_isCritical ; - private String id; // ------------------------------------------------------------------------ @@ -57,14 +56,48 @@ /** * Creates a non-root Lockable Control implementation whose state is - * overriden by a parent Lockable. + * overriden by a parent Lockable, then copies the content of the + * given LDAP control to this object. + * + * @param a_parent the overriding parent Lockable. + * @param src the source LDAP control whose elements will be + * copied to this object. */ + public ControlImpl( final Lockable a_parent, + final javax.naming.ldap.Control src ) + { + super( a_parent, false ) ; + m_oid = src.getID() ; + m_value = src.getEncodedValue() ; + m_isCritical = src.isCritical() ; + } + + + /** + * Creates a root Lockable Control implementation. + */ public ControlImpl() { super() ; } + /** + * Creates a root Lockable Control implementation, then copies the + * content of the given LDAP control to this object. + * + * @param src the source LDAP control whose elements will be + * copied to this object. + */ + public ControlImpl( final javax.naming.ldap.Control src ) + { + super() ; + m_oid = src.getID() ; + m_value = src.getEncodedValue() ; + m_isCritical = src.isCritical() ; + } + + // ------------------------------------------------------------------------ // Control Interface Method Implementations // ------------------------------------------------------------------------ @@ -102,7 +135,7 @@ * * @return the OID of this Control. */ - public String getType() + public String getID() { return m_oid ; } @@ -113,7 +146,7 @@ * * @param a_oid the OID of this Control. */ - public void setType( String a_oid ) + public void setID( String a_oid ) { lockCheck( "Attempt to alter OID of locked Control!" ) ; m_oid = a_oid ; @@ -126,7 +159,7 @@ * * @return ASN.1 BER encoded value as binary data. */ - public byte [] getValue() + public byte [] getEncodedValue() { return m_value ; } @@ -138,20 +171,10 @@ * * @param a_value ASN.1 BER encoded value as binary data. */ - public void setValue( byte [] a_value ) + public void setEncodedValue( byte [] a_value ) { lockCheck( "Attempt to alter encoded values of locked Control!" ) ; m_value = a_value ; } - - /** - * Retrieves the object identifier assigned for the LDAP control. - * - * @return The non-null object identifier string. - */ - public String getID() - { - return id; - } } Index: common/src/main/java/org/apache/ldap/common/message/Control.java =================================================================== --- common/src/main/java/org/apache/ldap/common/message/Control.java (revision 349843) +++ common/src/main/java/org/apache/ldap/common/message/Control.java (working copy) @@ -28,51 +28,4 @@ */ public interface Control extends Lockable, javax.naming.ldap.Control { - /** - * Gets the OID of the Control to identify the control type. - * - * @return the OID of this Control. - */ - String getType() ; - - /** - * Sets the OID of the Control to identify the control type. - * - * @param a_oid the OID of this Control. - */ - void setType( String a_oid ) ; - - /** - * Gets the ASN.1 BER encoded value of the control which would have its own - * custom ASN.1 defined structure based on the nature of the control. - * - * @return ASN.1 BER encoded value as binary data. - */ - byte [] getValue() ; - - /** - * Sets the ASN.1 BER encoded value of the control which would have its own - * custom ASN.1 defined structure based on the nature of the control. - * - * @param a_value ASN.1 BER encoded value as binary data. - */ - void setValue( byte [] a_value ) ; - - /** - * Determines whether or not this control is critical for the correct - * operation of a request or response message. The default for this value - * should be false. - * - * @return true if the control is critical false otherwise. - */ - boolean isCritical() ; - - /** - * Sets the criticil flag which determines whether or not this control is - * critical for the correct operation of a request or response message. The - * default for this value should be false. - * - * @param a_isCritical true if the control is critical false otherwise. - */ - void setCritical( boolean a_isCritical ) ; } Index: common/src/main/java/org/apache/ldap/common/message/AbstractMessage.java =================================================================== --- common/src/main/java/org/apache/ldap/common/message/AbstractMessage.java (revision 349843) +++ common/src/main/java/org/apache/ldap/common/message/AbstractMessage.java (working copy) @@ -98,7 +98,7 @@ throws MessageException { lockCheck( "Attempt to add control to locked message envelope!" ); - controls.put( control.getType(), control ); + controls.put( control.getID(), control ); } @@ -113,7 +113,7 @@ throws MessageException { lockCheck( "Attempt to remove control from locked message envelope!" ); - controls.remove( control.getType() ); + controls.remove( control.getID() ); } Index: apache2-provider/src/test/org/apache/asn1new/ldap/TwixProviderTest.java =================================================================== --- apache2-provider/src/test/org/apache/asn1new/ldap/TwixProviderTest.java (revision 0) +++ apache2-provider/src/test/org/apache/asn1new/ldap/TwixProviderTest.java (revision 0) @@ -0,0 +1,210 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.asn1new.ldap; + +import junit.framework.TestCase; +import junit.framework.Assert; +import org.apache.ldap.common.message.spi.Provider; +import org.apache.ldap.common.message.*; +import org.apache.asn1new.ldap.pojo.SearchResultDone; +import org.apache.asn1new.ldap.pojo.LdapMessage; +import org.apache.asn1new.ldap.pojo.Control; + +import java.io.ByteArrayInputStream; +import java.util.Arrays; +import java.util.Iterator; +import java.nio.ByteBuffer; + +/** + * @author Apache Directory Project + */ +public class TwixProviderTest extends TestCase +{ + public void testSearchResultDoneWithNoControl() + { + byte[] asn1BER = new byte[] + { + 0x30, 0x0c, + 0x02, 0x01, 0x06, // messageID + 0x65, 0x07, + 0x0a, 0x01, 0x00, // LDAPResult: success + 0x04, 0x00, // matchedDN + 0x04, 0x00, // errorMessage + }; + + Provider twixProvider = TwixProvider.getProvider(); + + // test the decoder + Object message = twixProvider.getDecoder( null ) + .decode( null, new ByteArrayInputStream(asn1BER) ); + Assert.assertTrue( message instanceof LdapMessage ); + LdapMessage ldapMessage = (LdapMessage) message; + Assert.assertEquals( 6, ldapMessage.getMessageId() ); + + SearchResultDone searchDoneMessage = ldapMessage.getSearchResultDone(); + Assert.assertNotNull( searchDoneMessage ); + Assert.assertEquals( 0, searchDoneMessage.getLdapResult().getResultCode() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getMatchedDN() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getErrorMessage() ); + + Assert.assertNull( ldapMessage.getControls() ); + + try + { + ldapMessage.addControl( null ); + } + catch (NullPointerException ex) + { + Assert.fail("Got null pointer exception when calling LdapMessage.addControl()"); + } + } + + public void testSearchResultDoneWithControls() + { + byte[] asn1BER = new byte[] + { + 0x30, 0x35, + 0x02, 0x01, 0x06, // messageID + 0x65, 0x07, + 0x0a, 0x01, 0x00, // LDAPResult: success + 0x04, 0x00, // matchedDN + 0x04, 0x00, // errorMessage + (byte)0xa0, 0x27, // controls + 0x30, 0x25, + 0x04, 0x16, + 0x31, 0x2e, 0x32, 0x2e, 0x38, 0x34, 0x30, 0x2e, 0x31, 0x31, + 0x33, 0x35, 0x35, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x33, + 0x31, 0x39, // control oid: 1.2.840.113556.1.4.319 + 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x00, 0x04, 0x04, 0x4d, 0x00, 0x00, 0x00 // control value + }; + + Provider twixProvider = TwixProvider.getProvider(); + + // test the decoder + Object message = twixProvider.getDecoder( null ) + .decode( null, new ByteArrayInputStream(asn1BER) ); + Assert.assertTrue( message instanceof LdapMessage ); + LdapMessage ldapMessage = (LdapMessage) message; + Assert.assertEquals( 6, ldapMessage.getMessageId() ); + + SearchResultDone searchDoneMessage = ldapMessage.getSearchResultDone(); + Assert.assertNotNull( searchDoneMessage ); + Assert.assertEquals( 0, searchDoneMessage.getLdapResult().getResultCode() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getMatchedDN() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getErrorMessage() ); + + Assert.assertEquals( 1, ldapMessage.getControls().size() ); + // this is a constant in Java 5 API + String pagedResultsControlOID = "1.2.840.113556.1.4.319"; + Control pagedResultsControl = ldapMessage.getControls( 0 ); + Assert.assertEquals( pagedResultsControlOID, pagedResultsControl.getID() ); + Assert.assertFalse( pagedResultsControl.isCritical() ); + Assert.assertNotNull( pagedResultsControl.getEncodedValue() ); + + // test the encoder + ByteBuffer buff = + twixProvider.getEncoder().encodeBlocking( ldapMessage ); + Assert.assertTrue( Arrays.equals( asn1BER, buff.array() ) ); + } + + public void testSearchRequestWithControls() + { + byte[] asn1BER = new byte[] + { + 0x30, 0x64, + 0x02, 0x01, 0x04, // messageID + 0x63, 0x33, + 0x04, 0x13, + 0x64, 0x63, 0x3d, 0x6d, 0x79, 0x2d, 0x64, 0x6f, 0x6d, 0x61, + 0x69, 0x6e, 0x2c, 0x64, 0x63, 0x3d, 0x63, 0x6f, 0x6d, // baseObject: dc=my-domain,dc=com + 0x0a, 0x01, 0x02, // scope: subtree + 0x0a, 0x01, 0x03, // derefAliases: derefAlways + 0x02, 0x01, 0x00, // sizeLimit: 0 + 0x02, 0x01, 0x00, // timeLimit: 0 + 0x01, 0x01, 0x00, // typesOnly: false + (byte)0x87, 0x0b, + 0x6f, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x43, 0x6c, 0x61, 0x73, 0x73, // filter: (objectClass=*) + 0x30, 0x00, + (byte)0xa0, 0x2a, // controls + 0x30, 0x28, + 0x04, 0x16, + 0x31, 0x2e, 0x32, 0x2e, 0x38, 0x34, 0x30, 0x2e, 0x31, 0x31, + 0x33, 0x35, 0x35, 0x36, 0x2e, 0x31, 0x2e, 0x34, 0x2e, 0x33, + 0x31, 0x39, // control oid: 1.2.840.113556.1.4.319 + 0x01, 0x01, (byte)0xff, // criticality: true + 0x04, 0x0b, + 0x30, 0x09, 0x02, 0x01, 0x02, 0x04, 0x04, 0x47, 0x00, 0x00, 0x00, // value: pageSize=2 + }; + + Provider twixProvider = TwixProvider.getProvider(); + Object message = twixProvider.getDecoder( null ) + .decode( null, new ByteArrayInputStream(asn1BER) ); + Assert.assertTrue( message instanceof LdapMessage ); + LdapMessage ldapMessage = (LdapMessage) message; + + // test the transformer + Message snickerLdapMessage = + twixProvider.getTransformer().transform( ldapMessage ); + Assert.assertEquals( 4, snickerLdapMessage.getMessageId() ); + Assert.assertEquals( 1, snickerLdapMessage.getControls().size() ); + Iterator controlIterator = + snickerLdapMessage.getControls().iterator(); + org.apache.ldap.common.message.Control snickersControl = + (org.apache.ldap.common.message.Control)controlIterator.next(); + Assert.assertTrue( snickersControl.getEncodedValue().length > 0 ); + } + + public void testTransformerTransformingSnickersMessage() + { + SearchResponseDoneImpl snickerMessage = new SearchResponseDoneImpl( 6 ); + LdapResultImpl ldapResult = new LdapResultImpl( null ); + ldapResult.setErrorMessage( "" ); + ldapResult.setMatchedDn( "" ); + ldapResult.setResultCode( ResultCodeEnum.SUCCESS ); + snickerMessage.setLdapResult( ldapResult ); + snickerMessage.add(new ControlImpl() + { + public String getID() + { + return "1.2"; + } + public byte[] getEncodedValue() + { + return null; + } + }); + + // test the transformer + Provider twixProvider = TwixProvider.getProvider(); + Object twixMessage = + twixProvider.getTransformer().transform( snickerMessage ); + Assert.assertTrue( twixMessage instanceof LdapMessage ); + LdapMessage ldapMessage = (LdapMessage) twixMessage; + Assert.assertEquals( 6, ldapMessage.getMessageId() ); + + SearchResultDone searchDoneMessage = ldapMessage.getSearchResultDone(); + Assert.assertNotNull( searchDoneMessage ); + Assert.assertEquals( 0, searchDoneMessage.getLdapResult().getResultCode() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getMatchedDN() ); + Assert.assertEquals( "", searchDoneMessage.getLdapResult().getErrorMessage() ); + + Assert.assertNotNull( ldapMessage.getControls() ); + Assert.assertEquals( 1, ldapMessage.getControls().size() ); + Control pagedResultsControl = ldapMessage.getControls( 0 ); + Assert.assertEquals( "1.2", pagedResultsControl.getID() ); + } +} Index: apache2-provider/src/test/org/apache/asn1new/ldap/codec/SearchRequestTest.java =================================================================== --- apache2-provider/src/test/org/apache/asn1new/ldap/codec/SearchRequestTest.java (revision 349843) +++ apache2-provider/src/test/org/apache/asn1new/ldap/codec/SearchRequestTest.java (working copy) @@ -1837,13 +1837,13 @@ // this is a constant in Java 5 API String pagedResultsControlOID = "1.2.840.113556.1.4.319"; Control pagedResultsControl = message.getControls( 0 ); - Assert.assertEquals( pagedResultsControlOID, pagedResultsControl.getControlType() ); - Assert.assertTrue( pagedResultsControl.getCriticality() ); + Assert.assertEquals( pagedResultsControlOID, pagedResultsControl.getID() ); + Assert.assertTrue( pagedResultsControl.isCritical() ); // this is a constant in Java 5 API String manageReferralControlOID = "2.16.840.1.113730.3.4.2"; Control manageReferralControl = message.getControls( 1 ); - Assert.assertEquals( manageReferralControlOID, manageReferralControl.getControlType() ); + Assert.assertEquals( manageReferralControlOID, manageReferralControl.getID() ); SearchRequest sr = message.getSearchRequest(); Assert.assertEquals( "dc=my-domain,dc=com", sr.getBaseObject() ); Index: apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/Control.java =================================================================== --- apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/Control.java (revision 349843) +++ apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/Control.java (working copy) @@ -33,7 +33,7 @@ * * @author Apache Directory Project */ -public class Control extends Asn1Object +public class Control extends Asn1Object implements javax.naming.ldap.Control { //~ Instance fields ---------------------------------------------------------------------------- @@ -44,7 +44,7 @@ private boolean criticality = false; /** Optionnal control value */ - private Object controlValue; + private byte[] controlValue; /** The control length */ private transient int controlLength; @@ -58,7 +58,7 @@ * * @return A string which represent the control type */ - public String getControlType() + public String getID() { return (controlType == null ? "" : controlType.toString() ); } @@ -68,7 +68,7 @@ * * @param controlType An OID to store */ - public void setControlType( LdapString controlType ) + public void setID( LdapString controlType ) { this.controlType = controlType; } @@ -78,20 +78,13 @@ * * @return The control value */ - public byte[] getControlValue() + public byte[] getEncodedValue() { if ( controlValue == null ) { return EMPTY_BYTES; } - else if ( controlValue instanceof String ) - { - return StringUtils.getBytesUtf8( (String)controlValue ); - } - else - { - return (byte[])controlValue; - } + return controlValue; } /** @@ -99,7 +92,7 @@ * * @param controlValue The control value to store */ - public void setControlValue( Object controlValue ) + public void setEncodedValue( byte[] controlValue ) { this.controlValue = controlValue; } @@ -109,7 +102,7 @@ * * @return true if the criticality flag is true. */ - public boolean getCriticality() + public boolean isCritical() { return criticality; } @@ -119,7 +112,7 @@ * * @param criticality The criticality value */ - public void setCriticality( boolean criticality ) + public void setCritical( boolean criticality ) { this.criticality = criticality; } @@ -155,7 +148,7 @@ // The control value, if any if (controlValue != null) { - controlLength += 1 + Length.getNbBytes( getControlValue().length ) + getControlValue().length; + controlLength += 1 + Length.getNbBytes( getEncodedValue().length ) + getEncodedValue().length; } return 1 + Length.getNbBytes( controlLength ) + controlLength; @@ -201,7 +194,7 @@ // The control value, if any if ( controlValue != null ) { - Value.encode( buffer, getControlValue() ); + Value.encode( buffer, getEncodedValue() ); } return buffer; @@ -220,15 +213,7 @@ if ( controlValue != null ) { - if ( controlValue instanceof String ) - { - sb.append(" Control value : '").append( controlValue ).append("'\n"); - } - else - { - sb.append(" Control value : '").append( StringUtils.dumpBytes( (byte[])controlValue ) ).append("'\n"); - } - + sb.append(" Control value : '").append( StringUtils.dumpBytes( (byte[])controlValue ) ).append("'\n"); } return sb.toString(); Index: apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/LdapMessage.java =================================================================== --- apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/LdapMessage.java (revision 349843) +++ apache2-provider/src/main/java/org/apache/asn1new/ldap/pojo/LdapMessage.java (working copy) @@ -78,6 +78,10 @@ * @param i The index of the Control Object to get * * @return The selected Control Object + * + * @throws IndexOutOfBoundsException If the index is out of range. + * @throws NullPointerException If the control list has not + * been initialized with either {@link #addControl} or {@link @initControl}. */ public Control getControls( int i ) { @@ -112,6 +116,8 @@ public void addControl( Control control ) { currentControl = control; + if (this.controls == null) + initControl(); this.controls.add( control ); } @@ -549,7 +555,7 @@ protocolOp.encode(bb); // Do the same thing for Controls, if any. - if (controls != null) + if (controls != null && !controls.isEmpty()) { // Encode the controls bb.put( (byte)LdapConstants.CONTROLS_TAG ); Index: apache2-provider/src/main/java/org/apache/asn1new/ldap/codec/grammar/LdapControlGrammar.java =================================================================== --- apache2-provider/src/main/java/org/apache/asn1new/ldap/codec/grammar/LdapControlGrammar.java (revision 349843) +++ apache2-provider/src/main/java/org/apache/asn1new/ldap/codec/grammar/LdapControlGrammar.java (working copy) @@ -206,7 +206,7 @@ try { - control.setControlType( new LdapString( tlv.getValue().getData() ) ); + control.setID( new LdapString( tlv.getValue().getData() ) ); } catch ( LdapStringEncodingException lsee ) { @@ -219,7 +219,7 @@ if ( log.isDebugEnabled() ) { - log.debug( "Control OID : " + control.getControlType() ); + log.debug( "Control OID : " + control.getID() ); } } }); @@ -265,7 +265,7 @@ try { - control.setCriticality( BooleanDecoder.parse( value) ); + control.setCritical( BooleanDecoder.parse( value) ); } catch ( BooleanDecoderException bde ) { @@ -277,7 +277,7 @@ if ( log.isDebugEnabled() ) { - log.debug( "Control criticality : " + control.getCriticality() ); + log.debug( "Control criticality : " + control.isCritical() ); } } }); @@ -323,16 +323,16 @@ // We have to handle the special case of a 0 length value if ( tlv.getLength().getLength() == 0 ) { - control.setControlValue( new byte[]{} ); + control.setEncodedValue( new byte[]{} ); } else { - control.setControlValue( value.getData() ); + control.setEncodedValue( value.getData() ); } if ( log.isDebugEnabled() ) { - log.debug( "Control value : " + StringUtils.dumpBytes( control.getControlValue() ) ); + log.debug( "Control value : " + StringUtils.dumpBytes( control.getEncodedValue() ) ); } } }); Index: apache2-provider/src/main/java/org/apache/asn1new/ldap/TwixTransformer.java =================================================================== --- apache2-provider/src/main/java/org/apache/asn1new/ldap/TwixTransformer.java (revision 349843) +++ apache2-provider/src/main/java/org/apache/asn1new/ldap/TwixTransformer.java (working copy) @@ -28,6 +28,7 @@ import javax.naming.directory.ModificationItem; import org.apache.asn1.codec.DecoderException; +import org.apache.asn1.codec.util.StringUtils; import org.apache.asn1new.Asn1Object; import org.apache.asn1new.ldap.codec.LdapConstants; import org.apache.asn1new.ldap.codec.primitives.LdapDN; @@ -683,27 +684,8 @@ { Control twixControl = (Control)controls.next(); - ControlImpl snickersControl = new ControlImpl( snickersMessage ) - { - // Just to avoid a compilation warning !!! - public static final long serialVersionUID = 1L; - - - public byte[] getEncodedValue() - { - return null; - } - }; - - // Twix : boolean criticality -> Snickers : boolean m_isCritical - snickersControl.setCritical( twixControl.getCriticality() ); + ControlImpl snickersControl = new ControlImpl( snickersMessage, twixControl ); - // Twix : OID controlType -> Snickers : String m_oid - snickersControl.setType( twixControl.getControlType() ); - - // Twix : OctetString controlValue -> Snickers : byte [] m_value - snickersControl.setValue( twixControl.getControlValue() ); - snickersMessage.add( snickersControl ); } } @@ -1084,8 +1066,8 @@ } // We also have to transform the controls... - //transformControls( twixMessage, msg ); - + transformControls( twixMessage, msg ); + if (log.isDebugEnabled()) { log.debug( "Transformed message : " + twixMessage ); @@ -1093,4 +1075,45 @@ return twixMessage; } + + /** + * Transforms the LDAP controls of a SnickersMessage and copies + * them to a TwixMessage. + * + * @param twixMessage The destination TwixMessage + * @param msg The source SnickersMessage + */ + private void transformControls(LdapMessage twixMessage, Message msg) + { + Collection controls = msg.getControls(); + if ( controls == null ) + return; + for ( Iterator iter = controls.iterator(); iter.hasNext(); ) + { + javax.naming.ldap.Control nextControl = + (javax.naming.ldap.Control)iter.next(); + + // Create a new Twix control + Control twixControl = new Control(); + + try + { + twixControl.setID( new LdapString( + StringUtils.getBytesUtf8(nextControl.getID()) ) ); + } + catch ( LdapStringEncodingException lsee ) + { + String errorMessage = "The control type " + + nextControl.getID() + " is invalid : " + lsee.getMessage(); + log.error( errorMessage, lsee ); + throw new IllegalArgumentException( errorMessage ); + } + + twixControl.setCritical( nextControl.isCritical() ); + twixControl.setEncodedValue( nextControl.getEncodedValue() ); + + // Add it to the TwixMessage + twixMessage.addControl( twixControl ); + } + } }