KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > deploytool > MicroServerHttp


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philipe Merle
23 Contributor(s): Briclet Frederic_____________________________________.
24
25 ====================================================================*/

26
27
28 package org.objectweb.ccm.deploytool;
29
30 import java.io.*;
31 import java.net.*;
32
33
34
35 public class MicroServerHttp implements Runnable JavaDoc {
36
37     private Socket s;
38     private ServerSocket ss;
39     private InputStream is;
40     private String JavaDoc filename;
41     
42     public MicroServerHttp (ServerSocket ss)
43     {
44         this.ss=ss;
45     }
46
47     public MicroServerHttp(ServerSocket ss,java.io.InputStream JavaDoc is,String JavaDoc filename)
48     {
49         this.ss=ss;
50         this.filename=filename;
51         this.is=is;
52     }
53     
54     public void run ()
55     {
56         try {
57             s=ss.accept();
58
59             InputStreamReader in = new InputStreamReader(s.getInputStream());
60             PrintStream out = new PrintStream(s.getOutputStream());
61             String JavaDoc rq = new LineNumberReader(in).readLine();
62             System.err.println("Request received:"+rq);
63             if (rq.startsWith("GET ")) {
64                 if(filename==null)
65                     {
66                         File f = new File(rq.substring(5, rq.indexOf(' ', 4)));
67                         if (f.exists() && !f.isDirectory()) {
68                             is = new FileInputStream(f);
69                         }
70                         else return;
71                     }
72
73                 byte[] data = new byte[32000];
74
75                 out.print("HTTP/1.0 200 OK\n\n");
76                 
77                  int readed=is.read(data,0,32000);
78                  while(readed>0)
79                      {
80                          out.write(data,0,readed);
81                          readed=is.read(data,0,32000);
82                      }
83                 
84                 is.read(data);
85                 is.close();
86                
87             } else {
88                     out.print("HTTP/1.0 404 Not Found\n\n");
89                     out.print("<html>Document not found.</html>");
90                 }
91             out.flush();
92             out.close();
93             s.close();
94
95         } catch (Exception JavaDoc e) {e.printStackTrace(); }
96     }
97
98
99    // public static void
100
// main (String[] args)
101
// throws IOException
102
// {
103
// ServerSocket s = new ServerSocket(0);
104

105 // System.out.println(s);
106

107     
108 // System.out.println("http://" + InetAddress.getLocalHost().getHostName() + ':' + s.getLocalPort() + '/' +args[0]);
109

110 // while (true) { new Thread(new MicroServerHttp(s.accept())).start(); }
111
// }
112

113 }
114
Popular Tags