[[ import se.rupy.http.*; import se.rupy.http.Async.Work; import java.io.*; import java.sql.*; import org.json.*; import java.util.Date; import java.nio.file.Files; import java.nio.file.Paths; public class test extends Service implements Daemon.JVMListener { String[] description = new String[] {"Insert to database", "Exception", "Write file outside sandbox", "Read file outside sandbox", "Write file inside sandbox", "Link outside sandbox", "Link inside sandbox", "Write host header", "Add as controller", "Get hosts"}; public void create(Daemon daemon) throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); } catch(Exception e) { // Mysql driver not available on local } } public Object receive(Event event, Object message) throws Exception { return null; } public String path() { return "/"; } public void filter(Event event) throws Event, Exception { event.query().parse(); Output out = event.output(); String name = "test"; // This is entered if the /user filter chain fetches // the salt async. so then we call /user again to write // the login form and write the bottom of the page. if(event.push()) { event.daemon().chain(event, "/user").filter(event); out.println("
"); for(int i = 0; i < description.length; i++) { ]]
test_[[ i + 1 ]] [[ description[i] ]]
[[ } ]] [[ out.println("
"); out.finish(); out.flush(); throw event; } ]] Test [[ int id = event.medium("id"); String fail = null; JSONObject user = (JSONObject) event.session().get("user"); if(id == 1) { insert(); } if(id == 2) { fail.toString(); } if(id == 3) { new File("test").createNewFile(); } if(id == 4) { new File("passport").canRead(); } if(id == 5) { File file = new File("app/test.rupy.se/test"); file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); fos.write("Hello".getBytes()); BufferedReader br = new BufferedReader(new FileReader(file)); String line = br.readLine(); out.println(line); } if(id == 6) { Files.createSymbolicLink(Paths.get("app/test.rupy.se/link"), new File("passport").toPath()); } if(id == 7) { Files.createSymbolicLink(Paths.get("app/test.rupy.se/link"), new File("app/test.rupy.se/test.html").toPath()); } if(id == 8) { String host = (String) event.query().header().put("host", "trick"); String nothost = (String) event.query().header().put("nothost", "trick"); out.println("
" + host + " " + nothost + "
"); try { //String trick = (String) ((Hash) event.query().header()).secure("host", "trick"); //String trick = (String) Hack.hack((Hash) event.query().header()); } catch(Throwable t) { System.out.println(t); out.println("
" + t + "
"); } } if(id == 9) { event.daemon().set(this); } if(id == 10) { Root.getHost(); } if(user != null) { ]]
Hello [[ user.getString("name") ]]! Logout?

[[ } else { out.flush(); /* to understand this see src/se/rupy/http/User.java */ event.query().put("bare", "true"); event.query().put("url", event.query().header("host")); event.daemon().chain(event, "/user").filter(event); out.println("
"); } for(int i = 0; i < description.length; i++) { ]]
test_[[ i + 1 ]] [[ description[i] ]]
[[ } ]] [[ if(id > 0) { ]]
id: [[ id ]]
[[ } else { ]]
Host: [[ Root.local() ]]
[[ } ]] [[ } // Legacy: MySQL is replaced with ROOT, see http://root.rupy.se and below. // Just for demo, you should use connection pool. // Create tables on your database: mysql -utest_rupy_se -pMMWG8V4Y -hone.rupy.se test_rupy_se < test.sql // Edit test.sql and url, user, pass above and below. public void insert() throws Exception { /* //Disabled MySQL... Haven't used it for 5 years!!! //I will add some ROOT examples here when I have the time! Connection conn = DriverManager.getConnection("jdbc:mysql://one.rupy.se/test_rupy_se", "test_rupy_se", "MMWG8V4Y"); Statement stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO page (name, date) VALUES ('hello', " + System.currentTimeMillis() + ")"); stmt.close(); conn.close(); */ } // Async test public static class Async extends Service { int limit = 30; public String path() { return "/async"; } public void filter(Event event) throws Event, Exception { Output out = event.output(); if(event.push()) { String data = event.query().string("data"); out.println(data); if(data.equals("" + limit)) out.finish(); out.flush(); } else { event.hold(); start(event); for(int i = 0; i < 100; i++) { out.println(""); } out.println("
Async response started!");
				out.flush();
			}
		}

		private void start(final Event event) {
			new Thread(new Runnable() {
				public void run() {
					try {
						int state = Reply.OK;
						int count = 0;

						while(state == Reply.OK && count < limit + 1) {
							Thread.currentThread().sleep(300);
							event.query().put("data", "" + count++);
							state = event.reply().wakeup();
						}
					}
					catch(Exception e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
	}
	
	public static class Logout extends Service {
		public String path() { return "/logout"; }
		public void filter(Event event) throws Event, Exception {
			event.session().remove("user");
			User.redirect(event);
		}
	}
	
	// Root test
	
	public static class RootTwo extends Service {
		public int index() { return 1; }
		public String path() { return "/roottest"; }
		public void filter(Event event) throws Event, Exception {
			Output out = event.output();
			out.println("
"); for(int i = 0; i < 1000; i++) { out.println("hello"); } out.println(""); } } public static class RootOne extends Service { public String path() { return "/roottest"; } public void filter(Event event) throws Event, Exception { if(event.push()) { String success = (String) event.query().get("success"); Output out = event.output(); if(success != null) { out.print(success); } else { out.print(event.query().get("fail")); } out.flush(); out.finish(); throw event; } else { event.hold(); event.query().parse(); if(event.query().method() == Query.POST) { final String json = "{\"mail\":\"marc@test.se\"}"; se.rupy.http.Async.Work work = new se.rupy.http.Async.Work(event) { public void send(se.rupy.http.Async.Call call) throws Exception { call.post("/node", "Host:test.rupy.se", ("json=" + json + "&sort=key&create").getBytes("utf-8")); } public void read(String host, String body) throws Exception { System.out.println(body); if(body.indexOf("key") > 0) { event.query().put("success", body); } else { event.query().put("fail", body); } event.reply().wakeup(true); } public void fail(String host, Exception e) throws Exception { e.printStackTrace(); event.query().put("fail", e.toString()); event.reply().wakeup(true); } }; event.daemon().client().send("localhost", work, 30); throw event; } } } } } ]]