📄 xmlrequestprocessor2.java
字号:
/**
* Created on 10-Jan-2006
* Created by Allan Crooks
* Copyright (C) 2006 Aelitis, All Rights Reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package org.gudy.azureus2.ui.webplugin.remoteui.xml.server;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.gudy.azureus2.core3.util.Debug;
import org.gudy.azureus2.plugins.PluginInterface;
import org.gudy.azureus2.plugins.logging.Logger;
import org.gudy.azureus2.plugins.logging.LoggerChannel;
import org.gudy.azureus2.pluginsimpl.remote.MethodSignature;
import org.gudy.azureus2.pluginsimpl.remote.RPUtils;
import org.gudy.azureus2.pluginsimpl.remote.RPException;
import org.gudy.azureus2.pluginsimpl.remote.RPObject;
import org.gudy.azureus2.pluginsimpl.remote.RPRequest;
import org.gudy.azureus2.pluginsimpl.remote.RPRequestAccessController;
import org.gudy.azureus2.pluginsimpl.remote.RPRequestHandler;
import org.gudy.azureus2.pluginsimpl.remote.RPReply;
import org.gudy.azureus2.core3.xml.util.XMLElement;
import org.gudy.azureus2.core3.util.Constants;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNode;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentException;
import org.gudy.azureus2.core3.xml.simpleparser.SimpleXMLParserDocumentFactory;
import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttribute;
import org.gudy.azureus2.pluginsimpl.remote.rpexceptions.*;
/**
* Main methods here:
* <init>: Takes input and output stream and starts off the processing.
* Is responsible for parsing XML, writing data to output stream,
* and capturing all unexpected exceptions.
*
* process: Mainly involved in chaining together the main subfunctions.
* Will get RPRequest object from XML document, get RPReply
* returned and serialise reply into XMLElement object.
*
* deserialiseObject: Takes XML node and is involve in reconstructing the
* object's fields - usually just for RPRequest objects.
*
* serialiseObject: Takes any object (including primitive wrapper objects)
* and tries to generate their XML.
*
* deserialiseValue: Deserialises any object, either normal Java types or
* resolves it to be RPObject instances (or their
* delegation). No other calls are performed, we don't
* deserialise any fields.
*/
public class XMLRequestProcessor2 {
protected RPRequestHandler request_handler;
protected SimpleXMLParserDocument request;
protected boolean serialise_debug;
protected boolean deserialise_debug;
protected LoggerChannel logger;
protected XMLRequestProcessor2(RPRequestHandler _request_handler, RPRequestAccessController _access_controller, String _client_ip, InputStream _request, OutputStream _reply, PluginInterface pi, LoggerChannel rp_channel, boolean serialise_debug, boolean deserialise_debug, boolean space_out_xml) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new OutputStreamWriter(_reply, Constants.DEFAULT_ENCODING));
}
catch(UnsupportedEncodingException e){
Debug.printStackTrace(e);
pw = new PrintWriter(_reply);
}
this.serialise_debug = serialise_debug;
this.deserialise_debug = deserialise_debug;
this.request_handler = _request_handler;
this.logger = rp_channel;
pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
XMLElement response_xml = new XMLElement("RESPONSE", true);
try{
this.request = SimpleXMLParserDocumentFactory.create( _request);
process(_client_ip , _access_controller, response_xml, pi);
}
catch(Throwable e){
Debug.printStackTrace(e);
response_xml.clear();
RPException rpe = null;
if (e instanceof SimpleXMLParserDocumentException) {
rpe = new RPMalformedXMLException(e);
}
else if (e instanceof RPException) {
rpe = (RPException)e;
}
else {
rpe = new RPInternalProcessException(e);
}
response_xml.addContent(describeError(rpe));
}
finally{
response_xml.printTo(pw, space_out_xml);
pw.flush();
pw.close();
}
}
protected void process(String client_ip, RPRequestAccessController access_controller, XMLElement response_xml, PluginInterface pi) throws Exception {
debug_in("About to deserialise the RPRequest");
RPRequest req_obj = (RPRequest)deserialiseObject(this.request, RPRequest.class);
log_general("REQUEST: " +
"method=" + req_obj.getMethod() + ", " +
"object=" + req_obj.getObject());
req_obj.setClientIP(client_ip);
req_obj.setPluginInterface(pi);
RPReply reply = request_handler.processRequest(req_obj, access_controller);
debug_out("About to serialise the RPReply");
Map props = reply.getProperties();
Iterator it = props.entrySet().iterator();
XMLElement response_attr = null;
Map.Entry mapping = null;
while(it.hasNext()) {
mapping = (Map.Entry)it.next();
response_attr = new XMLElement((String)mapping.getKey());
response_attr.addContent((String)mapping.getValue());
response_xml.addContent(response_attr);
}
Object response = null;
try {
response = reply.getResponse();
}
/* We don't need to catch all exceptions, we can just let them
propogate. */
catch (RPException e) {
debug_out("RPException occurred - possibly occurred during the method invocation", e);
log_general("RESPONSE (ERROR): " +
((e.getCause() == null) ? e : e.getCause()));
Debug.printStackTrace(e);
response_xml.clear();
response_xml.addContent(describeError(e));
return;
}
if (RPUtils.hasDescriptiveToStringMethod(response)) {
log_general("RESPONSE: " + response);
}
else {
log_general("RESPONSE: value=" + response + ", type=" + RPUtils.describeObject(response));
}
if (response != null) {
if (response.getClass().isArray()) {
response_xml.setAutoOrdering(false);
}
serialiseObject(response, 0xFFFFFFFF, response_xml);
}
}
public XMLElement describeError(RPException rpe) {
XMLElement xe = new XMLElement("ERROR", true);
Class c = rpe.getErrorClass();
if (c != null) {
xe.addAttribute("class", RPUtils.getName(c));
}
String type = rpe.getRPType();
if (type != null) {
xe.addAttribute("type", type);
}
xe.addContent(rpe.getSerialisationMessage());
Throwable t = rpe.getSerialisableObject();
try {
if (t != null) {
serialiseObject(t, ~Modifier.PRIVATE, xe);
}
}
catch (RuntimeException re) {
/**
* Only uncheck this comment if you're a developer. Of course, only
* developers would actually be uncommenting this line. ;)
*/
// throw re;
Debug.out("Error serialising error object.");
Debug.printStackTrace(re);
}
return xe;
}
protected Object deserialiseObject(SimpleXMLParserDocumentNode node, Class cla) throws Exception {
/**
* hack I'm afraid, when deserialising request objects we need to use
* the method to correctly deserialise parameters
*/
String request_method = null;
if ( cla == RPRequest.class ){
request_method = node.getChild( "METHOD" ).getValue().trim();
}
try{
debug_in("Beginning deserialisation of " + RPUtils.getName(cla) + " instance.");
Object obj = cla.newInstance();
Field[] fields = cla.getDeclaredFields();
for (int i=0;i<fields.length;i++) {
Field field = fields[i];
int modifiers = field.getModifiers();
// Field is either transient and / or static, so skip it.
if ((modifiers & (Modifier.TRANSIENT | Modifier.STATIC)) !=0)
continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -