Skip to content

Commit 7150c26

Browse files
author
zhourenjian
committed
Do not transmit a field over Simple RPC if field is not modified inside #ajaxRun
1 parent 6d04801 commit 7150c26

File tree

3 files changed

+346
-3
lines changed

3 files changed

+346
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2007 java2script.org and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Zhou Renjian - initial API and implementation
10+
*******************************************************************************/
11+
12+
package net.sf.j2s.ajax;
13+
14+
/**
15+
* @author zhou renjian
16+
*
17+
* 2007-06-10
18+
*/
19+
public interface SimpleFieldFilter {
20+
21+
/**
22+
* Filter the fields to be serialized or not.
23+
* @param field
24+
* @return true for to be ignored and false for to be serialzed
25+
*/
26+
public boolean filter(String field);
27+
28+
}

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import java.io.InputStream;
1717
import java.io.PrintWriter;
1818
import java.lang.reflect.Constructor;
19+
import java.lang.reflect.Field;
1920
import java.lang.reflect.InvocationTargetException;
21+
import java.lang.reflect.Modifier;
2022
import java.net.URLDecoder;
23+
import java.util.Arrays;
2124
import java.util.Date;
2225
import java.util.Enumeration;
2326
import java.util.HashSet;
@@ -265,8 +268,29 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
265268
//resp.setCharacterEncoding("utf-8");
266269
PrintWriter writer = resp.getWriter();
267270
runnable.deserialize(request);
271+
SimpleRPCRunnable clonedRunnable = null;
272+
try {
273+
clonedRunnable = (SimpleRPCRunnable) runnable.clone();
274+
} catch (CloneNotSupportedException e) {
275+
//e.printStackTrace();
276+
}
268277
runnable.ajaxRun();
269-
writer.write(runnable.serialize());
278+
final String[] diffs = compareDiffs(runnable, clonedRunnable);
279+
String serialize = runnable.serialize(new SimpleFieldFilter() {
280+
281+
public boolean filter(String field) {
282+
if (diffs == null || diffs.length == 0) return true;
283+
for (int i = 0; i < diffs.length; i++) {
284+
if (diffs[i].equals(field)) {
285+
return false;
286+
}
287+
}
288+
return true;
289+
}
290+
291+
});
292+
293+
writer.write(serialize);
270294
}
271295

272296
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
@@ -311,8 +335,27 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
311335
return;
312336
}
313337
runnable.deserialize(request);
338+
SimpleRPCRunnable clonedRunnable = null;
339+
try {
340+
clonedRunnable = (SimpleRPCRunnable) runnable.clone();
341+
} catch (CloneNotSupportedException e) {
342+
//e.printStackTrace();
343+
}
314344
runnable.ajaxRun();
315-
String serialize = runnable.serialize();
345+
final String[] diffs = compareDiffs(runnable, clonedRunnable);
346+
String serialize = runnable.serialize(new SimpleFieldFilter() {
347+
348+
public boolean filter(String field) {
349+
if (diffs == null || diffs.length == 0) return true;
350+
for (int i = 0; i < diffs.length; i++) {
351+
if (diffs[i].equals(field)) {
352+
return false;
353+
}
354+
}
355+
return true;
356+
}
357+
358+
});
316359

317360
if (isScriptReuest) { // cross site script response
318361
resp.setContentType("text/javascript");
@@ -461,4 +504,94 @@ private void cleanSession(HttpSession ses) {
461504
}
462505
}
463506

507+
protected String[] compareDiffs(SimpleRPCRunnable runnable1, SimpleRPCRunnable runnable2) {
508+
Set diffSet = new HashSet();
509+
Set fieldSet = new HashSet();
510+
Class clazz = runnable1.getClass();
511+
while(clazz != null && !"net.sf.j2s.ajax.SimpleSerializable".equals(clazz.getName())) {
512+
Field[] fields = clazz.getDeclaredFields();
513+
for (int i = 0; i < fields.length; i++) {
514+
fieldSet.add(fields[i]);
515+
}
516+
clazz = clazz.getSuperclass();
517+
}
518+
Field[] fields = (Field []) fieldSet.toArray(new Field[0]);
519+
for (int i = 0; i < fields.length; i++) {
520+
Field field = fields[i];
521+
int modifiers = field.getModifiers();
522+
if ((modifiers & (Modifier.PUBLIC | Modifier.PROTECTED)) != 0
523+
&& (modifiers & Modifier.TRANSIENT) == 0
524+
&& (modifiers & Modifier.STATIC) == 0) {
525+
String name = field.getName();
526+
Object field1 = null;
527+
try {
528+
field1 = field.get(runnable1);
529+
} catch (IllegalArgumentException e1) {
530+
//e1.printStackTrace();
531+
} catch (IllegalAccessException e1) {
532+
//e1.printStackTrace();
533+
}
534+
Object field2 = null;
535+
try {
536+
field2 = field.get(runnable2);
537+
} catch (IllegalArgumentException e) {
538+
//e.printStackTrace();
539+
} catch (IllegalAccessException e) {
540+
//e.printStackTrace();
541+
}
542+
System.out.println(field1.getClass().getName());
543+
if (field1 == null) {
544+
if (field2 != null) {
545+
diffSet.add(name);
546+
}
547+
} else if (field1.getClass().getName().startsWith("[")) {
548+
Class type = field.getType();
549+
if (type == float[].class) {
550+
if (!Arrays.equals((float[]) field1, (float[]) field2)) {
551+
diffSet.add(name);
552+
}
553+
} else if (type == double[].class) {
554+
if (!Arrays.equals((double[]) field1, (double[]) field2)) {
555+
diffSet.add(name);
556+
}
557+
} else if (type == int[].class) {
558+
if (!Arrays.equals((int[]) field1, (int[]) field2)) {
559+
diffSet.add(name);
560+
}
561+
} else if (type == long[].class) {
562+
if (!Arrays.equals((long[]) field1, (long[]) field2)) {
563+
diffSet.add(name);
564+
}
565+
} else if (type == short[].class) {
566+
if (!Arrays.equals((short[]) field1, (short[]) field2)) {
567+
diffSet.add(name);
568+
}
569+
} else if (type == byte[].class) {
570+
if (!Arrays.equals((byte[]) field1, (byte[]) field2)) {
571+
diffSet.add(name);
572+
}
573+
} else if (type == char[].class) {
574+
if (!Arrays.equals((char[]) field1, (char[]) field2)) {
575+
diffSet.add(name);
576+
}
577+
} else if (type == boolean[].class) {
578+
if (!Arrays.equals((boolean[]) field1, (boolean[]) field2)) {
579+
diffSet.add(name);
580+
}
581+
} else if (type == String[].class) {
582+
if (!Arrays.equals((String[]) field1, (String[]) field2)) {
583+
diffSet.add(name);
584+
}
585+
} else if (type == Object[].class) {
586+
if (!Arrays.equals((Object[]) field1, (Object[]) field2)) {
587+
diffSet.add(name);
588+
}
589+
}
590+
} else if (!field1.equals(field2)) {
591+
diffSet.add(name);
592+
}
593+
}
594+
}
595+
return (String []) diffSet.toArray(new String[diffSet.size()]);
596+
}
464597
}

0 commit comments

Comments
 (0)