Ich möchte XML-RPC mit Wicket machen!
XmlRpcPage.java
public class XmlRpcPage extends WebPage {
private static final long serialVersionUID = 1L;
public XmlRpcPage(PageParameters params) {
super(params);
setStatelessHint(true);
Session.get().bind();
HttpServletRequest request = (HttpServletRequest)getRequestCycle().getRequest().getContainerRequest();
HttpServletResponse response = (HttpServletResponse)getRequestCycle().getResponse().getContainerResponse();
getRequestCycle().replaceAllRequestHandlers(new IRequestHandler() {
@Override
public void respond(IRequestCycle requestCycle) {
try {
processXmlRpcRequest(request, response);
} catch (ServletException | IOException e) {
throw new WicketRuntimeException(e);
}
}
@Override
public void detach(IRequestCycle requestCycle) {
}
});
}
/**
* Get XML-RPC server
* @return
*/
protected XmlRpcServletServer getXmlRpcServer() {
return ((WicketApplication)getApplication()).getXmlRpcServer();
}
/**
* Process XML-RPC request
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void processXmlRpcRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
getXmlRpcServer().execute(request, response);
}
@Override
public Markup getAssociatedMarkup() {
return null;
}
@Override
protected void configureResponse(WebResponse response) {
final String encoding = getApplication().getRequestCycleSettings().getResponseRequestEncoding();
response.setContentType("application/xhtml+xml; charset=" + encoding);
}
}
WicketApplication.java
protected XmlRpcServletServer xmlRpcServer = null;
protected XmlRpcServerConfigImpl xmlRpcConfig = null;
//Konstrukteur
public WicketApplication() {
super();
xmlRpcServer = new XmlRpcServletServer();
xmlRpcConfig = new XmlRpcServerConfigImpl();
}
WicketApplication.java
/*
* Initialize xmlrpc server
*/
public void initXmlRpc() {
/* configure xml-rpc server */
xmlRpcServer.setTypeFactory(new CustomTypeFactory(xmlRpcServer));
//CustomTypeConverterFactory ist XML-Fabrik zur Typumwandlung mit Rpc
xmlRpcServer.setTypeConverterFactory(new CustomTypeConverterFactory());
xmlRpcConfig = (XmlRpcServerConfigImpl)this.xmlRpcServer.getConfig();
xmlRpcConfig.setBasicEncoding(XmlRpcConfigImpl.UTF8_ENCODING);
xmlRpcConfig.setEnabledForExceptions(true);
xmlRpcConfig.setEnabledForExtensions(true);
PropertyHandlerMapping mapping = createXmlRpcHandlerMapping();
this.xmlRpcServer.setHandlerMapping(mapping);
}
CustomTypeFactory * ist eine Erweiterung von org.apache.xmlrpc.common.TypeFactoryImpl
CustomTypeConverterFactory * ist eine Erweiterung von org.apache.xmlrpc.common.TypeConverterFactoryImpl
Xml-Rpc-Zuordnungsmethode
WicketApplication.java
/**
* XML-Generierung der Property-Handler-Map für RPC
* @return
*/
private PropertyHandlerMapping createXmlRpcHandlerMapping() {
PropertyHandlerMapping mapping = new PropertyHandlerMapping();
mapping.setRequestProcessorFactoryFactory(new StatelessProcessorFactoryFactory());
mapping.setTypeConverterFactory(new CustomTypeConverterFactory());
try {
mapping.load(getClass().getClassLoader(), "xmlrpc.properties");
} catch (XmlRpcException | IOException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
logger.error("ERROR:\n"+sw.toString());
throw new WicketRuntimeException(e);
}
return mapping;
}
xmlrpc.properties
SampleService=my.api.xmlrpc.service.impl.SampleServiceImpl
WicketApplication.java
// initialize XML-RPC
initXmlRpc();
WicketApplication.java
// xml-rpc base
mount(new MountedMapper("/XmlRpc", XmlRpcPage.class));
TestSampleXmlRpc.java
public static void main(String[] args) throws Exception {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://localhost:8080/sample/XmlRpc"));
config.setEnabledForExtensions(true);
config.setEnabledForExceptions(true);
XmlRpcClient client = new XmlRpcClient();
client.setTransportFactory(new XmlRpcLoggingTransportFactory(client));
client.setTypeFactory(new CustomTypeFactory(client));
client.setConfig(config);
Vector<Object> arg = new Vector<Object>();
arg.add("some.parameter");
Object response = client.execute("SampleService.sampleMethod", arg);
System.out.println("Response: " + response);
}
Wenn Sie Fragen haben, können Sie sich gerne an uns wenden. Versuchen Sie zur Fehlerbehandlung ~ catch fängt alles ab und gibt das Ergebnis als Rückgabewert zurück. (Ich dachte, ich würde es später schreiben, aber ich habe mir nicht die Mühe gemacht zu schreiben ...)
Recommended Posts