Ahojte, nevim, zda s tim tu nekdo bude mit zkusenosti, ale zkusim to. Snazim se rozchodit xmlrpc server-klient (server jako servlet na webu a klient jako klasicka desktopova (swing) aplikace). Podle manualu http://ws.apache.org/xmlrpc/ chci provest server jako servlet. Mel bych tedy vytvorit nejakou tridu, kterou pak budu volat (Calculator), tady je muj kod:
package services;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int sub(int a, int b) {
return a - b;
}
}
Dale bych mel vytvorit soubor XmlRpcServlet.properties, kam bych mel umistit cestu k me tride Calculator. Po chvilce googleni jsem ale zjistil, ze je mozne to udelat i pridanim handleru v kodu serveru (to tedy ukazu pozdeji).
Mel bych pridat nejake radky do meho web.xml, pridal jsem tam tedy
<servlet>
<servlet-name>EOTService</servlet-name>
<servlet-class>services.EOTService</servlet-class>
<init-param>
<param-name>enabledForExtensions</param-name>
<param-value>true</param-value>
<description>Sets, whether the servlet supports vendor extensions for XML-RPC.</description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>EOTService</servlet-name>
<url-pattern>/service</url-pattern>
</servlet-mapping>
Jak je z tohoto videt, nepouzil jsem pro zpracovani tridu org.apache.xmlrpc.webserver.XmlRpcServlet, ale vytvoril si svoji services.EOTService, ktera vypada takto:package services;
import org.apache.xmlrpc.*;
import org.apache.xmlrpc.common.*;
import org.apache.xmlrpc.server.*;
import org.apache.xmlrpc.webserver.*;
public class EOTService extends XmlRpcServlet {
private boolean isAuthenticated(String pUserName, String pPassword) {
return pUserName.equals("username") && pPassword.equals("password");
}
protected XmlRpcHandlerMapping newXmlRpcHandlerMapping() throws XmlRpcException {
PropertyHandlerMapping mapping = (PropertyHandlerMapping) super.newXmlRpcHandlerMapping();
AbstractReflectiveHandlerMapping.AuthenticationHandler handler =
new AbstractReflectiveHandlerMapping.AuthenticationHandler() {
public boolean isAuthorized(XmlRpcRequest pRequest) {
XmlRpcHttpRequestConfig config = (XmlRpcHttpRequestConfig) pRequest.getConfig();
return isAuthenticated(config.getBasicUserName(), config.getBasicPassword());
}
};
mapping.setAuthenticationHandler(handler);
try {
mapping.addHandler("Calculator", Class.forName("services.Calculator"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return mapping;
}
}
a zde je prave radek: mapping.addHandler("Calculator", Class.forName("services.Calculator"));, ktery za me udela to, co by udelal soubor XmlRpcServlet.properties. Kdyz vse spustim, a v klientovi zadam adresu http://localhost:8080/eot/service, vrati miHTTP method GET is not supported by this URL
coz by asi melo byt v poradku.
Takze zbyva jen klient. Ten je sice jednoduchy, ale podle manualu by mel fungovat.
try {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/service"));
config.setBasicUserName("username");
config.setBasicPassword("password");
config.setBasicEncoding("UTF-8");
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Object[] params = new Object[]{new Integer(33), new Integer(9)};
Integer result = (Integer)client.execute("Calculator.add", params);
System.out.println("33 + 9 = " + result);
} catch (java.net.MalformedURLException e) {
System.out.println("MalformedURLException: " + e.getMessage());
} catch (org.apache.xmlrpc.XmlRpcException e) {
e.printStackTrace(System.out);
}
Jenze nefunguje. Kdyz to spustim, dostavam vyjimku:org.apache.xmlrpc.client.XmlRpcHttpTransportException: HTTP server returned unexpected status: Not Found
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.getInputStream(XmlRpcSunHttpTransport.java:94)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:152)
at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126)
Nevite nekdo, cim by to mohlo byt? Dekuji :-)