Tag Archives: XML

How to Call a REST Service with Apache XML-RPC

import static java.lang.System.out;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

@SuppressWarnings({"rawtypes", "unchecked"})
public final class ApacheXmlRpcSample {

  public static void main(final String[] args) throws Exception {
    final XmlRpcClient client = new XmlRpcClient();

    final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

    config.setServerURL(new URL("http://localhost/sample"));

    client.setConfig(config);

    final Map paramMap = new HashMap();

    if (args[0] != null)
      paramMap.put("param1", args[0]);

    final Map result = (Map) client.execute("sample", new Object[] { paramMap });

    for (final Object key : result.keySet())
      out.println(key + " = " + result.get(key));

  }

}
Advertisements

Evaluating XPath Expressions in Custom Processors in Apache Camel

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.builder.xml.Namespaces;

public final class SampleRouteBuilder extends RouteBuilder {
  private static final Namespaces NS = new Namespaces("sample", "http://fernandoribeiro.eti.br/sample/camel");

  @Override
  public void configure() throws Exception {
    from(...).process(new Processor() {

      public void process(final Exchange exchange) {
        final String str = (String) NS.xpath("/sample:Element", String.class).evaluate(exchange);

        ...
      }

    });
  }

}

Creating SOAP Faults in Apache ServiceMix

WSDL

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://www.fernandoribeiro.eti.br/sample/servicemix" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.fernandoribeiro.eti.br/sample/servicemix" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <xs:schema elementFormDefault="qualified" targetNamespace="http://www.fernandoribeiro.eti.br/sample/servicemix" xmlns:xs="http://www.w3.org/2001/XMLSchema">
            ...
      <xs:element name="SampleFault"> <!-- great opportunity for a common type -->
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Message" type="xs:string" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </wsdl:types>
  ...
  <wsdl:message name="SampleFault">
    <wsdl:part name="SampleFault" element="tns:SampleFault" />
  </wsdl:message>
  <wsdl:portType name="SamplePortType">
    <wsdl:operation name="SampleOperation">
      ...
      <wsdl:fault name="SampleFault" message="tns:SampleFault" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="SampleSoapBinding" type="tns:SamplePortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
    ...
    <wsdl:operation name="SampleOperation">
      <soap:operation ... />
      ...
      <wsdl:fault name="SampleFault">
        <soap:fault name="SampleFault" use="literal" />
      </wsdl:fault>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="SampleService">
    <wsdl:port name="SamplePort" binding="tns:SampleSoapBinding">
      <soap:address location="http://localhost/servicemixsample/SampleService/" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

RouteBuilder

package br.eti.fernandoribeiro.sample.servicemix;

import org.apache.camel.builder.RouteBuilder;

public final class SampleRouteBuilder extends RouteBuilder {

  @Override
  public void configure() throws Exception {
    onException(Exception.class).process(new SampleFaultProcessor());
  }

}

Processor

package br.eti.fernandoribeiro.ws.sample.servicemix;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public final class SampleFaultProcessor implements Processor {
  private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();

  private DocumentBuilder builder;

  public SampleFaultProcessor() throws ParserConfigurationException {
    builder = FACTORY.newDocumentBuilder();
  }

  public void process(final Exchange exch) throws ParserConfigurationException {
    final Document doc = builder.newDocument();

    final Element root = doc.createElementNS("http://www.fernandoribeiro.eti.br/sample/servicemix", "SampleFault");

    final Element msg = doc.createElementNS("http://www.fernandoribeiro.eti.br/sample/servicemix", "Message");

    msg.setTextContent(exch.getException().getMessage());

    root.appendChild(msg);

    doc.appendChild(root);

    exch.getFault().setBody(doc);
  }

}

Specializing Complex Types with Mixed Content in JAXB

XML Schema

<xs:complexType name="Param" abstract="true" mixed="true">
  <xs:sequence>
    <xs:any minOccurs="0" maxOccurs="unbounded" />
  </xs:sequence>
</xs:complexType>
<xs:complexType name="ResultSetParam" mixed="true">
  <xs:complexContent>
    <xs:restriction base="tns:Param">
      <xs:sequence>
        <xs:element name="Row" type="tns:Row" minOccurs="0" maxOccurs="unbounded" />
      </xs:sequence>
    </xs:restriction>
  </xs:complexContent>
</xs:complexType>

Java

Param.java

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAnyElement;
import java.xml.bind.annotation.XmlMixed;

public abstract class Param {
  @XmlMixed
  @XmlAnyElement(lax = true)
  protected List content;

  public List getContent() {
    return new ArrayList();
  }

}

<strong><i>ResultSetParam.java</i></strong>

public class ResultSetParam extends Param {
}

Refer to 6.12.5 and 6.12.7 sections of the spec for details.