xitiomet / placebohttp (http://openstatic.org/)

Embeded Http server for gcj/java applications. PlaceboHttp uses an automatic session management system, Treating each session as a HttpRequest queue.

Clone this repository (size: 135.2 KB): HTTPS / SSH
$ hg clone http://hg.openstatic.org/placebohttp
commit 39: bdc180ea8724
parent 38: 893032f95884
branch: default
lots of features for plugins
xitiomet
4 months ago

Changed (Δ2.9 KB):

raw changeset »

src/org/openstatic/http/HttpResponse.java (18 lines added, 2 lines removed)

src/org/openstatic/placebo/ControlPanel.java (12 lines added, 2 lines removed)

src/org/openstatic/placebo/CoreServer.java (47 lines added, 8 lines removed)

src/org/openstatic/placebo/PlaceboPlugin.java (4 lines added, 3 lines removed)

src/org/openstatic/placebo/plugins/Clock.java (9 lines added, 6 lines removed)

Up to file-list src/org/openstatic/http/HttpResponse.java:

@@ -27,8 +27,24 @@ public class HttpResponse
27
27
28
28
    public static String getContentTypeFor(String filename)
29
29
    {
30
        FileNameMap fnm = URLConnection.getFileNameMap();
31
        return fnm.getContentTypeFor(filename);
30
        String lc_file = filename.toLowerCase();
31
        if (lc_file.endsWith(".html") || lc_file.endsWith(".htm"))
32
        {
33
            return "text/html";
34
        } else if (lc_file.endsWith(".txt")) {
35
            return "text/plain";
36
        } else if (lc_file.endsWith(".css")) {
37
            return "text/css";
38
        } else if (lc_file.endsWith(".js")) {
39
            return "text/javascript";
40
        } else if (lc_file.endsWith(".jpg") || lc_file.endsWith(".jpe") || lc_file.endsWith(".jpeg")) {
41
            return "image/jpeg";
42
        } else if (lc_file.endsWith(".gif")) {
43
            return "image/gif";
44
        } else {
45
            FileNameMap fnm = URLConnection.getFileNameMap();
46
            return fnm.getContentTypeFor(filename);
47
        }
32
48
    }
33
49
    
34
50
    public void setContentType(String type)

Up to file-list src/org/openstatic/placebo/ControlPanel.java:

@@ -23,6 +23,7 @@ import java.awt.Toolkit;
23
23
import java.awt.Dimension;
24
24
import java.awt.event.ActionEvent;
25
25
import java.awt.event.ActionListener;
26
import java.awt.event.WindowEvent;
26
27
import javax.swing.JTabbedPane;
27
28
import java.awt.Font;
28
29
import java.awt.Color;
@@ -136,8 +137,17 @@ public class ControlPanel extends JFrame
136
137
    public ControlPanel()
137
138
    {
138
139
        super("Placebo Web Server");
139
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140
140
        this.addWindowListener(new java.awt.event.WindowAdapter()
141
        {
142
            public void windowClosing(WindowEvent winEvt)
143
            {
144
                if (ControlPanel.this.core != null)
145
                {
146
                    ControlPanel.this.core.quit();
147
                }
148
                System.exit(0); 
149
            }
150
        });
141
151
        tabbed = new JTabbedPane();
142
152
        
143
153
        JPanel main_pane = new JPanel();

Up to file-list src/org/openstatic/placebo/CoreServer.java:

@@ -16,6 +16,7 @@ import java.util.StringTokenizer;
16
16
import java.util.Properties;
17
17
import java.util.Hashtable;
18
18
import java.util.Enumeration;
19
import java.util.Properties;
19
20
import java.lang.reflect.Constructor;
20
21
21
22
public class CoreServer extends Thread
@@ -28,7 +29,8 @@ public class CoreServer extends Thread
28
29
    private boolean keep_running;
29
30
    private PlaceboHttpServer server;
30
31
    private Hashtable<String, PlaceboPlugin> plugins;
31
    
32
    private Properties global_settings;
33
32
34
    public CoreServer()
33
35
    {
34
36
        this.http_port = 80;
@@ -39,6 +41,7 @@ public class CoreServer extends Thread
39
41
        this.show_data = false;
40
42
        this.server = new PlaceboHttpServer(http_port);
41
43
        this.plugins = new Hashtable<String, PlaceboPlugin>();
44
        this.global_settings = new Properties();
42
45
    }
43
46
    
44
47
    public void addPlugins(Hashtable<String, String> plugin_list)
@@ -66,17 +69,27 @@ public class CoreServer extends Thread
66
69
    
67
70
    public void addPlugin(String path, String class_name)
68
71
    {
72
        String pl_path = path;
73
        if (!pl_path.startsWith("/"))
74
            pl_path = "/" + pl_path;
75
        if (!pl_path.endsWith("/"))
76
            pl_path = pl_path + "/";
69
77
        try
70
78
        {
71
79
            Class<?> c = Class.forName(class_name);
72
80
            Constructor<?> cons = c.getDeclaredConstructor();
73
81
            PlaceboPlugin new_plugin = (PlaceboPlugin) cons.newInstance();
74
            new_plugin.startup();
75
            this.plugins.put(path, new_plugin);
76
            this.server.logln("Added (" + class_name + ") at " + path);
82
            if (new_plugin.startup(this.global_settings))
83
            {
84
                this.server.logln("Plugin Started (" + class_name + ")");
85
                this.plugins.put(pl_path, new_plugin);
86
                this.server.logln("Mounted (" + class_name + ") at " + pl_path);
87
            } else {
88
                this.server.logln("Plugin (" + class_name + ") Failed to Startup");
89
            }
77
90
            this.pluginChange();
78
91
        } catch (Exception e) {
79
            this.server.logln("Couldn't Init (" + class_name + ") at " + path + " because " + e.toString() + " / " + e.getMessage());
92
            this.server.logln("Couldn't Init (" + class_name + ") at " + pl_path + " because " + e.toString() + " / " + e.getMessage());
80
93
        }
81
94
    }
82
95
    
@@ -101,7 +114,20 @@ public class CoreServer extends Thread
101
114
    {
102
115
        return this.server.getSessions();
103
116
    }
104
    
117
118
    private String findPluginPath(String path)
119
    {
120
        for (Enumeration<String> e = this.plugins.keys(); e.hasMoreElements(); )
121
        {
122
            String plugin_key = e.nextElement();
123
            if (path.startsWith(plugin_key))
124
            {
125
                return plugin_key;
126
            }
127
        }
128
        return null;
129
    }    
130
105
131
    private PlaceboPlugin findPlugin(String path)
106
132
    {
107
133
        for (Enumeration<String> e = this.plugins.keys(); e.hasMoreElements(); )
@@ -144,6 +170,17 @@ public class CoreServer extends Thread
144
170
    public void quit()
145
171
    {
146
172
        this.keep_running = false;
173
        for (Enumeration<String> e = this.plugins.keys(); e.hasMoreElements(); )
174
        {
175
            String plugin_key = e.nextElement();
176
            PlaceboPlugin this_plugin = this.plugins.get(plugin_key);
177
            if (this_plugin.shutdown())
178
            {
179
                this.server.logln("Shutdown Plugin (" + this_plugin.toString() + ") OK!");
180
            } else {
181
                this.server.logln("Shutdown Plugin (" + this_plugin.toString() + ") Plugin failed to shutdown!");
182
            }
183
        }
147
184
        server.shutdown();
148
185
        this.interrupt();
149
186
    }
@@ -175,7 +212,9 @@ public class CoreServer extends Thread
175
212
                if (plugin != null)
176
213
                {
177
214
                    server.logln("Found Plugin for [" + request.getPath() + "]");
178
                    plugin.onRequest(request);
215
                    String plugin_path_prefix = findPluginPath(request.getPath());
216
                    String local_path = request.getPath().substring(plugin_path_prefix.length()-1);
217
                    plugin.onRequest(request.getPath().substring(plugin_path_prefix.length()-1), request);
179
218
                } else {
180
219
                    // create new response object
181
220
                    HttpResponse response = new HttpResponse();
@@ -245,7 +284,7 @@ public class CoreServer extends Thread
245
284
                                        if (ze != null)
246
285
                                        {
247
286
                                            InputStream zeis = zipfile.getInputStream(ze);
248
                                            response.setBufferedData(zeis, HttpResponse.getContentTypeFor(file_internal_path));
287
                                            response.setBufferedData(zeis, HttpResponse.getContentTypeFor(zip_item));
249
288
                                        } else {
250
289
                                            response.setResponseCode("404 Not Found");
251
290
                                        }

Up to file-list src/org/openstatic/placebo/PlaceboPlugin.java:

1
1
package org.openstatic.placebo;
2
2
3
3
import org.openstatic.http.HttpRequest;
4
import java.util.Properties;
4
5
5
6
public interface PlaceboPlugin
6
7
{
7
    public void startup();
8
    public void onRequest(HttpRequest request);
9
    public void shutdown();
8
    public boolean startup(Properties settings);
9
    public void onRequest(String local_path, HttpRequest request);
10
    public boolean shutdown();
10
11
}

Up to file-list src/org/openstatic/placebo/plugins/Clock.java:

@@ -5,24 +5,27 @@ import org.openstatic.http.HttpResponse;
5
5
import org.openstatic.placebo.PlaceboPlugin;
6
6
7
7
import java.util.Date;
8
import java.util.Properties;
8
9
9
10
10
11
public class Clock implements PlaceboPlugin
11
12
{
12
    public void startup()
13
    public boolean startup(Properties settings)
13
14
    {
14
        
15
        System.err.println("CLOCK: I RECIEVED A STARTUP");
16
        return true;
15
17
    }
16
18
    
17
    public void onRequest(HttpRequest request)
19
    public void onRequest(String local_path, HttpRequest request)
18
20
    {
19
21
        HttpResponse response = new HttpResponse();
20
        response.setData("<html><body>Current Time/Date: " +(new Date()).toString() + "<br /><br />Your Session key is " + request.getPlaceboSession().getSessionId() + "</body></html>");
22
        response.setData("<html><body>Current Time/Date: " +(new Date()).toString() + "<br /><br />Your Session key is " + request.getPlaceboSession().getSessionId() + "<br /><br />local request path = '" + local_path + "'</body></html>");
21
23
        request.sendResponse(response);
22
24
    }
23
25
    
24
    public void shutdown()
26
    public boolean shutdown()
25
27
    {
26
        
28
        System.err.println("CLOCK: I RECIEVED A SHUTDOWN");
29
        return true;
27
30
    }
28
31
}