mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

neil_a_wilson
20.33.2007 dfb4f94fefa3ba20073b8fa2986b7e39d4f290a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Portions Copyright 2007 Sun Microsystems, Inc.
 */
package org.opends.server.protocols.internal;
 
 
 
import java.io.OutputStream;
import java.io.IOException;
import java.util.ArrayList;
 
import org.opends.messages.Message;
import org.opends.server.core.*;
import org.opends.server.protocols.asn1.ASN1Element;
import org.opends.server.protocols.ldap.*;
import org.opends.server.types.AuthenticationType;
import org.opends.server.types.Control;
import org.opends.server.types.SearchResultEntry;
import org.opends.server.types.SearchResultReference;
 
import static org.opends.messages.ProtocolMessages.*;
import static org.opends.server.protocols.ldap.LDAPConstants.*;
import static org.opends.server.util.ServerConstants.*;
 
 
 
/**
 * This class provides an implementation of a
 * {@code java.io.OutputStream} that can be used to facilitate
 * internal communication with the Directory Server.  On the backend,
 * data written to this output stream will be first decoded as an
 * ASN.1 element and then as an LDAP message.  That LDAP message will
 * be converted to an internal operation which will then be processed
 * and the result returned to the client via the input stream on the
 * other side of the associated internal LDAP socket.
 */
@org.opends.server.types.PublicAPI(
     stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
     mayInstantiate=false,
     mayExtend=false,
     mayInvoke=true)
public final class InternalLDAPOutputStream
       extends OutputStream
       implements InternalSearchListener
{
  // Indicates whether this stream has been closed.
  private boolean closed;
 
  // Indicates whether the type of the ASN.1 element is needed.
  private boolean needType;
 
  // The BER type for the ASN.1 element being read.
  private byte elementType;
 
  // The data for the ASN.1 element being read.
  private byte[] elementBytes;
 
  // The length bytes for the ASN.1 element being read.
  private byte[] lengthBytes;
 
  // The offset in the appropriate array at which we should begin
  // writing data.  This could either refer to the length or data
  // array, depending on the stage of the encoding process.
  private int arrayOffset;
 
  // The internal LDAP socket with which this output stream is
  // associated.
  private InternalLDAPSocket socket;
 
 
 
  /**
   * Creates a new instance of an internal LDAP output stream that is
   * associated with the provided internal LDAP socket.
   *
   * @param  socket  The internal LDAP socket that will be serviced by
   *                 this internal LDAP output stream.
   */
  public InternalLDAPOutputStream(InternalLDAPSocket socket)
  {
    this.socket = socket;
 
    closed = false;
 
    needType = true;
    elementType = 0x00;
    elementBytes = null;
    lengthBytes = null;
    arrayOffset = 0;
  }
 
 
 
  /**
   * Closes this output stream, its associated socket, and the
   * socket's associated input stream.
   */
  @Override()
  public void close()
  {
    socket.close();
  }
 
 
 
  /**
   * Closes this output stream through an internal mechanism that will
   * not cause an infinite recursion loop by trying to also close the
   * output stream.
   */
  @org.opends.server.types.PublicAPI(
       stability=org.opends.server.types.StabilityLevel.PRIVATE,
       mayInstantiate=false,
       mayExtend=false,
       mayInvoke=false)
  void closeInternal()
  {
    closed = true;
  }
 
 
 
  /**
   * Flushes this output stream and forces any buffered data to be
   * written out.  This will have no effect, since this output
   * stream implementation does not use buffering.
   */
  @Override()
  public void flush()
  {
    // No implementation is required.
  }
 
 
 
  /**
   * Writes the contents of the provided byte array to this output
   * stream.
   *
   * @param  b  The byte array to be written.
   *
   * @throws  IOException  If the output stream is closed, or if there
   *                       is a problem with the data being written.
   */
  @Override()
  public void write(byte[] b)
         throws IOException
  {
    write(b, 0, b.length);
  }
 
 
 
  /**
   * Writes the specified portion of the data in the provided byte
   * array to this output stream.  Any data written will be
   * accumulated until a complete ASN.1 element is available, at which
   * point it will be decoded as an LDAP message and converted to an
   * internal operation that will be processed.
   *
   * @param  b    The byte array containing the data to be read.
   * @param  off  The position in the array at which to start reading
   *              data.
   * @param  len  The number of bytes to read from the array.
   *
   * @throws  IOException  If the output stream is closed, or if there
   *                       is a problem with the data being written.
   */
  @Override()
  public synchronized void write(byte[] b, int off, int len)
         throws IOException
  {
    if (closed)
    {
      Message m = ERR_INTERNALOS_CLOSED.get();
      throw new IOException(m.toString());
    }
 
    if (len == 0)
    {
      return;
    }
 
 
    // See if we need to read the BER type.
    int position  = off;
    int remaining = len;
    if (needType)
    {
      elementType = b[position++];
      needType = false;
 
      if (--remaining <= 0)
      {
        return;
      }
    }
 
 
    // See if we need to read the first length byte.
    if ((lengthBytes == null) && (elementBytes == null))
    {
      int length = b[position++];
      if (length == (length & 0x7F))
      {
        // It's a single-byte length, so we can create the value
        // array.
        elementBytes = new byte[length];
      }
      else
      {
        // It's a multi-byte length, so we can create the length
        // array.
        lengthBytes = new byte[length & 0x7F];
      }
 
      arrayOffset = 0;
      if (--remaining <= 0)
      {
        return;
      }
    }
 
 
    // See if we need to continue reading part of a multi-byte length.
    if (lengthBytes != null)
    {
      // See if we have enough to read the full length.  If so, then
      // do it.  Otherwise, read what we can and return.
      int needed = lengthBytes.length - arrayOffset;
      if (remaining >= needed)
      {
        System.arraycopy(b, position, lengthBytes, arrayOffset,
                         needed);
        position += needed;
        remaining -= needed;
 
        int length = 0;
        for (byte lb : lengthBytes)
        {
          length <<= 8;
          length |= (lb & 0xFF);
        }
 
        elementBytes = new byte[length];
        lengthBytes = null;
        arrayOffset = 0;
        if (remaining <= 0)
        {
          return;
        }
      }
      else
      {
        System.arraycopy(b, position, lengthBytes, arrayOffset,
                         remaining);
        arrayOffset += remaining;
        return;
      }
    }
 
 
    // See if we need to read data for the element value.
    if (elementBytes != null)
    {
      // See if we have enough to read the full value.  If so, then
      // do it, create the element, and process it.  Otherwise, read
      // what we can and return.
      int needed = elementBytes.length - arrayOffset;
      if (remaining >= needed)
      {
        System.arraycopy(b, position, elementBytes, arrayOffset,
                         needed);
        position += needed;
        remaining -= needed;
        processElement(new ASN1Element(elementType, elementBytes));
 
        needType     = true;
        arrayOffset  = 0;
        lengthBytes  = null;
        elementBytes = null;
      }
      else
      {
        System.arraycopy(b, position, lengthBytes, arrayOffset,
                         remaining);
        arrayOffset += remaining;
        return;
      }
    }
 
 
    // If there is still more data available, then call this method
    // again to process it.
    if (remaining > 0)
    {
      write(b, position, remaining);
    }
  }
 
 
 
  /**
   * Writes a single byte of data to this output stream.  If the byte
   * written completes an ASN.1 element that was in progress, then it
   * will be decoded as an LDAP message and converted to an internal
   * operation that will be processed.  Otherwise, the data will be
   * accumulated until a complete element can be formed.
   *
   * @param  b The byte to be written.
   *
   * @throws  IOException  If the output stream is closed, or if there
   *                       is a problem with the data being written.
   */
  @Override()
  public synchronized void write(int b)
         throws IOException
  {
    if (closed)
    {
      Message m = ERR_INTERNALOS_CLOSED.get();
      throw new IOException(m.toString());
    }
 
    if (needType)
    {
      elementType = (byte) (b & 0xFF);
      needType = false;
      return;
    }
    else if (elementBytes != null)
    {
      // The byte should be part of the element value.
      elementBytes[arrayOffset++] = (byte) (b & 0xFF);
      if (arrayOffset == elementBytes.length)
      {
        // The element has been completed, so process it.
        processElement(new ASN1Element(elementType, elementBytes));
      }
 
      lengthBytes  = null;
      elementBytes = null;
      arrayOffset  = 0;
      needType     = true;
 
      return;
    }
    else if (lengthBytes != null)
    {
      // The byte should be part of a multi-byte length.
      lengthBytes[arrayOffset++] = (byte) (b & 0xFF);
      if (arrayOffset == lengthBytes.length)
      {
        int length = 0;
        for (int i=0; i < lengthBytes.length; i++)
        {
          length <<= 8;
          length |= (lengthBytes[i] & 0xFF);
        }
 
        elementBytes = new byte[length];
        lengthBytes  = null;
        arrayOffset   = 0;
      }
 
      return;
    }
    else
    {
      if ((b & 0x7F) == b)
      {
        // It's the complete length.
        elementBytes = new byte[b];
        lengthBytes  = null;
        arrayOffset = 0;
      }
      else
      {
        lengthBytes  = new byte[b & 0x7F];
        elementBytes = null;
        arrayOffset  = 0;
      }
 
      return;
    }
  }
 
 
 
  /**
   * Processes the provided ASN.1 element by decoding it as an LDAP
   * message, converting that to an internal operation, and sending
   * the appropriate response message(s) to the client through the
   * corresponding internal LDAP input stream.
   *
   * @param  element  The ASN.1 element to be processed.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       decode the provided ASN.1 element as an
   *                       LDAP message.
   */
  private void processElement(ASN1Element element)
          throws IOException
  {
    LDAPMessage message;
    try
    {
      message = LDAPMessage.decode(element.decodeAsSequence());
    }
    catch (Exception e)
    {
      throw new IOException(e.getMessage(), e);
    }
 
    switch (message.getProtocolOpType())
    {
      case OP_TYPE_ABANDON_REQUEST:
        // No action is required.
        return;
 
      case OP_TYPE_ADD_REQUEST:
        processAddOperation(message);
        break;
 
      case OP_TYPE_BIND_REQUEST:
        processBindOperation(message);
        break;
 
      case OP_TYPE_COMPARE_REQUEST:
        processCompareOperation(message);
        break;
 
 
      case OP_TYPE_DELETE_REQUEST:
        processDeleteOperation(message);
        break;
 
 
      case OP_TYPE_EXTENDED_REQUEST:
        processExtendedOperation(message);
        break;
 
 
      case OP_TYPE_MODIFY_REQUEST:
        processModifyOperation(message);
        break;
 
 
      case OP_TYPE_MODIFY_DN_REQUEST:
        processModifyDNOperation(message);
        break;
 
 
      case OP_TYPE_SEARCH_REQUEST:
        processSearchOperation(message);
        break;
 
 
      case OP_TYPE_UNBIND_REQUEST:
        socket.close();
        break;
 
 
      default:
        Message m = ERR_INTERNALOS_INVALID_REQUEST.get(
                         message.getProtocolElementName());
        throw new IOException(m.toString());
    }
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as an add
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processAddOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    AddRequestProtocolOp request = message.getAddRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    AddOperationBasis op =
         new AddOperationBasis(conn, conn.nextOperationID(),
                               messageID, requestControls,
                               request.getDN(),
                               request.getAttributes());
    op.run();
 
    AddResponseProtocolOp addResponse =
         new AddResponseProtocolOp(op.getResultCode().getIntValue(),
                                   op.getErrorMessage().toMessage(),
                                   op.getMatchedDN(),
                                   op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, addResponse, responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a bind
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processBindOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    BindRequestProtocolOp request =
         message.getBindRequestProtocolOp();
 
    if (request.getAuthenticationType() == AuthenticationType.SASL)
    {
      Message m = ERR_INTERNALOS_SASL_BIND_NOT_SUPPORTED.get();
      BindResponseProtocolOp bindResponse =
           new BindResponseProtocolOp(
                    LDAPResultCode.UNWILLING_TO_PERFORM, m);
      socket.getInputStream().addLDAPMessage(
           new LDAPMessage(messageID, bindResponse));
      return;
    }
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    BindOperationBasis op =
         new BindOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls,
                  String.valueOf(request.getProtocolVersion()),
                  request.getDN(), request.getSimplePassword());
    op.run();
 
    BindResponseProtocolOp bindResponse =
         new BindResponseProtocolOp(op.getResultCode().getIntValue(),
                                    op.getErrorMessage().toMessage(),
                                    op.getMatchedDN(),
                                    op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    if (bindResponse.getResultCode() == LDAPResultCode.SUCCESS)
    {
      socket.setConnection(new InternalClientConnection(
           op.getAuthenticationInfo()));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, bindResponse, responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a compare
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processCompareOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    CompareRequestProtocolOp request =
         message.getCompareRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    CompareOperationBasis op =
         new CompareOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getDN(),
                  request.getAttributeType(),
                  request.getAssertionValue());
    op.run();
 
    CompareResponseProtocolOp compareResponse =
         new CompareResponseProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, compareResponse,
                         responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a delete
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processDeleteOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    DeleteRequestProtocolOp request =
         message.getDeleteRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    DeleteOperationBasis op =
         new DeleteOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getDN());
    op.run();
 
    DeleteResponseProtocolOp deleteResponse =
         new DeleteResponseProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, deleteResponse,
                         responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as an extended
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processExtendedOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    ExtendedRequestProtocolOp request =
         message.getExtendedRequestProtocolOp();
    if (request.getOID().equals(OID_START_TLS_REQUEST))
    {
      Message m = ERR_INTERNALOS_STARTTLS_NOT_SUPPORTED.get();
      ExtendedResponseProtocolOp extendedResponse =
           new ExtendedResponseProtocolOp(
                    LDAPResultCode.UNWILLING_TO_PERFORM, m);
      socket.getInputStream().addLDAPMessage(
           new LDAPMessage(messageID, extendedResponse));
      return;
    }
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    ExtendedOperationBasis op =
         new ExtendedOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getOID(),
                  request.getValue());
    op.run();
 
    ExtendedResponseProtocolOp extendedResponse =
         new ExtendedResponseProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs(), op.getResponseOID(),
                  op.getResponseValue());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, extendedResponse,
                         responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a modify
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processModifyOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    ModifyRequestProtocolOp request =
         message.getModifyRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    ModifyOperationBasis op =
         new ModifyOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getDN(),
                  request.getModifications());
    op.run();
 
    ModifyResponseProtocolOp modifyResponse =
         new ModifyResponseProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, modifyResponse,
                         responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a modify DN
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processModifyDNOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    ModifyDNRequestProtocolOp request =
         message.getModifyDNRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    ModifyDNOperationBasis op =
         new ModifyDNOperationBasis(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getEntryDN(),
                  request.getNewRDN(), request.deleteOldRDN(),
                  request.getNewSuperior());
    op.run();
 
    ModifyDNResponseProtocolOp modifyDNResponse =
         new ModifyDNResponseProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, modifyDNResponse,
                         responseControls));
  }
 
 
 
  /**
   * Processes the content of the provided LDAP message as a search
   * operation and returns the appropriate result to the client.
   *
   * @param  message  The LDAP message containing the request to
   *                  process.
   *
   * @throws  IOException  If a problem occurs while attempting to
   *                       process the operation.
   */
  private void processSearchOperation(LDAPMessage message)
          throws IOException
  {
    int messageID = message.getMessageID();
    SearchRequestProtocolOp request =
         message.getSearchRequestProtocolOp();
 
    ArrayList<Control> requestControls = new ArrayList<Control>();
    if (message.getControls() != null)
    {
      for (LDAPControl c : message.getControls())
      {
        requestControls.add(c.getControl());
      }
    }
 
    InternalClientConnection conn = socket.getConnection();
    InternalSearchOperation op =
         new InternalSearchOperation(conn, conn.nextOperationID(),
                  messageID, requestControls, request.getBaseDN(),
                  request.getScope(), request.getDereferencePolicy(),
                  request.getSizeLimit(), request.getTimeLimit(),
                  request.getTypesOnly(), request.getFilter(),
                  request.getAttributes(), this);
    op.run();
 
    SearchResultDoneProtocolOp searchDone =
         new SearchResultDoneProtocolOp(
                  op.getResultCode().getIntValue(),
                  op.getErrorMessage().toMessage(),
                  op.getMatchedDN(),
                  op.getReferralURLs());
    ArrayList<LDAPControl> responseControls =
         new ArrayList<LDAPControl>();
    for (Control c : op.getResponseControls())
    {
      responseControls.add(new LDAPControl(c));
    }
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(messageID, searchDone, responseControls));
  }
 
 
 
  /**
   * Performs any processing necessary for the provided search result
   * entry.
   *
   * @param  searchOperation  The internal search operation being
   *                          processed.
   * @param  searchEntry      The matching search result entry to be
   *                          processed.
   */
  @org.opends.server.types.PublicAPI(
       stability=org.opends.server.types.StabilityLevel.PRIVATE,
       mayInstantiate=false,
       mayExtend=false,
       mayInvoke=false)
  public void handleInternalSearchEntry(
                   InternalSearchOperation searchOperation,
                   SearchResultEntry searchEntry)
  {
    ArrayList<LDAPControl> entryControls =
         new ArrayList<LDAPControl>();
    for (Control c : searchEntry.getControls())
    {
      entryControls.add(new LDAPControl(c));
    }
 
    SearchResultEntryProtocolOp entry =
         new SearchResultEntryProtocolOp(searchEntry);
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(searchOperation.getMessageID(), entry,
                         entryControls));
  }
 
 
 
  /**
   * Performs any processing necessary for the provided search result
   * reference.
   *
   * @param  searchOperation  The internal search operation being
   *                          processed.
   * @param  searchReference  The search result reference to be
   *                          processed.
   */
  @org.opends.server.types.PublicAPI(
       stability=org.opends.server.types.StabilityLevel.PRIVATE,
       mayInstantiate=false,
       mayExtend=false,
       mayInvoke=false)
  public void handleInternalSearchReference(
                   InternalSearchOperation searchOperation,
                   SearchResultReference searchReference)
  {
    ArrayList<LDAPControl> entryControls =
         new ArrayList<LDAPControl>();
    for (Control c : searchReference.getControls())
    {
      entryControls.add(new LDAPControl(c));
    }
 
    SearchResultReferenceProtocolOp reference =
         new SearchResultReferenceProtocolOp(searchReference);
 
    socket.getInputStream().addLDAPMessage(
         new LDAPMessage(searchOperation.getMessageID(), reference,
                         entryControls));
  }
 
 
 
  /**
   * Retrieves a string representation of this internal LDAP socket.
   *
   * @return  A string representation of this internal LDAP socket.
   */
  @Override()
  public String toString()
  {
    return "InternalLDAPOutputStream";
  }
}