Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4adc2fa

Browse files
author
zhourenjian
committed
Add compound pipe support;
Fix bugs on XSS request mode; Support exporting this plugin inside Eclipse;
1 parent 083c398 commit 4adc2fa

21 files changed

+890
-184
lines changed

sources/net.sf.j2s.ajax/.j2s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#Java2Script Configuration
2-
#Sun Mar 18 19:32:54 CST 2007
2+
#Sun Sep 07 01:57:44 CST 2008
33
j2s.abandoned.resources.list=bin/net/sf/j2s/ajax/AjaxPlugin.js,bin/net/sf/j2s/ajax/AJAXVariableInitializer.js
44
j2s.compiler.abbreviation=true
55
j2s.compiler.status=enable
66
j2s.compiler.abbreviation.prefix=$_
77
j2s.compiler.mode=debug
8-
j2s.resources.list=../../plugins/net.sf.j2s.lib_1.0.0/j2slib/java.runtime.j2x,bin/net/sf/j2s/ajax/XHRCallbackSWTAdapter.js,bin/net/sf/j2s/ajax/XHRCallbackAdapter.js,bin/net/sf/j2s/ajax/IXHRCallback.js,bin/net/sf/j2s/ajax/HttpRequest.js,bin/net/sf/j2s/ajax/AClass.js,bin/net/sf/j2s/ajax/ASWTClass.js,bin/net/sf/j2s/ajax/ARunnable.js,bin/net/sf/j2s/ajax/Base64.js,bin/net/sf/j2s/ajax/SimpleSerializable.js,bin/net/sf/j2s/ajax/SimpleRPCRunnable.js,bin/net/sf/j2s/ajax/SimpleRPCHttpServlet.js,bin/net/sf/j2s/ajax/SimpleRPCRequest.js,bin/net/sf/j2s/ajax/SimpleRPCSWTRequest.js,bin/net/sf/j2s/ajax/Base64Auth.js
8+
j2s.resources.list=../../plugins/net.sf.j2s.lib_1.0.0/j2slib/java.runtime.j2x,bin/net/sf/j2s/ajax/XHRCallbackSWTAdapter.js,bin/net/sf/j2s/ajax/XHRCallbackAdapter.js,bin/net/sf/j2s/ajax/IXHRCallback.js,bin/net/sf/j2s/ajax/HttpRequest.js,bin/net/sf/j2s/ajax/AClass.js,bin/net/sf/j2s/ajax/ASWTClass.js,bin/net/sf/j2s/ajax/ARunnable.js,bin/net/sf/j2s/ajax/Base64.js,bin/net/sf/j2s/ajax/SimpleSerializable.js,bin/net/sf/j2s/ajax/SimpleRPCRunnable.js,bin/net/sf/j2s/ajax/SimpleRPCHttpServlet.js,bin/net/sf/j2s/ajax/SimpleRPCRequest.js,bin/net/sf/j2s/ajax/SimpleRPCSWTRequest.js,bin/net/sf/j2s/ajax/SimpleFieldFilter.js,bin/net/sf/j2s/ajax/SimpleFilter.js,bin/net/sf/j2s/ajax/SimplePipeHelper.js,bin/net/sf/j2s/ajax/SimplePipeHttpServlet.js,bin/net/sf/j2s/ajax/SimplePipeRPCHttpServlet.js,bin/net/sf/j2s/ajax/SimplePipeRequest.js,bin/net/sf/j2s/ajax/SimplePipeRunnable.js,bin/net/sf/j2s/ajax/SimplePipeSWTRequest.js,bin/net/sf/j2s/ajax/CompoundPipeRunnable.js,bin/net/sf/j2s/ajax/SessionPipeRunnable.js,bin/net/sf/j2s/ajax/PipeManager.js,bin/net/sf/j2s/ajax/PipeObject.js,bin/net/sf/j2s/ajax/CompoundPipeRequest.js,bin/net/sf/j2s/ajax/CompoundSerializable.js,bin/net/sf/j2s/ajax/CompoundPipeSWTRequest.js,bin/net/sf/j2s/ajax/CompoundPipeSession.js,bin/net/sf/j2s/ajax/CompoundPipeHttpServlet.js,bin/net/sf/j2s/ajax/CompoundPipeRPCHttpServlet.js,bin/net/sf/j2s/ajax/SWTHelper.js
99
j2s.compiler.whitespace=false
1010
j2s.output.path=bin
1111
j2s.compiler.linebreak=\r\n
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package net.sf.j2s.ajax;
2+
3+
public class CompoundPipeRPCHttpServlet extends SimplePipeRPCHttpServlet {
4+
5+
private static final long serialVersionUID = 1605715722698968061L;
6+
7+
@Override
8+
protected SimpleRPCRunnable getRunnableByRequest(String request) {
9+
SimpleRPCRunnable runnable = super.getRunnableByRequest(request);
10+
if (runnable instanceof CompoundPipeSession) {
11+
CompoundPipeSession session = (CompoundPipeSession) runnable;
12+
SimplePipeRunnable pipe = SimplePipeHelper.getPipe(session.pipeKey);
13+
if (pipe instanceof CompoundPipeRunnable) {
14+
CompoundPipeRunnable p = (CompoundPipeRunnable) pipe;
15+
p.weave(session);
16+
//p.status = 3;
17+
}
18+
}
19+
return runnable;
20+
}
21+
22+
protected boolean validateRunnable(String clazzName) {
23+
if ("net.sf.j2s.ajax.CompoundPipeRunnable".equals(clazzName)) {
24+
return true;
25+
}
26+
return runnables.contains(clazzName);
27+
}
28+
29+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package net.sf.j2s.ajax;
2+
3+
import java.util.Date;
4+
5+
public class CompoundPipeRequest extends SimplePipeRequest {
6+
7+
static CompoundPipeRunnable pipe;
8+
9+
static int count = 0;
10+
11+
static long lastTried = 0;
12+
13+
static String pipeMethod = "GET";
14+
15+
static String rpcMethod = "POST";
16+
17+
static String pipeURL = "simplepipe";
18+
19+
static String rpcURL = "piperpc";
20+
21+
22+
public static void weave(CompoundPipeSession p) {
23+
if (pipe == null || !pipe.isPipeLive()) {
24+
pipe = new CompoundPipeRunnable() {
25+
26+
@Override
27+
public void ajaxOut() {
28+
super.ajaxOut();
29+
for (int i = 0; i < pipes.length; i++) {
30+
if (pipes[i] != null) {
31+
pipes[i].pipeKey = pipe.pipeKey;
32+
SimpleRPCRequest.request(pipes[i]);
33+
if (pipe.status < 2) {
34+
pipe.status = 2; // requested
35+
}
36+
}
37+
}
38+
}
39+
40+
@Override
41+
public void ajaxFail() {
42+
CompoundPipeRequest.pipeFailed(this);
43+
}
44+
45+
};
46+
pipe.weave(p);
47+
pipe.updateStatus(true);
48+
SimplePipeRequest.pipe(pipe);
49+
pipe.status = 1; // pipe
50+
} else {
51+
pipe.weave(p);
52+
if (pipe.pipeKey != null) {
53+
p.pipeKey = pipe.pipeKey;
54+
SimpleRPCRequest.request(p);
55+
if (pipe.status < 2) {
56+
pipe.status = 2; // requested
57+
}
58+
}
59+
}
60+
}
61+
62+
static void pipeFailed(CompoundPipeRunnable pipe) {
63+
long now = new Date().getTime();
64+
if (now - lastTried > 5 * 60 * 1000) { // five minutes
65+
count = 0;
66+
}
67+
count++;
68+
if (count <= 3) {
69+
// take another trys
70+
pipe.updateStatus(true);
71+
lastTried = now;
72+
SimplePipeRequest.pipe(pipe);
73+
} else {
74+
for (int i = 0; i < pipe.pipes.length; i++) {
75+
if (pipe.pipes[i] != null) {
76+
pipe.pipes[i].pipeFailed();
77+
}
78+
pipe.pipes[i] = null;
79+
}
80+
pipe = null;
81+
}
82+
}
83+
84+
public static void configure(String pipeURL, String pipeMethod, String rpcURL, String rpcMethod) {
85+
if (pipeURL != null) {
86+
CompoundPipeRequest.pipeURL = pipeURL;
87+
}
88+
if (pipeMethod != null) {
89+
CompoundPipeRequest.pipeMethod = pipeMethod;
90+
}
91+
if (rpcURL != null) {
92+
CompoundPipeRequest.rpcURL = rpcURL;
93+
}
94+
if (rpcMethod != null) {
95+
CompoundPipeRequest.rpcMethod = rpcMethod;
96+
}
97+
}
98+
}
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package net.sf.j2s.ajax;
2+
3+
public class CompoundPipeRunnable extends SimplePipeRunnable {
4+
5+
private static String nextSessionKey() {
6+
String hexStr = "0123456789abcdef";
7+
String key = "";
8+
for (int i = 0; i < 4; i++) {
9+
int hex = (int) Math.round(15 * Math.random());
10+
key += "" + hexStr.charAt(hex);
11+
}
12+
return key;
13+
}
14+
15+
CompoundPipeSession[] pipes;
16+
17+
int status;
18+
19+
public CompoundPipeRunnable() {
20+
pipes = new CompoundPipeSession[100];
21+
status = 0; // starting
22+
}
23+
24+
protected CompoundPipeSession getSession(String session) {
25+
if (session == null) {
26+
return null;
27+
}
28+
for (int i = 0; i < pipes.length; i++) {
29+
if (pipes[i] != null && session.equals(pipes[i].session)) {
30+
return pipes[i];
31+
}
32+
}
33+
return null;
34+
}
35+
36+
@Override
37+
public void pipeDestroy() {
38+
for (int i = 0; i < pipes.length; i++) {
39+
if (pipes[i] != null) {
40+
pipes[i].pipeDestroy();
41+
pipes[i] = null;
42+
}
43+
}
44+
}
45+
46+
@Override
47+
public boolean pipeSetup() {
48+
return true;
49+
}
50+
51+
@Override
52+
public boolean isPipeLive() {
53+
if (status < 3) { // connected
54+
return true; // still in starting status
55+
}
56+
if (super.isPipeLive()) {
57+
for (int i = 0; i < pipes.length; i++) {
58+
if (pipes[i] != null) {
59+
if (pipes[i].isPipeLive()) {
60+
return true;
61+
}
62+
}
63+
}
64+
}
65+
return false;
66+
}
67+
68+
@Override
69+
public void pipeClosed() {
70+
for (int i = 0; i < pipes.length; i++) {
71+
if (pipes[i] != null) {
72+
pipes[i].pipeClosed();
73+
}
74+
}
75+
}
76+
77+
@Override
78+
public void pipeLost() {
79+
for (int i = 0; i < pipes.length; i++) {
80+
if (pipes[i] != null) {
81+
pipes[i].pipeLost();
82+
}
83+
}
84+
}
85+
86+
@Override
87+
public void keepPipeLive() {
88+
for (int i = 0; i < pipes.length; i++) {
89+
if (pipes[i] != null) {
90+
pipes[i].keepPipeLive();
91+
}
92+
}
93+
}
94+
95+
public void weave(CompoundPipeSession pipe) {
96+
synchronized (pipes) {
97+
for (int i = 0; i < pipes.length; i++) {
98+
if (pipe == pipes[i]) {
99+
return;
100+
}
101+
}
102+
boolean added = false;
103+
for (int i = 0; i < pipes.length; i++) {
104+
if (pipes[i] == null) {
105+
pipes[i] = pipe;
106+
added = true;
107+
break;
108+
}
109+
}
110+
if (!added) {
111+
CompoundPipeSession[] newPipes = new CompoundPipeSession[pipes.length + 100];
112+
System.arraycopy(pipes, 0, newPipes, 0, pipes.length);
113+
newPipes[pipes.length] = pipe;
114+
}
115+
}
116+
while (pipe.session == null) {
117+
String key = nextSessionKey();
118+
boolean isKeyOK = true;
119+
for (int i = 0; i < pipes.length; i++) {
120+
if (pipes[i] != null && key.equals(pipes[i].session)) {
121+
isKeyOK = false;
122+
break;
123+
}
124+
}
125+
if (isKeyOK) {
126+
pipe.session = key;
127+
break;
128+
}
129+
}
130+
}
131+
132+
public void unweave(CompoundPipeSession pipe) {
133+
for (int i = 0; i < pipes.length; i++) {
134+
if (pipe == pipes[i]) {
135+
pipes[i] = null;
136+
break;
137+
}
138+
}
139+
}
140+
141+
@Override
142+
public SimpleSerializable[] through(Object... args) {
143+
/*
144+
for (int i = 0; i < pipes.length; i++) {
145+
if (pipes[i] != null) {
146+
CompoundSerializable[] ss = pipes[i].convert(args);
147+
if (ss != null) {
148+
for (int j = 0; j < ss.length; j++) {
149+
if (ss[j] != null) {
150+
ss[j].session = pipes[i].session;
151+
}
152+
}
153+
return ss;
154+
}
155+
}
156+
}
157+
*/
158+
return null;
159+
}
160+
161+
@Override
162+
public boolean deal(SimpleSerializable ss) {
163+
if (ss instanceof CompoundSerializable) {
164+
CompoundSerializable cs = (CompoundSerializable) ss;
165+
Class<? extends CompoundSerializable> clazz = cs.getClass();
166+
if ("net.sf.j2s.ajax.CompoundSerializable".equals(clazz.getName())) {
167+
return true; // seldom or never reach this branch, just ignore
168+
}
169+
for (int i = 0; i < pipes.length; i++) {
170+
CompoundPipeSession p = pipes[i];
171+
if (p != null && p.session != null
172+
&& p.session.equals(cs.session)
173+
&& p.deal(cs)) {
174+
return true;
175+
}
176+
}
177+
}
178+
return false;
179+
}
180+
181+
@Override
182+
public String getHttpURL() {
183+
return CompoundPipeRequest.rpcURL;
184+
}
185+
186+
@Override
187+
public String getHttpMethod() {
188+
return CompoundPipeRequest.rpcMethod;
189+
}
190+
191+
@Override
192+
public String getPipeURL() {
193+
return CompoundPipeRequest.pipeURL;
194+
}
195+
196+
@Override
197+
public String getPipeMethod() {
198+
return CompoundPipeRequest.pipeMethod;
199+
}
200+
201+
}

0 commit comments

Comments
 (0)