2
* File : XMLRequestProcessor.java
3
* Created : 15-Mar-2004
6
* Azureus - a Java Bittorrent client
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details ( see the LICENSE file ).
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
package org.gudy.azureus2.ui.webplugin.remoteui.xml.server;
32
import java.lang.reflect.*;
34
import org.gudy.azureus2.core3.util.ByteFormatter;
35
import org.gudy.azureus2.core3.util.Debug;
36
import org.gudy.azureus2.core3.xml.simpleparser.*;
37
import org.gudy.azureus2.core3.xml.util.*;
38
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument;
39
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttribute;
40
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentException;
41
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNode;
42
import org.gudy.azureus2.pluginsimpl.remote.*;
48
protected RPRequestHandler request_handler;
49
protected SimpleXMLParserDocument request;
53
RPRequestHandler _request_handler,
54
RPRequestAccessController _access_controller,
61
request_handler = _request_handler;
63
writeLineRaw( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
66
writeLineRaw( "<RESPONSE>" );
70
request = SimpleXMLParserDocumentFactory.create( _request );
72
process( _client_ip , _access_controller);
74
}catch( Throwable e ){
76
Debug.printStackTrace( e );
78
if ( e instanceof SimpleXMLParserDocumentException ){
80
writeTag("ERROR", "Invalid XML Plugin request received - " + exceptionToString(e));
84
writeTag("ERROR", e );
92
writeLineRaw( "</RESPONSE>" );
101
RPRequestAccessController access_controller )
105
RPRequest req_obj = (RPRequest)deserialiseObject( request, RPRequest.class, "" );
107
req_obj.setClientIP( client_ip );
109
RPReply reply = request_handler.processRequest( req_obj, access_controller );
111
// void methods result in null return
113
if ( reply != null ){
115
Map props = reply.getProperties();
117
Iterator it = props.keySet().iterator();
119
while( it.hasNext()){
121
String name = (String)it.next();
122
String value = (String)props.get(name);
124
writeTag( name, value );
128
Object response = reply.getResponse();
130
// null responses are represented by no response
132
if ( response != null ){
134
serialiseObject( response, "", 0xffffffff );
137
}catch( RPException e ){
139
Debug.printStackTrace( e );
141
writeTag("ERROR", e );
152
writeLineRaw( "<ERROR>");
154
writeLineEscaped( exceptionToString(e));
156
if ( e instanceof RPException && e.getCause() != null ){
161
serialiseObject( e, " ", ~Modifier.PRIVATE );
163
writeLineRaw( "</ERROR>" );
170
Throwable cause = e.getCause();
172
if ( cause != null ){
174
String m = cause.getMessage();
181
return( cause.toString());
184
String m = e.getMessage();
191
return( e.toString());
196
SimpleXMLParserDocumentNode node,
200
// hack I'm afraid, when deserialising request objects we need to use the
201
// method to correctly deserialise parameters
203
String request_method = null;
205
if ( cla == RPRequest.class ){
207
request_method = node.getChild( "METHOD" ).getValue().trim();
211
// System.out.println(indent + "deser:" + cla.getName());
214
Object obj = cla.newInstance();
216
Field[] fields = cla.getDeclaredFields();
218
for (int i=0;i<fields.length;i++){
220
Field field = fields[i];
222
int modifiers = field.getModifiers();
224
if (( modifiers & ( Modifier.TRANSIENT | Modifier.STATIC )) == 0 ){
226
String name = field.getName();
228
Class type = field.getType();
230
SimpleXMLParserDocumentNode child = node.getChild( name );
232
// System.out.println( indent + " field:" + field.getName() + " -> " + child );
234
if ( child != null ){
236
if ( type.isArray()){
238
Class sub_type = type.getComponentType();
240
SimpleXMLParserDocumentNode[] entries = child.getChildren();
242
Object array = Array.newInstance( sub_type, entries.length );
244
// hack. for request objects we deserialise the parameters
245
// array by using the method signature
247
if ( request_method != null ){
251
String[] bits = new String[entries.length];
253
int method_pos = request_method.indexOf( '[' )+1;
255
for (int j=0;j<entries.length;j++){
257
int p1 = request_method.indexOf(',',method_pos);
263
bits[j] = request_method.substring( method_pos, request_method.length()-1).toLowerCase();
268
bits[j] = request_method.substring( method_pos, p1 ).toLowerCase();
274
for (int j=0;j<entries.length;j++){
276
SimpleXMLParserDocumentNode array_child = entries[j];
278
SimpleXMLParserDocumentAttribute index_attr = array_child.getAttribute("index");
280
String index_str = index_attr==null?null:index_attr.getValue().trim();
282
int array_index = index_str==null?j:Integer.parseInt(index_str);
284
String bit = bits[array_index];
286
String sub_value = array_child.getValue().trim();
288
if ( bit.equals("string")){
290
Array.set( array, array_index, sub_value );
292
}else if ( bit.equals("int")){
294
Array.set( array, array_index, Integer.valueOf( sub_value));
296
}else if ( bit.equals("boolean")){
298
Array.set( array, array_index, Boolean.valueOf( sub_value ));
300
}else if ( bit.equals("url")){
302
Array.set( array, array_index, new URL(sub_value));
304
}else if ( bit.equals("byte[]")){
306
Array.set( array, array_index, ByteFormatter.decodeString( sub_value ));
309
// see if its an object
311
SimpleXMLParserDocumentNode obj_node = array_child.getChild("OBJECT");
313
if ( obj_node != null ){
315
String oid_str = obj_node.getChild("_object_id").getValue().trim();
317
long oid = Long.parseLong( oid_str );
319
RPObject local_obj = RPObject._lookupLocal( oid );
321
Array.set( array, array_index, local_obj );
325
throw( new RuntimeException( "not implemented"));
332
for (int j=0;j<entries.length;j++){
334
SimpleXMLParserDocumentNode array_child = entries[j];
336
if ( sub_type == String.class ){
338
Array.set( array, j, child.getValue().trim());
342
throw( new RuntimeException( "not implemented"));
347
field.set( obj, array );
350
String value = child.getValue().trim();
352
if ( type == String.class ){
354
field.set( obj, value );
356
}else if ( type == long.class ){
358
field.setLong( obj,Long.parseLong( value ));
360
}else if ( type == boolean.class ){
362
throw( new RuntimeException( "not implemented"));
364
//field.set( obj, new Long(Long.parseLong( value )));
366
}else if ( type == byte.class ){
368
throw( new RuntimeException( "not implemented"));
370
//field.set( obj, new Long(Long.parseLong( value )));
372
}else if ( type == char.class ){
374
field.setChar( obj, value.charAt(0));
376
}else if ( type == double.class ){
378
field.setDouble( obj, Double.parseDouble( value));
380
}else if ( type == float.class ){
382
field.setFloat( obj, Float.parseFloat( value));
384
}else if ( type == int.class ){
386
field.setInt( obj, Integer.parseInt( value));
388
}else if ( type == short.class ){
390
field.setShort( obj, (Short.parseShort( value )));
392
}else if ( type == Long.class || type == long.class ){
394
field.set( obj, new Long(Long.parseLong( value )));
398
field.set( obj, deserialiseObject( child, type, indent + " " ));
407
}catch( Throwable e ){
409
Debug.printStackTrace( e );
411
throw( new RuntimeException( e.toString()));
419
int original_modifier_filter )
421
int modifier_filter = original_modifier_filter & (~(Modifier.TRANSIENT | Modifier.STATIC));
423
Class cla = obj.getClass();
425
// System.out.println(indent + "ser:" + cla.getName());
429
int len = Array.getLength( obj );
431
// byte[] -> hex chars
433
if ( cla.getComponentType() == byte.class ){
435
byte[] data = (byte[])obj;
437
writeLineEscaped( ByteFormatter.nicePrint( data, true ));
441
for (int i=0;i<len;i++){
443
Object entry = Array.get( obj, i );
446
writeLineRaw( "<ENTRY index=\""+i+"\">" );
450
serialiseObject( entry, indent+" ", original_modifier_filter);
456
writeLineRaw( "</ENTRY>");
464
if ( cla == String.class ){
466
writeLineEscaped( "" + (String)obj);
468
}else if ( cla == Integer.class ){
470
writeLineEscaped( "" + ((Integer)obj).intValue());
472
}else if ( cla == Boolean.class ){
474
writeLineEscaped( "" + ((Boolean)obj).booleanValue());
478
while( cla != null ){
480
Field[] fields = cla.getDeclaredFields();
482
for (int i=0;i<fields.length;i++){
484
Field field = fields[i];
486
int modifiers = field.getModifiers();
488
if ((( modifiers | modifier_filter ) == modifier_filter )){
490
String name = field.getName();
492
Class type = field.getType();
494
// System.out.println( indent + " field:" + field.getName() + ", type = " + type );
497
writeLineRaw( "<" + name + ">" );
501
if ( type == String.class ){
503
writeLineEscaped( (String)field.get( obj ));
505
}else if ( type == File.class ){
507
writeLineEscaped( "" + ((File)field.get( obj )).toString());
509
}else if ( type == Integer.class ){
511
writeLineEscaped( ""+((Integer)field.get( obj )).intValue());
513
}else if ( type == Boolean.class ){
515
writeLineEscaped( ""+((Boolean)field.get( obj )).booleanValue());
517
}else if ( type == long.class ){
519
writeLineEscaped( ""+field.getLong( obj ));
521
}else if ( type == boolean.class ){
523
writeLineEscaped( ""+field.getBoolean( obj ));
525
}else if ( type == byte.class ){
527
writeLineEscaped( ""+field.getByte( obj ));
529
}else if ( type == char.class ){
531
writeLineEscaped( ""+field.getChar( obj ));
533
}else if ( type == double.class ){
535
writeLineEscaped( ""+field.getDouble( obj ));
537
}else if ( type == float.class ){
539
writeLineEscaped( ""+field.getFloat( obj ));
541
}else if ( type == int.class ){
543
writeLineEscaped( ""+field.getInt( obj ));
545
}else if ( type == short.class ){
547
writeLineEscaped( ""+field.getShort( obj ));
549
}else if ( type == Long.class ){
551
writeLineEscaped( ""+field.get( obj ));
555
serialiseObject( field.get(obj), indent + " ", original_modifier_filter );
562
writeLineRaw( "</" + name + ">" );
567
}catch( Throwable e ){
569
Debug.printStackTrace( e );
571
throw( new RuntimeException( e.toString()));
574
cla = cla.getSuperclass();
2
* File : XMLRequestProcessor.java
3
* Created : 15-Mar-2004
6
* Azureus - a Java Bittorrent client
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details ( see the LICENSE file ).
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
package org.gudy.azureus2.ui.webplugin.remoteui.xml.server;
32
import java.lang.reflect.*;
34
import org.gudy.azureus2.core3.util.ByteFormatter;
35
import org.gudy.azureus2.core3.util.Debug;
36
import org.gudy.azureus2.core3.xml.simpleparser.*;
37
import org.gudy.azureus2.core3.xml.util.*;
38
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument;
39
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttribute;
40
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentException;
41
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNode;
42
import org.gudy.azureus2.pluginsimpl.remote.*;
48
protected RPRequestHandler request_handler;
49
protected SimpleXMLParserDocument request;
53
RPRequestHandler _request_handler,
54
RPRequestAccessController _access_controller,
61
request_handler = _request_handler;
63
writeLineRaw( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
66
writeLineRaw( "<RESPONSE>" );
70
request = SimpleXMLParserDocumentFactory.create( _request );
72
process( _client_ip , _access_controller);
74
}catch( Throwable e ){
76
Debug.printStackTrace( e );
78
if ( e instanceof SimpleXMLParserDocumentException ){
80
writeTag("ERROR", "Invalid XML Plugin request received - " + exceptionToString(e));
84
writeTag("ERROR", e );
92
writeLineRaw( "</RESPONSE>" );
101
RPRequestAccessController access_controller )
105
RPRequest req_obj = (RPRequest)deserialiseObject( request, RPRequest.class, "" );
107
req_obj.setClientIP( client_ip );
109
RPReply reply = request_handler.processRequest( req_obj, access_controller );
111
// void methods result in null return
113
if ( reply != null ){
115
Map props = reply.getProperties();
117
Iterator it = props.keySet().iterator();
119
while( it.hasNext()){
121
String name = (String)it.next();
122
String value = (String)props.get(name);
124
writeTag( name, value );
128
Object response = reply.getResponse();
130
// null responses are represented by no response
132
if ( response != null ){
134
serialiseObject( response, "", 0xffffffff );
137
}catch( RPException e ){
139
Debug.printStackTrace( e );
141
writeTag("ERROR", e );
152
writeLineRaw( "<ERROR>");
154
writeLineEscaped( exceptionToString(e));
156
if ( e instanceof RPException && e.getCause() != null ){
161
serialiseObject( e, " ", ~Modifier.PRIVATE );
163
writeLineRaw( "</ERROR>" );
170
Throwable cause = e.getCause();
172
if ( cause != null ){
174
String m = cause.getMessage();
181
return( cause.toString());
184
String m = e.getMessage();
191
return( e.toString());
196
SimpleXMLParserDocumentNode node,
200
// hack I'm afraid, when deserialising request objects we need to use the
201
// method to correctly deserialise parameters
203
String request_method = null;
205
if ( cla == RPRequest.class ){
207
request_method = node.getChild( "METHOD" ).getValue().trim();
211
// System.out.println(indent + "deser:" + cla.getName());
214
Object obj = cla.newInstance();
216
Field[] fields = cla.getDeclaredFields();
218
for (int i=0;i<fields.length;i++){
220
Field field = fields[i];
222
int modifiers = field.getModifiers();
224
if (( modifiers & ( Modifier.TRANSIENT | Modifier.STATIC )) == 0 ){
226
String name = field.getName();
228
Class type = field.getType();
230
SimpleXMLParserDocumentNode child = node.getChild( name );
232
// System.out.println( indent + " field:" + field.getName() + " -> " + child );
234
if ( child != null ){
236
if ( type.isArray()){
238
Class sub_type = type.getComponentType();
240
SimpleXMLParserDocumentNode[] entries = child.getChildren();
242
Object array = Array.newInstance( sub_type, entries.length );
244
// hack. for request objects we deserialise the parameters
245
// array by using the method signature
247
if ( request_method != null ){
251
String[] bits = new String[entries.length];
253
int method_pos = request_method.indexOf( '[' )+1;
255
for (int j=0;j<entries.length;j++){
257
int p1 = request_method.indexOf(',',method_pos);
261
bits[j] = request_method.substring( method_pos, request_method.length()-1).toLowerCase();
266
bits[j] = request_method.substring( method_pos, p1 ).toLowerCase();
272
for (int j=0;j<entries.length;j++){
274
SimpleXMLParserDocumentNode array_child = entries[j];
276
SimpleXMLParserDocumentAttribute index_attr = array_child.getAttribute("index");
278
String index_str = index_attr==null?null:index_attr.getValue().trim();
280
int array_index = index_str==null?j:Integer.parseInt(index_str);
282
String bit = bits[array_index];
284
String sub_value = array_child.getValue().trim();
286
if ( bit.equals("string")){
288
Array.set( array, array_index, sub_value );
290
}else if ( bit.equals("int")){
292
Array.set( array, array_index, Integer.valueOf( sub_value));
294
}else if ( bit.equals("boolean")){
296
Array.set( array, array_index, Boolean.valueOf( sub_value ));
298
}else if ( bit.equals("url")){
300
Array.set( array, array_index, new URL(sub_value));
302
}else if ( bit.equals("byte[]")){
304
Array.set( array, array_index, ByteFormatter.decodeString( sub_value ));
307
// see if its an object
309
SimpleXMLParserDocumentNode obj_node = array_child.getChild("OBJECT");
311
if ( obj_node != null ){
313
String oid_str = obj_node.getChild("_object_id").getValue().trim();
315
long oid = Long.parseLong( oid_str );
317
RPObject local_obj = RPObject._lookupLocal( oid );
319
Array.set( array, array_index, local_obj );
323
throw( new RuntimeException( "not implemented"));
330
for (int j=0;j<entries.length;j++){
332
SimpleXMLParserDocumentNode array_child = entries[j];
334
if ( sub_type == String.class ){
336
Array.set( array, j, child.getValue().trim());
340
throw( new RuntimeException( "not implemented"));
345
field.set( obj, array );
348
String value = child.getValue().trim();
350
if ( type == String.class ){
352
field.set( obj, value );
354
}else if ( type == long.class ){
356
field.setLong( obj,Long.parseLong( value ));
358
}else if ( type == boolean.class ){
360
throw( new RuntimeException( "not implemented"));
362
//field.set( obj, new Long(Long.parseLong( value )));
364
}else if ( type == byte.class ){
366
throw( new RuntimeException( "not implemented"));
368
//field.set( obj, new Long(Long.parseLong( value )));
370
}else if ( type == char.class ){
372
field.setChar( obj, value.charAt(0));
374
}else if ( type == double.class ){
376
field.setDouble( obj, Double.parseDouble( value));
378
}else if ( type == float.class ){
380
field.setFloat( obj, Float.parseFloat( value));
382
}else if ( type == int.class ){
384
field.setInt( obj, Integer.parseInt( value));
386
}else if ( type == short.class ){
388
field.setShort( obj, (Short.parseShort( value )));
390
}else if ( type == Long.class || type == long.class ){
392
field.set( obj, new Long(Long.parseLong( value )));
396
field.set( obj, deserialiseObject( child, type, indent + " " ));
405
}catch( Throwable e ){
407
Debug.printStackTrace( e );
409
throw( new RuntimeException( e.toString()));
417
int original_modifier_filter )
419
int modifier_filter = original_modifier_filter & (~(Modifier.TRANSIENT | Modifier.STATIC));
421
Class cla = obj.getClass();
423
// System.out.println(indent + "ser:" + cla.getName());
427
int len = Array.getLength( obj );
429
// byte[] -> hex chars
431
if ( cla.getComponentType() == byte.class ){
433
byte[] data = (byte[])obj;
435
writeLineEscaped( ByteFormatter.nicePrint( data, true ));
439
for (int i=0;i<len;i++){
441
Object entry = Array.get( obj, i );
444
writeLineRaw( "<ENTRY index=\""+i+"\">" );
448
serialiseObject( entry, indent+" ", original_modifier_filter);
454
writeLineRaw( "</ENTRY>");
462
if ( cla == String.class ){
464
writeLineEscaped( "" + (String)obj);
466
}else if ( cla == Integer.class ){
468
writeLineEscaped( "" + ((Integer)obj).intValue());
470
}else if ( cla == Boolean.class ){
472
writeLineEscaped( "" + ((Boolean)obj).booleanValue());
476
while( cla != null ){
478
Field[] fields = cla.getDeclaredFields();
480
for (int i=0;i<fields.length;i++){
482
Field field = fields[i];
484
int modifiers = field.getModifiers();
486
if ((( modifiers | modifier_filter ) == modifier_filter )){
488
String name = field.getName();
490
Class type = field.getType();
492
// System.out.println( indent + " field:" + field.getName() + ", type = " + type );
495
writeLineRaw( "<" + name + ">" );
499
if ( type == String.class ){
501
writeLineEscaped( (String)field.get( obj ));
503
}else if ( type == File.class ){
505
writeLineEscaped( "" + ((File)field.get( obj )).toString());
507
}else if ( type == Integer.class ){
509
writeLineEscaped( ""+((Integer)field.get( obj )).intValue());
511
}else if ( type == Boolean.class ){
513
writeLineEscaped( ""+((Boolean)field.get( obj )).booleanValue());
515
}else if ( type == long.class ){
517
writeLineEscaped( ""+field.getLong( obj ));
519
}else if ( type == boolean.class ){
521
writeLineEscaped( ""+field.getBoolean( obj ));
523
}else if ( type == byte.class ){
525
writeLineEscaped( ""+field.getByte( obj ));
527
}else if ( type == char.class ){
529
writeLineEscaped( ""+field.getChar( obj ));
531
}else if ( type == double.class ){
533
writeLineEscaped( ""+field.getDouble( obj ));
535
}else if ( type == float.class ){
537
writeLineEscaped( ""+field.getFloat( obj ));
539
}else if ( type == int.class ){
541
writeLineEscaped( ""+field.getInt( obj ));
543
}else if ( type == short.class ){
545
writeLineEscaped( ""+field.getShort( obj ));
547
}else if ( type == Long.class ){
549
writeLineEscaped( ""+field.get( obj ));
553
serialiseObject( field.get(obj), indent + " ", original_modifier_filter );
560
writeLineRaw( "</" + name + ">" );
565
}catch( Throwable e ){
567
Debug.printStackTrace( e );
569
throw( new RuntimeException( e.toString()));
572
cla = cla.getSuperclass();