KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > server > web > HTTPEndPoint


1 package com.quikj.server.web;
2
3 import com.quikj.server.framework.*;
4
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8
9 public class HTTPEndPoint extends AceThread implements EndPointInterface
10 {
11     // make sure that the following constants match with the applet counter-part
12
public static final String JavaDoc METHOD_PLUGIN = "SERVICE";
13     public static final String JavaDoc METHOD_PING = "PING";
14     public static final String JavaDoc PLUGIN_APP_ID = "Plugin-Id";
15     public static final String JavaDoc CORRELATION_ID = "Correlation-Id";
16     public static final String JavaDoc CONTENT_TYPE = "Content-Type";
17     public static final String JavaDoc CODE = "X-Encoding";
18     
19     public static final String JavaDoc PLAIN_TEXT = "text/plain";
20     
21     public static final long PING_TIMER_INTERVAL = 30 * 60 * 1000L; // 30 minutes
22

23     // the following constants are used by plugin apps to send a message to the client (sendPluginMessageToClent())
24
public static final int SEND_MESSAGE = AceInputSocketStreamMessage.READ_COMPLETED;
25     public static final int SEND_EOF = AceInputSocketStreamMessage.EOF_REACHED;
26     
27     private Random random = new Random((new Date()).getTime());
28     
29     public HTTPEndPoint(Socket socket)
30     throws IOException
31     {
32         super("HTTPEndPoint");
33         
34         clientSocket = socket;
35         
36         try
37         {
38             clientSocket.setTcpNoDelay(true);
39         }
40         catch (SocketException ex)
41         {
42             ; // ignore
43
}
44         
45         clientHost = socket.getInetAddress().getHostAddress();
46         
47         clientOutputStream = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
48         
49         
50         synchronized (clientIdLock)
51         {
52             sessionId = ++clientId;
53         }
54         
55         setName("HTTPEndPoint_" + sessionId);
56         
57         // System.out.println ("New connection from client received. Creating handler: "
58
// + getName());
59

60         synchronized (countLock)
61         {
62             identifier = HTTPApplicationServer.getInstance().getHostName()
63             + ":" + clientHost + ":" + (new Date()).getTime()
64             + ":" + sessionId;
65         }
66     }
67     
68     public void dispose()
69     {
70         // System.out.println (getName() + " handler being disposed");
71
// kill the thread by sending a signal
72
if (interruptWait(AceSignalMessage.SIGNAL_TERM, "Request to kill the thread received") == false)
73         {
74             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
75             getName()
76             + "HTTPEndPoint.dispose() -- Error occured while sending signal : "
77             + getErrorMessage());
78         }
79         
80         if (client != null)
81         {
82             // System.out.println (client.getName() + " being disposed");
83
client.dispose();
84             client = null;
85         }
86         
87         if (server != null)
88         {
89             // System.out.println (server.getName() + " being disposed");
90
server.dispose();
91             server = null;
92         }
93         
94         if (plugin != null)
95         {
96             plugin.connectionClosed();
97             plugin = null;
98         }
99         
100         if (pingTimerId != -1)
101         {
102             try
103             {
104                 AceTimer.Instance().cancelTimer(pingTimerId);
105                 pingTimerId = -1;
106             }
107             catch (IOException ex)
108             {
109                 ; // ignore
110
}
111         }
112         
113         super.dispose();
114     }
115     
116     public void run()
117     {
118         try
119         {
120             client = new AceInputSocketStream(CLIENT_CONNECTION,
121             "client_" + sessionId,
122             clientSocket,
123             "http");
124             client.start();
125             // System.out.println ("Created client: " + client.getName());
126
}
127         catch (Exception JavaDoc ex)
128         {
129             
130             // print error message
131
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
132             getName()
133             + "- HTTPEndPoint.run() -- Error occured while creating a thread from the client connection - "
134             + ex.getMessage());
135             dispose();
136             return;
137         }
138         
139         
140         try
141         {
142             pingTimerId = AceTimer.Instance().startTimer(PING_TIMER_INTERVAL, 0L);
143             if (pingTimerId < 0)
144             {
145                 AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
146                 getName()
147                 + "- HTTPEndPoint.run() -- Error occured while starting ping timer - "
148                 + getErrorMessage());
149             }
150         }
151         catch (IOException ex)
152         {
153             ; // ignore
154
}
155         
156         boolean to_cont = true;
157         while (to_cont == true)
158         {
159             AceMessageInterface message = waitMessage();
160             if (message == null)
161             {
162                 // print error message
163
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
164                 getName()
165                 + "- HTTPEndPoint.run() -- A null message was received while waiting for a message - "
166                 + getErrorMessage());
167                 
168                 break;
169             }
170             
171             if ((message instanceof AceInputSocketStreamMessage) == true)
172             {
173                 if (pingTimerId != -1) // if the timer was successfully started
174
{
175                     try
176                     {
177                         if (AceTimer.Instance().cancelTimer(pingTimerId) == false)
178                         {
179                             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
180                             getName()
181                             + "- HTTPEndPoint.run() -- Could not cancel ping timer - "
182                             + getErrorMessage());
183                         }
184                         else
185                         {
186                             pingTimerId = AceTimer.Instance().startTimer(PING_TIMER_INTERVAL,
187                             0L);
188                             if (pingTimerId < 0)
189                             {
190                                 AceLogger.Instance().log(AceLogger.WARNING,
191                                 AceLogger.SYSTEM_LOG,
192                                 getName()
193                                 + "- HTTPEndPoint.run() -- Error occured while starting ping timer - "
194                                 + getErrorMessage());
195                             }
196                         }
197                         
198                     }
199                     catch (IOException ex)
200                     {
201                     }
202                 }
203                 
204                 to_cont = processHTTPMessage((AceInputSocketStreamMessage)message);
205             }
206             else if ((message instanceof AceTimerMessage) == true)
207             {
208                 AceTimerMessage timer_message = (AceTimerMessage)message;
209                 
210                 int id = timer_message.getTimerId();
211                 
212                 if (id == pingTimerId)
213                 {
214                     // ping timer has expired
215
pingTimerId = -1;
216                     
217                     AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
218                     getName()
219                     + " - HTTPEndPoint.run() -- Closing connection because of inactivity");
220                     
221                     // treat it as if the end-point has closed connection
222
to_cont = false;
223                 }
224                 else // this timer must have been started by a plugin, pass the event
225
{
226                     if (plugin != null)
227                     {
228                         // send the message to the plugin
229
to_cont = plugin.eventReceived(message);
230                     }
231                     else
232                     {
233                         // print error message
234
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
235                         getName()
236                         + "- HTTPEndPoint.run() -- An unexpected timer message is received");
237                     }
238                 }
239             }
240             else if ((message instanceof AceSignalMessage) == true)
241             {
242                 // A signal message is received
243

244                 // print informational message
245
AceLogger.Instance().log(AceLogger.INFORMATIONAL, AceLogger.SYSTEM_LOG,
246                 getName()
247                 + " - HTTPEndPoint.run() -- A signal "
248                 + ((AceSignalMessage)message).getSignalId()
249                 + " is received : "
250                 + ((AceSignalMessage)message).getMessage());
251                 
252                 break;
253             }
254             else
255             {
256                 if (plugin != null)
257                 {
258                     // send the message to the plugin
259
to_cont = plugin.eventReceived(message);
260                 }
261                 else
262                 {
263                     // print error message
264
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
265                     getName()
266                     + "- HTTPEndPoint.run() -- An unexpected event is received : "
267                     + message.messageType());
268                 }
269             }
270         } // forever
271

272         // do the necessary clean-up
273
dispose();
274     }
275     
276     private String JavaDoc generateRandomString()
277     {
278         char[] c = new char[10];
279         
280         for (int i = 0; i < 10; i++)
281         {
282             int x = random.nextInt() % 26;
283             c[i] = Integer.toString(Character.getNumericValue('a') + x, 10).charAt(0);
284         }
285         
286         return new String JavaDoc(c);
287     }
288     
289     //*********************************************************************************
290
// plugin methods
291
//*********************************************************************************
292
public boolean sendRequestMessageToClient(int request_id,
293     String JavaDoc content_type,
294     String JavaDoc body,
295     boolean immediate)
296     {
297         AceHTTPMessage message = new AceHTTPMessage(METHOD_PLUGIN,
298         "/",
299         false,
300         "1.1");
301         message.addHeader(PLUGIN_APP_ID, (new Integer JavaDoc(pluginApplicationId)).toString());
302         message.addHeader(CORRELATION_ID, (new Integer JavaDoc(request_id)).toString());
303         
304         if (content_type != null)
305         {
306             message.addHeader(CONTENT_TYPE, content_type);
307         }
308         
309         if (body != null)
310         {
311             if (body.length() > 0)
312             {
313                 String JavaDoc code = generateRandomString();
314                 
315                 message.addHeader(CODE,
316                 (new DESEncryption()).encrypt(code,
317                 SysParam.getAuth2(getClass())));
318                 
319                 // encode the body with the code
320
String JavaDoc encoded = (new DESEncryption()).encrypt(body, code);
321                 
322                 message.addBody(encoded);
323             }
324         }
325         
326         if (immediate == true)
327         {
328             return sendMessageToClient(message);
329         }
330         else
331         {
332             // send the message to self
333
return sendMessage(new AceInputSocketStreamMessage(null,
334             message,
335             AceInputSocketStreamMessage.READ_COMPLETED,
336             PLUGIN_CONNECTION));
337         }
338     }
339     
340     
341     public boolean sendRequestMessageToClient(int request_id,
342     String JavaDoc content_type,
343     String JavaDoc body)
344     {
345         return sendRequestMessageToClient(request_id, content_type, body, false);
346     }
347     
348     
349     public boolean sendResponseMessageToClient(int request_id, int status, String JavaDoc reason,
350     String JavaDoc content_type, String JavaDoc body,
351     boolean immediate)
352     {
353         AceHTTPMessage message = new AceHTTPMessage("1.1",
354         (new Integer JavaDoc(status)).toString(),
355         reason);
356         
357         message.addHeader(PLUGIN_APP_ID, (new Integer JavaDoc(pluginApplicationId)).toString());
358         message.addHeader(CORRELATION_ID, (new Integer JavaDoc(request_id)).toString());
359         
360         if (content_type != null)
361         {
362             message.addHeader(CONTENT_TYPE, content_type);
363         }
364         
365         if (body != null)
366         {
367             if (body.length() > 0)
368             {
369                 String JavaDoc code = generateRandomString();
370                 
371                 message.addHeader(CODE,
372                 (new DESEncryption()).encrypt(code,
373                 SysParam.getAuth2(getClass())));
374                 
375                 // encode the body with the code
376
String JavaDoc encoded = (new DESEncryption()).encrypt(body, code);
377                 
378                 message.addBody(encoded);
379             }
380         }
381         
382         if (immediate == false)
383         {
384             // send the message to self
385
return sendMessage(new AceInputSocketStreamMessage(null,
386             message,
387             AceInputSocketStreamMessage.READ_COMPLETED,
388             PLUGIN_CONNECTION));
389         }
390         else
391         {
392             return sendMessageToClient(message);
393         }
394     }
395     
396     public boolean sendResponseMessageToClient(int request_id, int status, String JavaDoc reason,
397     String JavaDoc content_type, String JavaDoc body)
398     {
399         return sendResponseMessageToClient(request_id, status, reason,
400         content_type, body, false);
401     }
402     
403     
404     public boolean closeClientConnection()
405     {
406         AceHTTPMessage message = new AceHTTPMessage(null, null, false, null);
407         
408         // send the message to self
409
return sendMessage(new AceInputSocketStreamMessage(null,
410         message,
411         AceInputSocketStreamMessage.EOF_REACHED,
412         PLUGIN_CONNECTION));
413     }
414     
415     public boolean sendEvent(AceMessageInterface message)
416     {
417         return sendMessage(message);
418     }
419     
420     //*********************************************************************************
421
// private methods
422
//*********************************************************************************
423
private boolean processHTTPMessage(AceInputSocketStreamMessage message)
424     {
425         boolean to_cont = true;
426         boolean from_client = false;
427         
428         // check if the message from client, server or plugin application side
429
int from = (int)message.getUserParm();
430         
431         // check the status
432
int status = message.getStatus();
433         
434         // System.out.println ("Message from "
435
// + from
436
// + " received with status = "
437
// + status);
438

439         
440         if ((status == AceInputSocketStreamMessage.READ_COMPLETED) ||
441         (status == AceInputSocketStreamMessage.EOF_REACHED))
442         {
443             // if the message has something inside it, then process it
444
if ((message.getHTTPMethod() != null) || (message.getHTTPStatus() != null))
445             {
446                 AceHTTPMessage parsed_msg = new AceHTTPMessage(message.isHTTPRequest(),
447                 message.isSimpleRequest(),
448                 message.getHTTPMethod(),
449                 message.getURL(),
450                 message.getHTTPVersion(),
451                 message.getHTTPStatus(),
452                 message.getHTTPReasonPhrase(),
453                 message.getHTTPHeaders(),
454                 message.getHTTPBody(),
455                 0,
456                 message.getHTTPBodyLength());
457                 
458                 
459                 switch (from)
460                 {
461                     case CLIENT_CONNECTION:
462                         //System.out.println (getName() + ": " + parsed_msg.formatMessage());
463
if (server != null) // if there is a server
464
{
465                             // send the message to the server
466
to_cont = sendMessageToServer(parsed_msg);
467                         }
468                         else if (plugin != null) // if there is a plugin associated with this client
469
{
470                             boolean ping = false;
471                             if (parsed_msg.isHTTPRequest() == true)
472                             {
473                                 if (parsed_msg.getRequestMethod().equalsIgnoreCase(METHOD_PING)
474                                 == true)
475                                 {
476 // System.out.println ("PING received");
477
ping = true;
478                                     to_cont = true;
479                                 }
480                             }
481                             
482                             if (ping == false)
483                             {
484                                 // make sure that the message is properly formatted
485
to_cont = verifyPluginMessage(parsed_msg);
486                                 
487                                 if (to_cont == true)
488                                 {
489                                     // send the message to the plugin
490
to_cont = sendMessageToPlugin(parsed_msg);
491                                 }
492                             }
493                         }
494                         else // this is the first message from the client
495
{
496                             
497                             if (parsed_msg.isHTTPRequest() == true)
498                             {
499                                 // if the message is for a plugin
500
if (parsed_msg.getRequestMethod().equalsIgnoreCase(METHOD_PLUGIN)
501                                 == true)
502                                 {
503                                     to_cont = connectToPlugin(parsed_msg);
504                                     
505                                     if (to_cont == true)
506                                     {
507                                         to_cont = sendMessageToPlugin(parsed_msg);
508                                     }
509                                 }
510                                 else if (parsed_msg.getRequestMethod().equalsIgnoreCase(METHOD_PING)
511                                 == true)
512                                 {
513                                     to_cont = true; // will never come here, just in case
514
}
515                                 else // must be a message for the server
516
{
517                                     to_cont = connectToServer(parsed_msg);
518                                     
519                                     if (to_cont == true)
520                                     {
521                                         to_cont = sendMessageToServer(parsed_msg);
522                                     }
523                                 }
524                             }
525                             else
526                             {
527                                 // print log message about malformed message
528
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
529                                 getName()
530                                 + "- HTTPEndPoint.processHTTPMessage() -- The first HTTP message received from the client side is not a request message");
531                                 
532                                 to_cont = false;
533                             }
534                         }
535                         break;
536                         
537                     case SERVER_CONNECTION:
538                         to_cont = sendMessageToClient(parsed_msg);
539                         break;
540                         
541                     case PLUGIN_CONNECTION:
542                         to_cont = sendMessageToClient(parsed_msg);
543                         break;
544                         
545                     default:
546                         // print error message
547
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
548                         getName()
549                         + "- HTTPEndPoint.processHTTPMessage() -- An HTTP message is received from an unknown entity: "
550                         + from);
551                         to_cont = false;
552                         break;
553                 }
554             }
555             
556             // if a side has closed connection, get out
557
if (status == AceInputSocketStreamMessage.EOF_REACHED)
558             {
559                 to_cont = false;
560             }
561         }
562         else
563         {
564             // print error message
565
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
566             getName()
567             + "- HTTPEndPoint.processHTTPMessage() -- An HTTP message is received with status "
568             + status);
569             
570             
571             to_cont = false;
572         }
573         
574         return to_cont;
575     }
576     
577     private boolean sendMessageToServer(AceHTTPMessage parsed_msg)
578     {
579         boolean to_cont = true;
580         // System.out.println ("Sending message to the server...");
581
String JavaDoc msg_to_send = parsed_msg.formatMessage();
582         
583         try
584         {
585             if (msg_to_send != null)
586             {
587                 // System.out.println ("Sending message to server"
588
// + '\n'
589
// + "-----"
590
// + msg_to_send
591
// + "----");
592

593                 serverOutputStream.write(msg_to_send, 0, msg_to_send.length());
594                 serverOutputStream.flush();
595             }
596         }
597         catch (IOException ex1)
598         {
599             // print error message
600
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
601             getName()
602             + "- HTTPEndPoint.sendMessageToServer() -- IO error while processing message - "
603             + ex1.getMessage());
604             to_cont = false;
605         }
606         return to_cont;
607     }
608     
609     private synchronized boolean sendMessageToClient(AceHTTPMessage parsed_msg)
610     {
611         boolean to_cont = true;
612         
613         try
614         {
615             String JavaDoc msg_to_send = parsed_msg.formatMessage();
616             
617             if (msg_to_send != null)
618             {
619                 // System.out.println ("Sending message to client"
620
// + '\n'
621
// + "-----"
622
// + msg_to_send
623
// + "----");
624

625                 clientOutputStream.write(msg_to_send, 0, msg_to_send.length());
626                 clientOutputStream.flush();
627             }
628         }
629         catch (IOException ex1)
630         {
631             // print error message
632
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
633             getName()
634             + "- HTTPEndPoint.sendMessageToClient() -- IO error while processing message - "
635             + ex1.getMessage());
636             to_cont = false;
637         }
638         return to_cont;
639     }
640     
641     private boolean connectToPlugin(AceHTTPMessage parsed_msg)
642     {
643         boolean to_cont = true;
644         
645         String JavaDoc appl_id_s = parsed_msg.findHeaderField(PLUGIN_APP_ID);
646         if (appl_id_s == null)
647         {
648             // the application id was not specified
649

650             // print error message
651
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
652             getName()
653             + "- HTTPEndPoint.connectToPlugin() -- Receivedmessage does not contain the header field: "
654             + PLUGIN_APP_ID);
655             
656             to_cont = false;
657         }
658         
659         if (to_cont == true)
660         {
661             try
662             {
663                 pluginApplicationId = Integer.parseInt(appl_id_s);
664             }
665             catch (NumberFormatException JavaDoc ex)
666             {
667                 AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
668                 getName()
669                 + "- HTTPEndPoint.connectToPlugin() -- Received message does not contain a numeric plugin identifier: "
670                 + appl_id_s);
671                 to_cont = false;
672             }
673         }
674         
675         if (to_cont == true)
676         {
677             plugin = PluginAppList.Instance().newInstance(pluginApplicationId);
678             
679             if (plugin == null)
680             {
681                 // the plugin was not found
682
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
683                 getName()
684                 + "- HTTPEndPoint.connectToPlugin() -- Received message's request for plugin application: "
685                 + appl_id_s
686                 + " could not be instantiated: "
687                 + PluginAppList.Instance().getErrorMessage());
688                 
689                 // format a response and send it to the client
690
AceHTTPMessage msg_to_send = new AceHTTPMessage("1.1", "404",
691                 "Plugin application: "
692                 + appl_id_s
693                 + " not found");
694                 
695                 msg_to_send.addHeader(PLUGIN_APP_ID, appl_id_s);
696                 sendMessageToClient(msg_to_send);
697                 
698                 to_cont = false;
699                 
700             }
701         }
702         
703         if (to_cont == true)
704         {
705             // now that we have created a plugin, inform it of the new connection
706
to_cont = plugin.newConnection(clientHost, this);
707         }
708         
709         return to_cont;
710     }
711     
712     public boolean verifyPluginMessage(AceHTTPMessage parsed_msg)
713     {
714         boolean to_cont = true;
715         
716         // if the message is a request message
717
if (parsed_msg.isHTTPRequest() == true)
718         {
719             // make sure that the method matches
720
if (parsed_msg.getRequestMethod().equalsIgnoreCase(METHOD_PLUGIN) == false)
721             {
722                 to_cont = false;
723             }
724         }
725         else // response message
726
{
727             String JavaDoc status_s = parsed_msg.getHTTPStatus();
728             if (status_s != null)
729             {
730                 try
731                 {
732                     int status = Integer.parseInt(status_s);
733                 }
734                 catch (NumberFormatException JavaDoc ex)
735                 {
736                     to_cont = false;
737                 }
738             }
739             else
740             {
741                 to_cont = false;
742             }
743         }
744         
745         String JavaDoc appl_id_s = null;
746         if (to_cont == true)
747         {
748             appl_id_s = parsed_msg.findHeaderField(PLUGIN_APP_ID);
749             if (appl_id_s == null)
750             {
751                 to_cont = false;
752             }
753         }
754         
755         if (to_cont == true)
756         {
757             try
758             {
759                 int appl_id = Integer.parseInt(appl_id_s);
760                 
761                 if (appl_id != pluginApplicationId)
762                 {
763                     to_cont = false;
764                 }
765             }
766             catch (NumberFormatException JavaDoc ex)
767             {
768                 to_cont = false;
769             }
770         }
771         
772         String JavaDoc crl_id_s = null;
773         if (to_cont == true)
774         {
775             crl_id_s = parsed_msg.findHeaderField(CORRELATION_ID);
776             if (crl_id_s == null)
777             {
778                 to_cont = false;
779             }
780         }
781         
782         if (to_cont == true)
783         {
784             try
785             {
786                 int crl_id = Integer.parseInt(crl_id_s);
787             }
788             catch (NumberFormatException JavaDoc ex)
789             {
790                 to_cont = false;
791             }
792         }
793         
794         return to_cont;
795     }
796     
797     private boolean sendMessageToPlugin(AceHTTPMessage parsed_msg)
798     {
799         boolean to_cont = true;
800         
801         int crl_id = Integer.parseInt(parsed_msg.findHeaderField(CORRELATION_ID));
802         
803         String JavaDoc content_type = parsed_msg.findHeaderField(CONTENT_TYPE);
804         if (content_type == null)
805         {
806             content_type = PLAIN_TEXT;
807         }
808         
809         if (parsed_msg.isHTTPRequest() == true)
810         {
811             String JavaDoc body = parsed_msg.getBody();
812             if (body != null)
813             {
814                 if (body.length() > 0)
815                 {
816                     String JavaDoc code = parsed_msg.findHeaderField(CODE);
817                     if (code != null)
818                     {
819                         String JavaDoc dec = (new DESEncryption()).decrypt(code,
820                         SysParam.getAuth2(getClass()));
821                         
822                         if (dec != null)
823                         {
824                             // use the code to decrypt the content
825
String JavaDoc dec_body = (new DESEncryption()).decrypt(body, dec);
826                             if (dec_body != null)
827                             {
828                                 body = dec_body;
829                             }
830                         }
831                     }
832                 }
833             }
834             
835             to_cont = plugin.requestReceived(crl_id, content_type, body);
836         }
837         else // response message
838
{
839             int status = Integer.parseInt(parsed_msg.getHTTPStatus());
840             String JavaDoc reason = parsed_msg.getHTTPReason();
841             
842             String JavaDoc body = parsed_msg.getBody();
843             if (body != null)
844             {
845                 if (body.length() > 0)
846                 {
847                     String JavaDoc code = parsed_msg.findHeaderField(CODE);
848                     if (code != null)
849                     {
850                         String JavaDoc dec = (new DESEncryption()).decrypt(code,
851                         SysParam.getAuth2(getClass()));
852                         
853                         if (dec != null)
854                         {
855                             // use the code to decrypt the content
856
String JavaDoc dec_body = (new DESEncryption()).decrypt(body, dec);
857                             if (dec_body != null)
858                             {
859                                 body = dec_body;
860                             }
861                         }
862                     }
863                 }
864             }
865             
866             to_cont = plugin.responseReceived(crl_id, status, reason,
867             content_type, body);
868         }
869         
870         return to_cont;
871     }
872     
873     
874     private boolean connectToServer(AceHTTPMessage parsed_msg)
875     {
876         boolean to_cont = true;
877         
878         // has to be a request, check anyway
879
String JavaDoc host = null;
880         int port = 0;
881         String JavaDoc file = null;
882         
883         try
884         {
885             String JavaDoc url_spec = parsed_msg.getURL();
886             
887             URL url = null;
888             try
889             {
890                 url = new URL(url_spec);
891                 host = url.getHost();
892                 port = url.getPort();
893                 file = url.getFile();
894                 
895                 if (port == -1)
896                 {
897                     port = AceHTTPMessage.DEFAULT_HTTP_PORT;
898                 }
899                 
900                 // set the URL to the file
901
parsed_msg.setURL(file);
902             }
903             catch (MalformedURLException ex)
904             {
905                 // must be the file name only, send it to the default host
906
host = HTTPApplicationConfiguration.Instance().getHTTPServerHost();
907                 port = HTTPApplicationConfiguration.Instance().getHTTPServerPort();
908             }
909             
910             // connect to the server
911
// System.out.println ("Connecting to the server...");
912
serverSocket = new Socket(host, port);
913             try
914             {
915                 serverSocket.setTcpNoDelay(true);
916             }
917             catch (SocketException ex)
918             {
919                 ; // ignore
920
}
921             serverOutputStream = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream()));
922             
923             // create the server side AceInputSocketStream
924
// and start it
925
server = new AceInputSocketStream(SERVER_CONNECTION,
926             "server_" + sessionId,
927             serverSocket,
928             "http");
929             server.start();
930             
931             // System.out.println ("Started server: " + server.getName());
932
}
933         catch (UnknownHostException ex2)
934         {
935             // print error message
936
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
937             getName()
938             + "- HTTPEndPoint.connectToServer() -- Could not resolve the server name - "
939             + HTTPApplicationConfiguration.Instance().getHTTPServerHost());
940             
941             to_cont = false;
942         }
943         catch (IOException ex1)
944         {
945             // print error message
946
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
947             getName()
948             + "- HTTPEndPoint.connectToServer() -- IO error while processing message - "
949             + ex1.getMessage());
950             to_cont = false;
951         }
952         catch (AceException ex3)
953         {
954             // print error message
955
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
956             getName()
957             + "- HTTPEndPoint.processHTTPMessage() -- Internal error while processing message - "
958             + ex3.getMessage());
959             
960             to_cont = false;
961         }
962         
963         return to_cont;
964     }
965     
966     public String JavaDoc getIdentifier()
967     {
968         return identifier;
969     }
970     
971     public void setParam(String JavaDoc key, String JavaDoc value)
972     {
973         synchronized (keyValuePair)
974         {
975             keyValuePair.put(key, value);
976         }
977     }
978     
979     public String JavaDoc getParam(String JavaDoc key)
980     {
981         synchronized (keyValuePair)
982         {
983             return (String JavaDoc)keyValuePair.get(key);
984         }
985     }
986     
987     public void setParamObject(String JavaDoc key, Object JavaDoc value)
988     {
989         synchronized (keyValuePair)
990         {
991             keyValuePair.put(key, value);
992         }
993     }
994     
995     public Object JavaDoc getParamObject(String JavaDoc key)
996     {
997         synchronized (keyValuePair)
998         {
999             return keyValuePair.get(key);
1000        }
1001    }
1002    
1003    public void removeParam(String JavaDoc key)
1004    {
1005        synchronized (keyValuePair)
1006        {
1007            keyValuePair.remove(key);
1008        }
1009    }
1010    
1011    private String JavaDoc identifier;
1012    private static Object JavaDoc countLock = new Object JavaDoc();
1013    
1014    private static final int CLIENT_CONNECTION = 1;
1015    private static final int SERVER_CONNECTION = 2;
1016    private static final int PLUGIN_CONNECTION = 3;
1017    
1018    private AceInputSocketStream client = null;
1019    private AceInputSocketStream server = null;
1020    private PluginAppClientInterface plugin = null;
1021    private int pluginApplicationId;
1022    private String JavaDoc clientHost = "";
1023    
1024    private Socket clientSocket = null;
1025    private Socket serverSocket = null;
1026    private BufferedWriter clientOutputStream = null;
1027    private BufferedWriter serverOutputStream = null;
1028    private int sessionId = 0;
1029    
1030    private static int clientId = 0;
1031    private static Object JavaDoc clientIdLock = new Object JavaDoc();
1032    
1033    private int pingTimerId = -1;
1034    
1035    private HashMap keyValuePair = new HashMap();
1036}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
Popular Tags