Java tutorial
/* The MIT License (MIT) Copyright (c) 2015, Dalibor Drgo <emptychannelmc@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package eu.wordnice.wnconsole; import java.io.ByteArrayOutputStream; import org.json.simple.JSONObject; import org.json.simple.JSONValue; import eu.wordnice.api.Handler; import eu.wordnice.api.Map; import eu.wordnice.api.Set; import eu.wordnice.sockets.HIO; public class WNCListener implements Handler.TwoVoidHandler<Thread, HIO> { protected WNCPlugin wnc; public WNCListener(WNCPlugin wnc_ins) { this.wnc = wnc_ins; } /* * Format * $action_index=url_encode(json_encode($data)) * * If $action_index is not number, then is ignored. * ((TODO)) You can get values from any json with ${<val_name>.<name>...} * Settings can be stored under "settings" name, are ignored. * If name or value is null, or json parsing of value fails, * entry is ignored. * * * Example request (not url-encoded) * $ /?some={"val":"Write"}&1={"getPlayer":"Ema", "sudo":"${some.val} it!"} * There is only "1" index ("some" is ignored), and this will find player "Ema" * and force him to write "${some.val} it!", i.e. "Write it!" * * Example response * If we call the request above, and player "Ema" is online, we will get * > "1":{"getPlayer":true} * but, if "Ema" is not online, we will get * > "1":{"getPlayer":false} * * * FOR DEVS: To extend functionality and add commands, * go to the bottom of page (from line 150) * For storing shared variables, modify WNCHIOStore */ @SuppressWarnings("unchecked") @Override public void handle(Thread thr_cur, HIO hio) { try { if (hio.PATH == null || !hio.PATH.equals("/") || hio.GET == null) { try { WNCUtils.writeHeaders(hio, "200 OK", 2); hio.out.write(new byte[] { '{', '}' }); } catch (Throwable t) { } try { hio.close(); } catch (Throwable t2) { } return; } // <String, JSONObject or JSONArray or primitive> Map<String, Object> settings = new Map<String, Object>(); // <String, Map<String, Object>> Map<String, Object> args = new Map<String, Object>(); int i = 0; for (; i < hio.GET.size(); i++) { String name = hio.GET.getNameI(i); String svalue = hio.GET.getI(i); if (svalue == null || name == null) { continue; } name = name.trim(); svalue = svalue.trim(); if (svalue.length() == 0 || name.length() == 0) { continue; } try { Object value = JSONValue.parse(svalue); if (value instanceof JSONObject) { value = WNCUtils.sortObject((JSONObject) value); if (name.equals("settings")) { settings.addAll((Map<String, Object>) value); } args.addWC(name, value); } } catch (Throwable t) { t.printStackTrace(); } } Map<String, Object> out_complet = new Map<String, Object>(); WNCStore store = new WNCStore(); for (i = 0; i < args.size(); i++) { String arg_name = args.getNameI(i); Object value = args.getI(i); if (!(value instanceof Map)) { continue; } try { Long.parseLong(arg_name); } catch (Throwable t) { continue; } Map<String, Object> out = new Map<String, Object>(); Map<String, Object> cur = (Map<String, Object>) value; store.players = null; store.plugins = null; store.worlds = null; Set<String> unh = null; int i2 = 0; for (; i2 < cur.size(); i2++) { String key = cur.getNameI(i2); Object val = cur.getI(i2); /*** 153 EXTEND AREA ***/ if (eu.wordnice.wnconsole.hio.WNCHIO_Player.handleHIO(this.wnc, store, out, key, val)) continue; if (eu.wordnice.wnconsole.hio.WNCHIO_World.handleHIO(this.wnc, store, out, key, val)) continue; if (eu.wordnice.wnconsole.hio.WNCHIO_Plugin.handleHIO(this.wnc, store, out, key, val)) continue; if (eu.wordnice.wnconsole.hio.WNCHIO_File.handleHIO(this.wnc, store, out, key, val)) continue; /*** 177 ENDIF EXTEND AREA ***/ if (unh == null) { unh = new Set<String>(); } unh.addWC(key); } if (unh != null) { out.addWC("unhandled", unh); } out_complet.addWC(arg_name, out); } /* * DONE! Lets them know what we did (not) */ try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); out_complet.toJsonString(baos); byte[] response = baos.toByteArray(); WNCUtils.writeHeaders(hio, "200 OK", response.length); hio.out.write(response); } catch (Throwable t) { } try { hio.close(); } catch (Throwable t2) { } } catch (Throwable et) { this.wnc.init.getLogger().severe("Error occured while executing instructions... Details:"); et.printStackTrace(); try { WNCUtils.writeHeaders(hio, "200 OK", 2); hio.out.write(new byte[] { '{', '}' }); } catch (Throwable t) { } try { hio.close(); } catch (Throwable t2) { } return; } } //line 200 }