forked from m0ver/tinystruct-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyeditor.java
More file actions
executable file
·71 lines (56 loc) · 1.97 KB
/
tinyeditor.java
File metadata and controls
executable file
·71 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package tinystruct.examples;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.tinystruct.AbstractApplication;
import org.tinystruct.system.util.StringUtilities;
public class tinyeditor extends AbstractApplication {
private final Map<String, String> map = Collections.synchronizedMap(new HashMap<String, String>());
@Override
public void init() {
// TODO Auto-generated method stub
this.setAction("tinyeditor", "index");
this.setAction("tinyeditor/update", "update");
this.setAction("tinyeditor/save", "save");
this.setAction("tinyeditor/version", "version");
}
public tinyeditor index(){
return this;
}
public void update() throws InterruptedException, IOException {
HttpServletResponse response = (HttpServletResponse) this.context
.getAttribute("HTTP_RESPONSE");
synchronized(this.map) {
while (true) {
this.map.wait();
if(this.map.containsKey("textvalue")) {
System.out.println(this.getVariable("browser").getValue().toString()+":"+this.map.get("textvalue"));
response.getWriter().println(
"<script charset=\"utf-8\"> var message = '" + new StringUtilities(this.map.get("textvalue")).replace('\n', "\\n")
+ "';parent.update(message);</script>");
response.getWriter().flush();
}
}
}
}
public boolean save() {
HttpServletRequest request = (HttpServletRequest) this.context
.getAttribute("HTTP_REQUEST");
synchronized(this.map){
String[] agent = request.getHeader("User-Agent").split(" ");
this.setVariable("browser", agent[agent.length-1]);
this.map.put("textvalue", request.getParameter("text"));
if(this.map.containsKey("textvalue")) this.map.remove("textvalue");
this.map.notify();
}
return true;
}
@Override
public String version() {
// TODO Auto-generated method stub
return "Welcome to use tinystruct 2.0";
}
}