KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > proxy > model > LdapProxyThread


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.directory.ldapstudio.proxy.model;
21
22
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.ServerSocket JavaDoc;
28 import java.net.Socket JavaDoc;
29 import java.nio.ByteBuffer JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.naming.NamingException JavaDoc;
35 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
36
37 import org.apache.directory.shared.asn1.ber.Asn1Decoder;
38 import org.apache.directory.shared.asn1.ber.IAsn1Container;
39 import org.apache.directory.shared.asn1.codec.DecoderException;
40 import org.apache.directory.shared.asn1.util.Asn1StringUtils;
41 import org.apache.directory.shared.ldap.codec.LdapConstants;
42 import org.apache.directory.shared.ldap.codec.LdapDecoder;
43 import org.apache.directory.shared.ldap.codec.LdapMessage;
44 import org.apache.directory.shared.ldap.codec.LdapMessageContainer;
45
46
47 /**
48  * This class implements the thread for LDAP Proxy.
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class LdapProxyThread extends Thread JavaDoc
54 {
55     /** The LDAP proxy */
56     private LdapProxy ldapProxy;
57     
58     /** The proxy port */
59     private int localPort;
60
61     /** The LDAP Server hostname */
62     private String JavaDoc remoteHost;
63
64     /** The LDAP Server port */
65     private int remotePort;
66
67     /** The timeout */
68     private long timeout;
69
70     /** The client socket */
71     private Socket JavaDoc cSocket;
72
73     /** The last message ID */
74     private int lastMessageId = -1;
75
76
77     /**
78      * Creates a new instance of LdapProxyThread.
79      *
80      * @param localPort
81      * @param remoteHost
82      * @param remotePort
83      * @param timeout
84      * @param cSocket
85      */

86     public LdapProxyThread( LdapProxy ldapProxy, int localPort, String JavaDoc remoteHost, int remotePort, long timeout )
87     {
88         this.ldapProxy = ldapProxy;
89         this.localPort = localPort;
90         this.remoteHost = remoteHost;
91         this.remotePort = remotePort;
92         this.timeout = timeout;
93     }
94
95
96     /* (non-Javadoc)
97      * @see java.lang.Thread#run()
98      */

99     public void run()
100     {
101         try
102         {
103             ServerSocket JavaDoc sSocket = new ServerSocket JavaDoc( localPort );
104             cSocket = sSocket.accept();
105
106             if ( cSocket != null )
107             {
108                 InputStream JavaDoc clientIn = null;
109                 OutputStream JavaDoc clientOut = null;
110                 InputStream JavaDoc serverIn = null;
111                 OutputStream JavaDoc serverOut = null;
112                 Socket JavaDoc toServer = null;
113                 int r0 = -1;
114                 int r1 = -1;
115                 int ch = -1;
116                 int i = -1;
117                 long time0 = System.currentTimeMillis();
118                 long time1 = time0 + timeout;
119
120                 try
121                 {
122                     toServer = new Socket JavaDoc( remoteHost, remotePort );
123                     System.out.println( "open connection to:" + toServer + "(timeout=" + timeout + " ms)" );
124                     clientIn = cSocket.getInputStream();
125                     clientOut = new BufferedOutputStream JavaDoc( cSocket.getOutputStream() );
126                     serverIn = toServer.getInputStream();
127                     serverOut = new BufferedOutputStream JavaDoc( toServer.getOutputStream() );
128
129                     while ( r0 != 0 || r1 != 0 || ( time1 - time0 ) <= timeout )
130                     {
131                         r0 = clientIn.available();
132
133                         if ( r0 != 0 )
134                         {
135                             System.out.println( "" );
136                             // LdapProxy.setTextIn("<<<" + r0 + " bytes from client
137
// \n","red");
138
System.out.println( "" );
139                             System.out.println( "<<<" + r0 + " bytes from client" );
140                             System.out.println( "<<<" + r1 + " bytes from server" );
141                         }
142
143                         ByteBuffer JavaDoc bb = null;
144
145                         while ( ( r0 = clientIn.available() ) > 0 )
146                         {
147                             byte[] in = new byte[r0];
148                             int k = 0;
149
150                             for ( i = 0; i < r0; i++ )
151                             {
152                                 ch = clientIn.read();
153
154                                 if ( ch != -1 )
155                                 {
156                                     in[k++] = ( byte ) ch;
157                                     serverOut.write( ch );
158                                 }
159                                 else
160                                 {
161                                     System.out.println( "client stream closed" );
162                                     // LdapProxy.setTextIn("client stream closed
163
// \n","redbold");
164
}
165                             }
166
167                             bb = ByteBuffer.allocate( r0 );
168                             bb.put( in );
169                             bb.flip();
170
171                             while ( bb.hasRemaining() )
172                             {
173                                 decode( bb );
174                             }
175
176                             /*
177                              * LdapProxy.setTextIn("--->>>\n", "regular");
178                              * LdapProxy.setTextIn(result, "regular");
179                              * LdapProxy.setTextIn("----------------------------------\n",
180                              * "regular");
181                              */

182
183                             time0 = System.currentTimeMillis();
184                             serverOut.flush();
185                         }
186
187                         bb = null;
188
189                         while ( ( r1 = serverIn.available() ) > 0 )
190                         {
191                             System.out.println( "" );
192                             System.out.println( ">>>" + r1 + " bytes from server" );
193                             // LdapProxy.setTextOut(">>>" + r1 + " bytes from server
194
// \n","red");
195
System.out.println( "" );
196                             System.out.println( ">>>" + r1 + " bytes from server" );
197
198                             byte[] out = new byte[r1];
199                             int k = 0;
200
201                             for ( i = 0; i < r1; i++ )
202                             {
203                                 ch = serverIn.read();
204
205                                 if ( ch != -1 )
206                                 {
207                                     out[k++] = ( byte ) ch;
208                                 }
209                                 else
210                                 {
211                                     System.out.println( "server stream closed" );
212                                     // LdapProxy.setTextOut("server stream closed
213
// \n","redbold");
214
break;
215                                 }
216
217                                 clientOut.write( ch );
218                             }
219
220                             bb = ByteBuffer.allocate( r1 );
221                             bb.put( out );
222                             bb.flip();
223
224                             // while ( true )
225
// {
226
// try
227
// {
228
// String result = decode( bb );
229
// System.out.println( result );
230
// /*
231
// * LdapProxy.setTextOut("<<<---\n", "blue");
232
// * LdapProxy.setTextOut(result, "blue");
233
// * LdapProxy.setTextOut("----------------------------------\n",
234
// * "regular");
235
// */
236
// if ( bb.hasRemaining() == false )
237
// {
238
// break;
239
// }
240
// }
241
// catch ( DecoderException de )
242
// {
243
// StringBuffer result = new StringBuffer();
244
// result.append( de.getMessage() ).append( '\n' );
245
//
246
// byte[] tmp = new byte[out.length - bb.position()];
247
// System.arraycopy( out, bb.position(), tmp, 0, out.length - bb.position() );
248
// result.append( StringUtils.dumpBytes( tmp ) );
249
//
250
// System.out.println( result );
251
// /*
252
// * LdapProxy.setTextOut("<<<---\n", "regular");
253
// * LdapProxy.setTextOut(new String(tmp), "regular");
254
// * LdapProxy.setTextOut("----------------------------------\n",
255
// * "regular");
256
// */
257
// break;
258
// }
259
// }
260

261                             time0 = new Date JavaDoc().getTime();
262                             clientOut.flush();
263                         }
264                         if ( r0 == 0 && r1 == 0 )
265                         {
266                             time1 = new Date JavaDoc().getTime();
267                             Thread.sleep( 100 );
268                             // Proxy.display("waiting:"+(time1-time0)+" ms");
269
}
270                     }
271                 }
272                 catch ( Throwable JavaDoc t )
273                 {
274                     System.out.println( "i=" + i + " ch=" + ch );
275                     t.printStackTrace( System.err );
276                 }
277                 finally
278                 {
279                     try
280                     {
281                         clientIn.close();
282                         clientOut.close();
283                         serverIn.close();
284                         serverOut.close();
285                         cSocket.close();
286                         toServer.close();
287                         // LdapProxy.quit( time1 - time0 );
288
}
289                     catch ( Exception JavaDoc e )
290                     {
291                         e.printStackTrace( System.err );
292                     }
293                 }
294             }
295         }
296         catch ( IOException JavaDoc e1 )
297         {
298             // TODO Auto-generated catch block
299
e1.printStackTrace();
300         }
301     }
302     
303     private String JavaDoc decode( ByteBuffer JavaDoc buffer ) throws DecoderException, NamingException JavaDoc
304     {
305         int position = buffer.position();
306
307         DefaultMutableTreeNode JavaDoc mess;
308
309         // DefaultMutableTreeNode messTrue;
310
DefaultMutableTreeNode JavaDoc messTrue;
311
312         Asn1Decoder ldapDecoder = new LdapDecoder();
313
314         // Allocate a LdapMessageContainer Container
315
IAsn1Container ldapMessageContainer = new LdapMessageContainer();
316
317         // Decode the PDU
318
ldapDecoder.decode( buffer, ldapMessageContainer );
319         // Check that everything is OK
320
LdapMessage ldapmessage = ( (LdapMessageContainer) ldapMessageContainer ).getLdapMessage();
321         LdapMessageWithPDU message = new LdapMessageWithPDU();
322         
323
324         message.setLdapMessage( ldapmessage );
325         message.setMessageId( ldapmessage.getMessageId() );
326
327         // check the Id to verfy if it's a new Entry or not
328
if ( message.getMessageId() != lastMessageId )
329         {
330 // mess = new DefaultMutableTreeNode( transformToStringType( ldapmessage.getMessageType() ) + " [Id = "
331
// + ( (LdapMessage) ldapmessage ).getMessageId() + " ]" );
332
//
333
// messTrue = new DefaultMutableTreeNode( message );
334
// lastNode = mess;
335
// lastMessNode = messTrue;
336
// lastMessageId = message.getMessageId();
337
// // mainFrame.getTop().add(mess);
338
// mainFrame.getTreeModel().insertNodeInto( mess, mainFrame.getTop(), mainFrame.getTop().getChildCount() );
339
// mainFrame.getLdapMessageTree().add( messTrue );
340
// currentCount = 1;
341

342         }
343         else
344         {
345 // mess = lastNode;
346
// messTrue = lastMessNode;
347
// currentCount++;
348
}
349
350           String JavaDoc type = transformToStringType( ldapmessage.getMessageType() );
351
352         int pduLength = buffer.position() - position;
353         byte[] bytes = buffer.array();
354         byte[] newBytes = new byte[pduLength];
355         System.arraycopy(bytes, position, newBytes, 0, pduLength );
356
357         //TODO only one methode to Dump including buffer and position.
358
message.setDumpBytes( Asn1StringUtils.dumpBytes( newBytes ) );
359
360         ldapProxy.addReceivedLdapMessage( message );
361         
362         return message.getLdapMessage().toString();
363     }
364     
365     public String JavaDoc transformToStringType( int type )
366     {
367         String JavaDoc stringType;
368
369         switch ( type )
370         {
371             case LdapConstants.ABANDON_REQUEST :
372                 stringType = "ABANDON REQUEST";
373                 break;
374             case LdapConstants.ADD_REQUEST :
375                 stringType = "ADD REQUEST";
376                 break;
377             case LdapConstants.ADD_RESPONSE :
378                 stringType = "ADD RESPONSE";
379                 break;
380             case LdapConstants.BIND_REQUEST :
381                 stringType = "BIND REQUEST";
382                 break;
383             case LdapConstants.BIND_RESPONSE :
384                 stringType = "BIND RESPONSE";
385                 break;
386             case LdapConstants.COMPARE_REQUEST :
387                 stringType = "COMPARE REQUEST";
388                 break;
389             case LdapConstants.COMPARE_RESPONSE :
390                 stringType = "COMPARE RESPONSE";
391                 break;
392             case LdapConstants.DEL_REQUEST :
393                 stringType = "DEL REQUEST";
394                 break;
395             case LdapConstants.DEL_RESPONSE :
396                 stringType = "DEL RESPONSE";
397                 break;
398             case LdapConstants.EXTENDED_REQUEST :
399                 stringType = "EXTENDED REQUEST";
400                 break;
401             case LdapConstants.EXTENDED_RESPONSE :
402                 stringType = "EXTENDED RESPONSE";
403                 break;
404             case LdapConstants.MODIFYDN_REQUEST :
405                 stringType = "MODIFYDN REQUEST";
406                 break;
407             case LdapConstants.MODIFYDN_RESPONSE :
408                 stringType = "MODIFYDN RESPONSE";
409                 break;
410             case LdapConstants.MODIFY_REQUEST :
411                 stringType = "MODIFY REQUEST";
412                 break;
413             case LdapConstants.MODIFY_RESPONSE :
414                 stringType = "MODIFY RESPONSE";
415                 break;
416             case LdapConstants.SEARCH_REQUEST :
417                 stringType = "SEARCH REQUEST";
418                 break;
419             case LdapConstants.SEARCH_RESULT_DONE :
420                 stringType = "SEARCH RESULT DONE";
421                 break;
422             case LdapConstants.SEARCH_RESULT_ENTRY :
423                 stringType = "SEARCH RESULT ENTRY";
424                 break;
425             case LdapConstants.SEARCH_RESULT_REFERENCE :
426                 stringType = "SEARCH RESULT REFERENCE";
427                 break;
428             case LdapConstants.UNBIND_REQUEST :
429                 stringType = "UNBIND REQUEST";
430                 break;
431             case LdapConstants.UNKNOWN:
432                 stringType = "UNKNOWN";
433                 break;
434
435             default :
436                 stringType = "UNKNOWN";
437                 break;
438         }
439         
440         return stringType;
441     }
442 }
443
Popular Tags