Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

John Munsch http://www.johnmunsch.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

A Client For the XML-RPC Servlet

// When combined with the Apache XML-RPC library this code
// will let you call the servlet in the snippet "An XML-RPC
// Servlet". Of course, since XML-RPC is pretty ubiquitous
// you can also use this code to call servers in dozens of
// other languages as well.

   1  
   2  import java.util.Vector;
   3  
   4  import org.apache.commons.logging.Log;
   5  import org.apache.commons.logging.LogFactory;
   6  import org.apache.xmlrpc.XmlRpcClient;
   7  
   8  public class XMLRPCTestClient {
   9      private static final String serverAddress = 
  10  	"http://localhost:8080/lol/remoteapi";
  11      private static Log log = LogFactory.getLog(XMLRPCTestClient.class);
  12      
  13      /** Creates a new instance of XMLRPCTestClient */
  14      public XMLRPCTestClient(String address) {
  15          try {
  16              XmlRpcClient xmlrpc = new XmlRpcClient(address);
  17  
  18              Vector params = new Vector();
  19              params.addElement("Hello World! Hello!");
  20  
  21              try {
  22                  // this method returns a string
  23                  String result = (String) xmlrpc.execute("echo.echo", params);
  24                  System.out.println(result);
  25              } catch (Exception e) {
  26                  log.error("The remote procedure call failed.", e);
  27              }
  28          } catch (java.net.MalformedURLException mue) {
  29              log.error(
  30                  "The address given for the XML-RPC interface is bad: " + address);
  31          }
  32      }
  33  
  34      /**
  35       * @param args the command line arguments
  36       */
  37      public static void main(String[] args) {
  38          XMLRPCTestClient xmlRPCTestClient = new XMLRPCTestClient(
  39              serverAddress);
  40      }
  41  }

An XML-RPC Servlet

// This depends upon the Apache XML-RPC library.

   1  
   2  import java.io.IOException;
   3  import java.io.OutputStream;
   4  
   5  import javax.servlet.ServletConfig;
   6  import javax.servlet.ServletException;
   7  import javax.servlet.http.HttpServlet;
   8  import javax.servlet.http.HttpServletRequest;
   9  import javax.servlet.http.HttpServletResponse;
  10  
  11  import org.apache.commons.logging.Log;
  12  import org.apache.commons.logging.LogFactory;
  13  import org.apache.xmlrpc.XmlRpcServer;
  14  
  15  public class XmlRpcServlet extends HttpServlet {
  16      public class EchoHandler {
  17          public String echo(String input) {
  18              return input;
  19          }
  20      }
  21  	
  22      private XmlRpcServer server = new XmlRpcServer();
  23  
  24      private static Log log = LogFactory.getLog(XmlRpcServlet.class);
  25  
  26  	@Override
  27  	public void init(ServletConfig config) throws ServletException {
  28          server.addHandler("echo", new EchoHandler());
  29  	}
  30  
  31  	@Override
  32  	protected void doGet(HttpServletRequest request, 
  33  			HttpServletResponse response) throws ServletException, IOException {
  34          doPost(request, response);
  35  	}
  36  
  37  	@Override
  38  	protected void doPost(HttpServletRequest request, 
  39  			HttpServletResponse response) throws ServletException, IOException {
  40          byte[] result = server.execute(request.getInputStream());
  41          
  42          response.setContentType("text/xml");
  43          response.setContentLength(result.length);
  44          
  45          OutputStream output = response.getOutputStream();
  46          output.write(result);
  47          output.flush();
  48      }
  49  }


// The following needs to be added to your web.xml file to
// expose the servlet so it may be called remotely.
   1  
   2    <servlet>
   3      <servlet-name>XmlRpcServlet</servlet-name>
   4      <servlet-class>XmlRpcServlet</servlet-class>
   5    </servlet>
   6  
   7    <servlet-mapping>
   8      <servlet-name>XmlRpcServlet</servlet-name>
   9      <url-pattern>/remoteapi</url-pattern>        
  10    </servlet-mapping>    

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS