Add JSON support to javascript sandbox
This task use JSON-lib to add JSON support to javascript sandbox.
Add JSON support to javascript sandbox
- Create javascript sandbox with jsoup support
- Modify com.paesia.schema.script.Machine class as following
- Create javascript as following
Modify com.paesia.schema.script.Machine class
javascript
1 | public class Machine { |
2 | |
3 | private Handler handler; |
4 | |
5 | public static void run(Machine env, String js, Map args) throws Exception { |
6 | try { |
7 | Context cx = Context.enter(); |
8 | cx.setClassShutter(new ClassShutter() { |
9 | public boolean visibleToScripts(String className) { |
10 | ........... |
11 | |
12 | if (className.startsWith("com.paesia.schema.script.safe.")) return true; |
13 | if ("org.jsoup.helper.HttpConnection$Response".equals(className)) return true; |
14 | |
15 | if ("net.sf.json.JSONArray".equals(className)) return true; |
16 | if ("net.sf.json.JSONObject".equals(className)) return true; |
17 | |
18 | return false; |
19 | } |
20 | }); |
21 | |
22 | ........... |
23 | |
24 | } catch (Exception e) { |
25 | throw e; |
26 | } finally { |
27 | Context.exit(); |
28 | } |
29 | } |
30 | |
31 | public JSONArray newJSONArray() { |
32 | return new JSONArray(); |
33 | } |
34 | |
35 | public JSONObject newJSONObject() { |
36 | return new JSONObject(); |
37 | } |
38 | |
39 | ........... |
40 | |
41 | } |
public class Machine { private Handler handler; public static void run(Machine env, String js, Map args) throws Exception { try { Context cx = Context.enter(); cx.setClassShutter(new ClassShutter() { public boolean visibleToScripts(String className) { ........... if (className.startsWith("com.paesia.schema.script.safe.")) return true; if ("org.jsoup.helper.HttpConnection$Response".equals(className)) return true; if ("net.sf.json.JSONArray".equals(className)) return true; if ("net.sf.json.JSONObject".equals(className)) return true; return false; } }); ........... } catch (Exception e) { throw e; } finally { Context.exit(); } } public JSONArray newJSONArray() { return new JSONArray(); } public JSONObject newJSONObject() { return new JSONObject(); } ........... }
1 | function main(env, args) { |
2 | var links = args.get('links'); |
3 | try { |
4 | var json = env.newJsoup().connect(env.newURL('https://www.odesk.com/api/profiles/v1/search/jobs.json?page=0;50&sort=date_posted;D')).execute().body(); |
5 | var root = env.newJSONObject().fromObject(json); |
6 | var jobs = root.getJSONObject('jobs').getJSONArray('job'); |
7 | for (var i = 0; i < jobs.size(); i++) { |
8 | var job = jobs.getJSONObject(i); |
9 | var desc = job.getString('op_description'); |
10 | var title = job.getString('op_title'); |
11 | var url = 'https://www.odesk.com/jobs/' + job.getString('ciphertext'); |
12 | var item = env.newHashMap(); |
13 | item.put('title', title); |
14 | item.put('desc', desc); |
15 | item.put('url', url); |
16 | links.add(item); |
17 | } |
18 | } catch (e) { |
19 | env.error(e); |
20 | } |
21 | } |
function main(env, args) { var links = args.get('links'); try { var json = env.newJsoup().connect(env.newURL('https://www.odesk.com/api/profiles/v1/search/jobs.json?page=0;50&sort=date_posted;D')).execute().body(); var root = env.newJSONObject().fromObject(json); var jobs = root.getJSONObject('jobs').getJSONArray('job'); for (var i = 0; i < jobs.size(); i++) { var job = jobs.getJSONObject(i); var desc = job.getString('op_description'); var title = job.getString('op_title'); var url = 'https://www.odesk.com/jobs/' + job.getString('ciphertext'); var item = env.newHashMap(); item.put('title', title); item.put('desc', desc); item.put('url', url); links.add(item); } } catch (e) { env.error(e); } }
No comments:
Post a Comment