org.openstatic.placebo.plugins.KeyStore.java Source code

Java tutorial

Introduction

Here is the source code for org.openstatic.placebo.plugins.KeyStore.java

Source

/*
Copyright (C) 2010 Brian Dunigan
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.openstatic.placebo.plugins;

import org.openstatic.http.HttpRequest;
import org.openstatic.http.HttpResponse;
import org.openstatic.placebo.*;
import org.openstatic.util.JSONUtil;

import java.util.Date;
import java.util.Properties;
import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.swing.JTextPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.Font;

import org.json.*;

public class KeyStore implements PlaceboPlugin {
    private JSONObject store;
    private JTextPane json_box;
    private boolean swing_mode;

    public boolean startup(CoreServer core, String mount_location) {
        System.err.println("KeyStore: Initializing Keystore");
        store = new JSONObject();

        if (core.isSwingOk()) {
            json_box = new JTextPane();
            json_box.setContentType("text/html");
            Font font = new Font("Monospaced", Font.BOLD, 12);
            json_box.setFont(font);
            JScrollPane scroller = new JScrollPane(json_box);
            scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            core.addPanel("Keystore [" + mount_location + "]", scroller);
            this.swing_mode = true;
        } else {
            this.swing_mode = false;
        }
        return true;
    }

    public void updateBox(JSONObject data) throws Exception {
        if (this.swing_mode)
            json_box.setText("<html><body>" + JSONUtil.json2table(store) + "</body></html>");
    }

    public void onRequest(HttpRequest request) throws Exception {
        HttpResponse response = new HttpResponse();
        if (request.getPath().equals("/set/")) {
            for (Enumeration<String> keys = request.getGetKeys(); keys.hasMoreElements();) {
                String key = keys.nextElement();
                store.put(key, request.getGetValue(key));
            }
            updateBox(store);
        } else if (request.getPath().startsWith("/set/") && request.getPath().endsWith("/")) {
            String storage_path = request.getPath().substring(5);
            storage_path = storage_path.substring(0, storage_path.length() - 1);
            StringTokenizer st = new StringTokenizer(storage_path, "/");
            JSONObject current_object = this.store;
            while (st.hasMoreTokens()) {
                String current_node = st.nextToken();
                try {
                    current_object = current_object.getJSONObject(current_node);
                } catch (Exception js_exc) {
                    current_object.put(current_node, new JSONObject());
                    current_object = current_object.getJSONObject(current_node);
                }

                if (!st.hasMoreTokens()) {
                    for (Enumeration<String> keys = request.getGetKeys(); keys.hasMoreElements();) {
                        String key = keys.nextElement();
                        current_object.put(key, request.getGetValue(key));
                    }
                }
            }
            updateBox(store);
        }
        if (request.getPath().equals("/get/")) {
            response.setData(store);
        } else if (request.getPath().startsWith("/get/") && request.getPath().endsWith("/")) {
            JSONObject this_response = new JSONObject();
            String requested_key = request.getPath().substring(5);
            requested_key = requested_key.substring(0, requested_key.length() - 1);
            StringTokenizer st = new StringTokenizer(requested_key, "/");
            JSONObject current_object = this.store;
            while (st.hasMoreTokens()) {
                String current_node = st.nextToken();
                Object temp_object = current_object.get(current_node);
                if (temp_object instanceof JSONObject) {
                    current_object = (JSONObject) temp_object;
                    if (!st.hasMoreTokens()) {
                        response.setData(current_object);
                    }
                } else {
                    if (!st.hasMoreTokens()) {
                        response.setData((String) temp_object);
                    }
                }
            }

        }
        request.sendResponse(response);
    }

    public boolean shutdown() {
        System.err.println("KeyStore: Shutdown");
        return true;
    }
}