<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>TechJava&#187; TechJava &#8211; Articles tagged by web service</title>
	<atom:link href="http://www.techjava.de/topics/tag/web-service/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techjava.de</link>
	<description>Journal on Java Technology</description>
	<lastBuildDate>Thu, 17 Jun 2010 10:41:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Exposing Functionality using Web Services and JEE</title>
		<link>http://www.techjava.de/topics/2010/01/functionality-web-services-jee/</link>
		<comments>http://www.techjava.de/topics/2010/01/functionality-web-services-jee/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 11:11:22 +0000</pubDate>
		<dc:creator>Simon Zambrovski</dc:creator>
				<category><![CDATA[enterprise systems]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[bean]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[ejb3]]></category>
		<category><![CDATA[java 5]]></category>
		<category><![CDATA[jee]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[wsdl]]></category>

		<guid isPermaLink="false">http://www.techjava.de/?p=554</guid>
		<description><![CDATA[
Abstract
About two years ago, I published an 
article about Exposing the Functionality implemented in Stateless Session Beans (EJB 2.1) using Web Services. J2EE 1.4 times are over and the new version of the Java Enterprise framework, called Java Enterprise Edition 5 (JavaEE 5, or simply JEE) has emerged. In this article the same business scenario [...]]]></description>
			<content:encoded><![CDATA[<h2>
<a  href="http://www.techjava.de/wp-content/uploads/valve.jpg" onclick="javascript:pageTracker._trackPageview('/downloads/wp-content/uploads/valve.jpg');" ><img style="float: right; margin: 5px;" title="valve" src="http://www.techjava.de/wp-content/uploads/valve-150x150.jpg" alt="" width="150" height="150" /></a>Abstract</h2>
<p>About two years ago, I published an 
<a  href="http://www.techjava.de/topics/2008/02/ws4j2ee14/">article</a> about Exposing the Functionality implemented in Stateless Session Beans (EJB 2.1) using Web Services. J2EE 1.4 times are over and the new version of the Java Enterprise framework, called Java Enterprise Edition 5 (JavaEE 5, or simply JEE) has emerged. In this article the same business scenario is repeated in the new framework. </p>
<h2>Requirements</h2>
<p>Before we dive into code examples, some software is required. The good news about the software is, that it also evolved over time. Here is what we use:</p>
<ul>
<li>Sun&#8217;s Java 6 SDK</li>
<li>JBoss AS 5.1.0 GA for JDK6</li>
<li>Eclipse Galileo 3.5.1 for JEE development</li>
</ul>
<p><span id="more-554"></span><br />
Some words about the used software. It is important to get the JDK6-compiled version of the JBoss Application Server. There are some issues with running the version compiled with Java 5 on JDK6, since JDK6 has its own JAX-WS implementation which causes problems with JBoss (affected should be all versions of JBoss &lt; 5.2. Currently it seems like version 5.2 will be compiled with JDK6 by default.) If you launch JBoss from Eclipse, make sure to add the <code>-Djava.endorsed.dirs=PATH_TO_JBOSS/jboss-5.1.0.GA/lib/endorsed</code> as a VM argument, if launched from the command line, please add it as an option inside of the <code>run.conf.bat</code> file:</p>
<pre class="brush: bash;">
set &quot;JAVA_OPTS=%JAVA_OPTS% -Djava.endorsed.dirs=$JBOSS_HOME/lib/endorsed&quot;
</pre>
<p>Otherwise you will get the following exception:</p>
<pre class="brush: java;">java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage</pre>
<p>After installation (unpacking) of Eclipse, make sure to configure the JDK (under <strong>Preferences &gt; Java &gt; Installed JREs</strong>) and the JBoss instance (<strong>Preferences &gt; Server &gt; Runtime Environment</strong>).</p>
<h2>Use Case</h2>
<p>In order to demonstrate the technology by example, imagine the following use case. The system under construction is capable of providing measurements of some sensors. The sensors are identified by numbers and can be queried by the user by providing the timespan of measurements. As a result, the systems delivers the set of measurements for the given timespan with one measurement per minute but at most sixty measurements. Every measurement contains the id of sensor from which it has been recorded, the timestamp, the value as a byte array, the measurement unit and finally a flag whether the measurement exceeds the limits or specified alarm values.</p>
<h2>Implementation</h2>
<p>We start the implementation with the creation of a JEE Session Bean and then expose the functionality using a second Facade bean. In doing so we follow the EJB 3 specification.</p>
<h3>Eclipse for Java EE development</h3>
<p>The plugins in Eclipse for Java EE provide massive support and foster the development by deep integration withe JEE servers. Instead of a simple Java Project, start with an <strong>Enterprise Application Project</strong>. Then, create a new <strong>EJB Project</strong> and add it to the created Enterprise Application Project. In order to deploy the resulting application to the server, right-click on the enterprise project and select <strong>Run As &gt; Run on Server </strong>.</p>
<p>To check the deployment on the application server, use the JBoss Consoles. These can be accessed via 
<a  href="http://localhost:8080/" onclick="javascript:pageTracker._trackPageview('/external/localhost/');" >http://localhost:8080/</a> for the locally installed JBoss. An additional Web Service Console is accessible via 
<a  href="http://localhost:8080/jbossws/" onclick="javascript:pageTracker._trackPageview('/external/localhost/jbossws/');" >http://localhost:8080/jbossws/</a>.</p>
<h3>Implementing the Functionality</h3>
<p>In order to implement the functionality in a stateless session bean, we start with the creation of the business interface.</p>
<pre class="brush: java;">
/**
 * Defines the sensor manager functionality
 */
public interface SensorManager
{
	/**
	 * Retrieve sensor data
	 * @param sensorId id of the sensor
	 * @param timespan the measurement period
	 * @return a list of measurements in the given period
	 * @throws SensorManagerException on any kinds of errors
	 */
	public List&lt;Measurement&gt; getSensorData(SensorID sensorId, Timespan timespan) throws SensorManagerException;
}
</pre>
<p>The POJOs <code>Measurement</code>, <code>SensorID</code> and <code>Timespan</code> are used to show the usage of custom user types:</p>
<pre class="brush: java;">
public class Measurement
{
	private SensorID sensorID;
	private Date timestamp;
	private boolean critical;
	private byte[] value;
	private String unit;
...
// getters and setters
}
....
public class SensorID
{
	private long sensorId;
...
// getters and setters
}

public class Timespan
{
	private Date start;
	private Date end;
...
// getters and setters
}
</pre>
<p>After the creation of the business interface, let us create the actual Session Bean. The class is annotated with the <code>@javax.ejb.Stateless</code> annotation in order to indicate that the bean is a Stateless Session Bean. Since it implements the business interface (and only it), the container will be able to deduce that the given interface is the Local Business interface: this is just one of many examples of the Configurations by Exception in EJB 3. In order to be able to reference the bean from other beans, we annotate the binding of the local interface to a specific JNDI name using the vendor-specific annotation <code>org.jboss.ejb3.LocalBinding</code>.</p>
<pre class="brush: java;">
@Stateless
@LocalBinding(jndiBinding=&quot;de.techjava.sensor/SensorManager&quot;)
public class SensorManagerBean implements SensorManager
{
	/** Logging facility */
	protected static Logger LOG = Logger.getLogger(SensorManagerBean.class);
	/** Max number of returned values */
	private static final long MAX_DURATION = 60;

	/**
	 * Implementation of the business method
	 */
	public List&lt;Measurement&gt; getSensorData(SensorID sensorId, Timespan timespan) throws SensorManagerException
	{
		LOG.debug(&quot;Entering getSensorData()&quot;);
		if (sensorId == null || timespan == null || timespan.getStart() == null || timespan.getStart() == null)
		{
			throw new SensorManagerException(&quot;Missing a mandatory parameter, that was null (not set)&quot;);
		} else if (!timespan.getStart().before(timespan.getEnd()))
		{
			throw new SensorManagerException(&quot;The timespan is defined by the start that should be before end&quot;);
		}

		List&lt;Measurement&gt; measurements = new LinkedList&lt;Measurement&gt;();
		for (long i = 0; i &lt; getNumberOfElements(timespan); i++)
		{
			Date date = new Date();
			date.setTime(timespan.getStart().getTime() + i * 1000 * 60);
			if (date.after(timespan.getEnd()))
				break;
			Measurement measurement = createMeasurement(sensorId, date, i);
			measurements.add(measurement);
		}

		LOG.debug(&quot;Leaving getSensorData(). Returning &quot; + measurements.size() + &quot; values.&quot;);
		return measurements;
	}

	/**
	 * Retrieves the number of measurements in timespan
	 */
	private long getNumberOfElements(Timespan timespan) { ... }

	/**
	 * Creates a measurement instance, a dummy implementation
	 */
	private Measurement createMeasurement(SensorID sensorId, Date timestamp, long number) { ... }
}
</pre>
<h2>Web Service Definition</h2>
<p>The quality of the web service interface is an important design consideration. As described in 
<a  href="http://www.techjava.de/topics/2008/02/ws4j2ee14/">the previous post</a>, in many cases it is a good idea to create it by hand instead of generation by a tool. In this particular case, we create another annotated Stateless Session Bean, which is used as a Facade and contains all Web Service-specific settings. From this Bean we generate the WSDL and then tune it in order to show the capability of the customization. In doing so we try to match the WSDL from the previous example as close as possible.</p>
<p>Firstly, we create the business interface and the bean class, then annotate it and finally add the code delegating the Web Service request to the SensorManager.</p>
<pre class="brush: java;">
public interface MeasurementProviderFacade
{
	public List&lt;Measurement&gt; getSensorData(SensorID id, Timespan timespan) throws SensorDataOperationFault;
}

...

@Stateless
public class MeasurementProviderFacadeBean implements MeasurementProviderFacade
{
	public List&lt;Measurement&gt; getSensorData(SensorID id, Timespan timespan) throws SensorDataOperationFault
	{
	...
	// implementation code
	}
}
</pre>
<p>In order to have a Session Bean exposed as a Web Service, the JAX-WS specification defines a set of annotations. The most important two are <code>@javax.jws.WebMethod</code> and <code>@javax.jws.WebService</code>. The <code>@WebService</code> annotation is used to mark the class to be exposed e.G.</p>
<pre class="brush: java;">
@Stateless
@WebService(
	serviceName = &quot;MeasurementProviderService&quot;,
	name = &quot;MeasurementProviderPortType&quot;,
	portName = &quot;MeasurementProviderPort&quot;,
	targetNamespace = &quot;http://www.techjava.de/2010/ws4jee/measurement/&quot;
)
public class MeasurementProviderFacadeBean implements MeasurementProviderFacade
{
	@WebMethod(
		operationName=&quot;GetSensorDataOperation&quot;,
		action=&quot;http://www.techjava.de/2010/ws4jee/measurement/GetSensorDataOperation&quot;
	)
	public List&lt;Measurement&gt; getSensorData(SensorID id, Timespan timespan) throws SensorDataOperationFault { ... }
}
</pre>
<p>All the attributes of the annotation are optional and their values will be derived from the classname and the name of the business interface. The default target namespace for the service definition is derived from the package name, the annotated class is located in. By default, all public methods of the annotated class will be exposed as WSDL operations, with operation&#8217;s name derived from the method name. Using the <code>@WebMethod</code> annotation, the name of the WSDL operation and the SOAP action can be changed.</p>
<p>The careful reader might have observed that the <code>getSensorData</code>-method is throwing an exception. In order to map the exception to a SOAP-Fault, the <code>javax.xml.ws.WebFault</code> class-level annotation can be used. The <code>faultBean</code> attribute is used to provide the full-qulified class name of the exception.</p>
<pre class="brush: java;">
...
@WebFault(
	name=&quot;GetSensorDataFault&quot;,
	targetNamespace=&quot;http://www.techjava.de/2010/ws4jee/measurement/&quot;,
	faultBean=&quot;de.techjava.sensor.bean.SensorDataOperationFault&quot;)
public class MeasurementProviderFacadeBean implements MeasurementProviderFacade { ... }
</pre>
<p>In order to increase the quality of the WSDL further, the method parameters and return type can be annotated with <code>javax.jws.WebParam</code> and <code>javax.jws.WebResult</code> respectively, so the method signature becomes a little unreadable. In order not to copy the long string containing the taget namespace a String constant can be used.</p>
<pre class="brush: java;">
...
	public final static String TYPES = &quot;http://www.techjava.de/2010/ws4jee/measurement/types/&quot;;
...
	@WebResult(name=&quot;measurement&quot;, targetNamespace=TYPES)
	public List&lt;Measurement&gt; getSensorData(
		@WebParam(name=&quot;sensor-id&quot;, targetNamespace=TYPES) SensorID id,
		@WebParam(name=&quot;timespan&quot;, targetNamespace=TYPES) Timespan timespan) throws SensorDataOperationFault	{ ... }
</pre>
<p>Finally, in order to force the marshaller / unmarshaller to map the custom data types (<code>Measurement</code>, <code>SensorID</code> and <code>Timespan</code>) to belong to the same namespace, the JAX-B class-level annotation <code>javax.xml.bind.annotation.XmlType</code> has to be used. By default, the datatypes will be mapped to XML Schema types according to their package name and class name.</p>
<pre class="brush: java;">
@XmlType(namespace = &quot;http://www.techjava.de/2010/ws4jee/measurement/types/&quot;, name = &quot;TimeSpan&quot;, propOrder = { &quot;start&quot;, &quot;end&quot; })
public class Timespan
{
	private Date start;
	private Date end;
</pre>
<p>If deployed to the JBoss, the resulting WSDL looks as following:</p>
<pre class="brush: xml; collapse: true; light: false; toolbar: true;">
&lt;definitions name='MeasurementProviderService' targetNamespace='http://www.techjava.de/2010/ws4jee/measurement/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:ns1='http://www.techjava.de/2010/ws4jee/measurement/types/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://www.techjava.de/2010/ws4jee/measurement/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'&gt;
 &lt;types&gt;
  &lt;xs:schema targetNamespace='http://www.techjava.de/2010/ws4jee/measurement/types/' version='1.0' xmlns:tns='http://www.techjava.de/2010/ws4jee/measurement/types/' xmlns:xs='http://www.w3.org/2001/XMLSchema'&gt;
   &lt;xs:element name='measurement' type='tns:Measurement'/&gt;
   &lt;xs:element name='sensor-id' type='tns:sensor-id'/&gt;
   &lt;xs:element name='timespan' type='tns:TimeSpan'/&gt;
   &lt;xs:complexType name='sensor-id'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element name='sensorId' type='xs:long'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
   &lt;xs:complexType name='TimeSpan'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element minOccurs='0' name='start' type='xs:dateTime'/&gt;
     &lt;xs:element minOccurs='0' name='end' type='xs:dateTime'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
   &lt;xs:complexType name='Measurement'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element name='critical' type='xs:boolean'/&gt;
     &lt;xs:element minOccurs='0' name='sensorID' type='tns:sensor-id'/&gt;
     &lt;xs:element minOccurs='0' name='timestamp' type='xs:dateTime'/&gt;
     &lt;xs:element minOccurs='0' name='unit' type='xs:string'/&gt;
     &lt;xs:element minOccurs='0' name='value' type='xs:base64Binary'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  &lt;xs:schema targetNamespace='http://www.techjava.de/2010/ws4jee/measurement/' version='1.0' xmlns:ns1='http://www.techjava.de/2010/ws4jee/measurement/types/' xmlns:tns='http://www.techjava.de/2010/ws4jee/measurement/' xmlns:xs='http://www.w3.org/2001/XMLSchema'&gt;
   &lt;xs:import namespace='http://www.techjava.de/2010/ws4jee/measurement/types/'/&gt;
   &lt;xs:element name='GetSensorDataOperation' type='tns:GetSensorDataOperation'/&gt;
   &lt;xs:element name='GetSensorDataOperationResponse' type='tns:GetSensorDataOperationResponse'/&gt;
   &lt;xs:element name='SensorDataOperationFault' type='tns:SensorDataOperationFault'/&gt;
   &lt;xs:complexType name='GetSensorDataOperation'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element minOccurs='0' ref='ns1:sensor-id'/&gt;
     &lt;xs:element minOccurs='0' ref='ns1:timespan'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
   &lt;xs:complexType name='GetSensorDataOperationResponse'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element maxOccurs='unbounded' minOccurs='0' ref='ns1:measurement'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
   &lt;xs:complexType name='SensorDataOperationFault'&gt;
    &lt;xs:sequence&gt;
     &lt;xs:element minOccurs='0' name='message' type='xs:string'/&gt;
    &lt;/xs:sequence&gt;
   &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
 &lt;/types&gt;
 &lt;message name='MeasurementProviderPortType_GetSensorDataOperation'&gt;
  &lt;part element='tns:GetSensorDataOperation' name='GetSensorDataOperation'&gt;&lt;/part&gt;
 &lt;/message&gt;
 &lt;message name='SensorDataOperationFault'&gt;
  &lt;part element='tns:SensorDataOperationFault' name='SensorDataOperationFault'&gt;&lt;/part&gt;
 &lt;/message&gt;
 &lt;message name='MeasurementProviderPortType_GetSensorDataOperationResponse'&gt;
  &lt;part element='tns:GetSensorDataOperationResponse' name='GetSensorDataOperationResponse'&gt;&lt;/part&gt;
 &lt;/message&gt;
 &lt;portType name='MeasurementProviderPortType'&gt;
  &lt;operation name='GetSensorDataOperation' parameterOrder='GetSensorDataOperation'&gt;
   &lt;input message='tns:MeasurementProviderPortType_GetSensorDataOperation' /&gt;
   &lt;output message='tns:MeasurementProviderPortType_GetSensorDataOperationResponse' /&gt;
   &lt;fault message='tns:SensorDataOperationFault' name='SensorDataOperationFault' /&gt;
  &lt;/operation&gt;
 &lt;/portType&gt;
 &lt;binding name='MeasurementProviderPortTypeBinding' type='tns:MeasurementProviderPortType'&gt;
  &lt;soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/&gt;
  &lt;operation name='GetSensorDataOperation'&gt;
   &lt;soap:operation soapAction='http://www.techjava.de/2010/ws4jee/measurement//GetSensorDataOperation'/&gt;
   &lt;input&gt;&lt;soap:body use='literal'/&gt;&lt;/input&gt;
   &lt;output&gt;&lt;soap:body use='literal'/&gt;&lt;/output&gt;
   &lt;fault name='SensorDataOperationFault'&gt;&lt;soap:fault name='SensorDataOperationFault' use='literal'/&gt;&lt;/fault&gt;
  &lt;/operation&gt;
 &lt;/binding&gt;
 &lt;service name='MeasurementProviderService'&gt;
  &lt;port binding='tns:MeasurementProviderPortTypeBinding' name='MeasurementProviderPort'&gt;
   &lt;soap:address location='http://127.0.0.1:8080/de.techjava.ws4ee-de.techjava.ws4ee.sensor/MeasurementProviderFacadeBean'/&gt;
  &lt;/port&gt;
 &lt;/service&gt;
&lt;/definitions&gt;
</pre>
<p>Please note, that the domain datatypes are mapped to the different namespace as the target namespace of the Web Service.</p>
<p>After some tuning the WSDL, like changing the cardinality of elements (minOccurs = 1, maxOccurs = 1) or adding comments we can let the container use the changed WSDL file instead of generating a new one. In order to do this, let us package the WSDL together with the source code. A good place for it is for example <code>/META-INF/wsdl/</code>, so we can provide a reference to it in an <code>wsdlLocation</code>attribute of the <code>@WebService</code> annotation:</p>
<pre class="brush: java;">
@WebService(
...
        wsdlLocation=&quot;META-INF/wsdl/MeasurementProviderService.wsdl&quot;
)
</pre>
<h2>Putting all together</h2>
<p>After the creation of the Facade Bean and its exposure as a Web Service, we need to delegate the call to the SensorManagerBean, providing the actual implementation. In order to do so, add a private member of type <code>SensorManager</code> inside of the facade bean and annotate it with the <code>javax.ejb.EJB</code> annotation. The container will intialize the member using Dependency Injection and provide a valid reference which can be simply used inside of the method.</p>
<pre class="brush: java; highlight: [4,5,20];">
...
public class MeasurementProviderFacadeBean implements MeasurementProviderFacade
{
	@EJB(mappedName=&quot;de.techjava.sensor/SensorManager&quot;)
	private SensorManager sensorManager;

	/**
	 * Simple delegate to the business method of the sensor manager
	 */
	@WebMethod(
			operationName=&quot;GetSensorDataOperation&quot;,
			action=TNS + &quot;/GetSensorDataOperation&quot;
	)
	public @WebResult(name=&quot;measurement&quot;, targetNamespace=TYPES) List&lt;Measurement&gt; getSensorData(
			@WebParam(name=&quot;sensor-id&quot;, targetNamespace=TYPES) SensorID id,
			@WebParam(name=&quot;timespan&quot;, targetNamespace=TYPES) Timespan timespan) throws SensorDataOperationFault
	{
		try
		{
			return sensorManager.getSensorData(id, timespan);
		} catch (SensorManagerException e)
		{
			throw new SensorDataOperationFault(&quot;Error accessing the Sensor Manager: &quot; + e.getMessage() + &quot;&quot;);
		}
	}
}
</pre>
<p>Here is the call in Web Service Explorer:<br />

<a  href="http://www.techjava.de/wp-content/uploads/ws-explorer.png" onclick="javascript:pageTracker._trackPageview('/downloads/wp-content/uploads/ws-explorer.png');" ><img style="margin: 5px;" title="WS Explorer" src="http://www.techjava.de/wp-content/uploads/ws-explorer.png" alt="image WS Explorer" /></a></p>
<h2>Outline</h2>
<p>In this article we showed how to expose the functionality implemented in an EJB 3 Session Bean using Web Service Technology. In fact, the creation of a FacadeBean was not required since one could annotate the <code>SensorManager</code>-Bean directly, but we separated the implementation Bean from the exposing Bean to make it easier to understand. Even if we didn&#8217;t match the WSDL from the J2EE 1.4 example exactly, we came very close. Merely some message-related datatypes have different names. In this example we used the Document/Literal/Wrapped style of Web Services in order to gain maximum interoperability. Even though the creation of the Web Service is a simple task using the annotations. The number of tools and dependencies has dramataically reduced since J2EE 1.4 and the development became less error-prone. Together with other EJB3 improvements it makes the JEE framework suitable for development in the context of enterprise distributed systems.</p>
<h2>References and Resources</h2>
<p>The example source code is available for download (
<a  href="http://www.techjava.de/download/examples/ws4jee.zip" onclick="javascript:pageTracker._trackPageview('/downloads/download/examples/ws4jee.zip');" >ws4jee.zip</a>). Just import the projects from the archive into Eclipse Worskspace. Make sure to set up the JRE and JBoss Server instance for the projecst to work.</p>
<ul>
<li>
<div>[2008,techreport] 
<a  href="#JAVAEE" class="toggle">bibtex</a>  
<a  href='http://java.sun.com/javaee/5/docs/tutorial/doc/' title='Go to document' onclick="javascript:pageTracker._trackPageview('/external/java.sun.com/javaee/5/docs/tutorial/doc/');" ><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>E. Jendrock, J. Ball, D. Carson, I. Evans, S. Fordin, and K. Haase, &quot;The Java EE 5 Tutorial,&quot; 2008.</div>
<div class="bibtex" id="JAVAEE">
         <code>@techreport{JAVAEE, <br />
 &nbsp;&nbsp;author =	{Eric Jendrock and Jennifer Ball and Debbie Carson and Ian Evans and Scott Fordin and Kim Haase}, <br />
 &nbsp; title =	{The Java EE 5 Tutorial}, <br />
 &nbsp; url = {http://java.sun.com/javaee/5/docs/tutorial/doc/}, <br />
 &nbsp; month =	{Oct}, <br />
 &nbsp; year =	{2008}<br />
}</code>
    </div>
</li>
<li>
<div>[2007,book] 
<a  href="#IHNS_2003" class="toggle">bibtex</a>  
<a  href='Oliver Ihns, Dierk Harbeck, Stefan M. Heldt, Holger Koschek, Roman Schlömmer, Jo Ehm, Carsten Sahling' title='Go to document'><img src='http://www.techjava.de/wp-content/plugins/bib2html/external.png' width='10' height='10' alt='Go to document' /></a></div>
<div>O. Ihns, D. Harbeck, S. M. Heldt, H. Koschek, R. Schl&#5954405;r, J. Ehm, and C. Sahling, <em>EJB 3 professionell. Grundlagen- und Expertenwissen zu Enterprise JavaBeans 3 fr Einsteiger, Umsteiger und Fortgeschrittene , publisher	DPunkt Verlag</em>, , 2007.</div>
<div class="bibtex" id="IHNS_2003">
         <code>@Book{IHNS_2003, <br />
 &nbsp;&nbsp;author =	{Oliver Ihns and Dierk Harbeck and Stefan M. Heldt and Holger Koschek and Roman Schlömmer and Jo Ehm and Carsten Sahling}, <br />
 &nbsp; title =	{EJB 3 professionell. Grundlagen- und Expertenwissen zu Enterprise JavaBeans 3 für Einsteiger, Umsteiger und Fortgeschrittene }, <br />
 &nbsp; publisher	{DPunkt Verlag}, <br />
 &nbsp; year =	{2007}, <br />
 &nbsp; month =	{Jul}, <br />
 &nbsp; url = {Oliver Ihns, Dierk Harbeck, Stefan M. Heldt, Holger Koschek, Roman Schlömmer, Jo Ehm, Carsten Sahling}<br />
}</code>
    </div>
</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.techjava.de/topics/2010/01/functionality-web-services-jee/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using SOAP on Nokia 6131 NFC</title>
		<link>http://www.techjava.de/topics/2008/02/using-soap-on-nokia-6131-nfc/</link>
		<comments>http://www.techjava.de/topics/2008/02/using-soap-on-nokia-6131-nfc/#comments</comments>
		<pubDate>Mon, 25 Feb 2008 14:50:38 +0000</pubDate>
		<dc:creator>Simon Zambrovski</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[Content-Type]]></category>
		<category><![CDATA[j2me]]></category>
		<category><![CDATA[NFC]]></category>
		<category><![CDATA[nokia 6131]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.techjava.de/topics/2008/02/using-soap-on-nokia-6131-nfc/</guid>
		<description><![CDATA[Introduction
During some work executed for the proposal to the 
NFC Congress 2008, we introduced a scenario for using the NFC-enabled Nokia 6131 in the industrial environment. The selected use case records some NFC data and uses the Internet to transmit it to the cental server. During the prototype construction, we decided to use Google Maps [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>During some work executed for the proposal to the 
<a  href="http://www.nfc-research.at/" onclick="javascript:pageTracker._trackPageview('/external/www.nfc-research.at/');" >NFC Congress 2008</a>, we introduced a scenario for using the NFC-enabled Nokia 6131 in the industrial environment. The selected use case records some NFC data and uses the Internet to transmit it to the cental server. During the prototype construction, we decided to use Google Maps as a framework for location-based display of gathered information. The server has been constructed based on J2EE EJBs and exposes its 
<a  href="http://www.techjava.de/topics/2008/02/ws4j2ee14/">functionality using SOAP-based document-literal Web Serives</a>, as described in one of the privious posts. </p>
<h3>Nokia 6131 NFC</h3>
<p>The 
<a  href="http://europe.nokia.com/A4307095" onclick="javascript:pageTracker._trackPageview('/external/europe.nokia.com/A4307095');" >Nokia 6131 NFC</a> is first NFC-enabled mobile phone. NFC stands for 
<a  href="http://en.wikipedia.org/wiki/Near_Field_Communication" onclick="javascript:pageTracker._trackPageview('/external/en.wikipedia.org/wiki/Near_Field_Communication');" >Near-Field-Communication</a> &#8211; a new evolving wireless technology to interact with RFID tags. It is strongly supported and pushed by several big players. This results in several conferences around this subject all around the world.<br />
The NFC in Java is specified  in 
<a  href="http://jcp.org/en/jsr/detail?id=257" onclick="javascript:pageTracker._trackPageview('/external/jcp.org/en/jsr/detail');" >JSR 257</a>. In addition to that, 6131 seems to support 
<a  href="http://www.forum.nokia.com/info/sw.nokia.com/id/8a5911e5-2631-4b3e-9599-0f8959c2fb43/6131_NFC.html" onclick="javascript:pageTracker._trackPageview('/external/www.forum.nokia.com/info/sw.nokia.com/id/8a5911e5-2631-4b3e-9599-0f8959c2fb43/6131_NFC.html');" >several other JSRs</a>, also including 
<a  href="http://jcp.org/en/jsr/detail?id=172" onclick="javascript:pageTracker._trackPageview('/external/jcp.org/en/jsr/detail');" >JSR 172</a> for Web Services. After having a closer look on the offered API, it cames out, that only the XML-parser part of JSR 172 is supported.</p>
<h3>Sending SOAP Requests without JSR 172</h3>
<p>In order to set the requests to a Web Service from a mobile phone, that does not supprt Web Service JSR 172, a manual creation of request is needed. This results in something like:</p>
<pre class="brush: java;">
public abstract class SoapHandler extends DefaultHandler {

    public static final String SOAP_BEGIN = &quot;&lt;soapenv:Envelope xmlns:soapenv=\&quot;http://schemas.xmlsoap.org/soap/envelope/\&quot; xmlns:q0=\&quot;http://www.techjava.de/2008/nfc/visualizer/types\&quot; xmlns:xsd=\&quot;http://www.w3.org/2001/XMLSchema\&quot; xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;&gt;&lt;soapenv:Body&gt;&quot;;
    public static final String SOAP_END = &quot;&lt;/soapenv:Body&gt;&lt;/soapenv:Envelope&gt;&quot;;

    private StringBuffer reqBuf = new StringBuffer(); // Buffer that holds the request message

    ...
    public SoapHandler(String message) {
        reqBuf.append(message);
    }

    public int doHttpRequest(String url, String soapAction) throws IOException {

        reqBuf.insert(0, SOAP_BEGIN);
        reqBuf.append(SOAP_END);

        HttpConnection httpConn = (HttpConnection) Connector.open(url);
        httpConn.setRequestMethod(HttpConnection.POST);
        httpConn.setRequestProperty(&quot;SOAPAction&quot;, soapAction);
        httpConn.setRequestProperty(&quot;Content-Length&quot;, Integer.toString(reqBuf.length()));

        OutputStream os = httpConn.openOutputStream();

        os.write(reqBuf.toString().getBytes());
        os.close(); // Request is sent when output-stream is closed
        return httpConn.getResponseCode();
    }

...
}
</pre>
<p>If you execute the code above, the 6131 device send a HTTP request using Content-Type plain/text, that is automatically inserted and changes the case of <strong>&#8220;SOAPAction&#8221;</strong> to lower. As long as JBoss 4.0.5 GA used in this experiment supports only Content-Types <strong>application/soap+xml</strong> and <strong>text/xml</strong> (as it should according to the specification), it throws a SOAP Exception:</p>
<pre class="brush: java;">
javax.xml.soap.SOAPException: Unsupported content type: text/plain
        at org.jboss.ws.soap.MessageFactoryImpl.createMessageInternal(MessageFactoryImpl.java:138)
        at org.jboss.ws.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:87)
        at org.jboss.ws.server.ServiceEndpoint.handleRequest(ServiceEndpoint.java:190)
        at org.jboss.ws.server.ServiceEndpointManager.processSOAPRequest(ServiceEndpointManager.java:355)
        at org.jboss.ws.server.StandardEndpointServlet.doPost(StandardEndpointServlet.java:115)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.jboss.ws.server.StandardEndpointServlet.service(StandardEndpointServlet.java:76)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        ...
</pre>
<p>The MIME Content-Type can easily be set on any HTTP connection:</p>
<pre class="brush: java;">
        httpConn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/soap+xml&quot;);
</pre>
<p>This works well in the NFC Nokia simulator, part of the Nokia SDK, but breaks down executed from the device. It seems to be a bug in the implementation, or an intended feature of Nokia limiting the usage of the phone for business applications, but the <strong>HTTP-Request contains a duplicate Content-Type entry valued by the &#8220;application/soap+xml&#8221;</strong>. Playing around with the implementation, we found that it put any Content-Type header twice into the request. Decompiling and analyzing of simulator code does not helped at all. Since we don&#8217;t know how to get and patch sources of J2ME port to Nokia 6131, I decided to apply a workaround.</p>
<h3>Server tolerance</h3>
<p>JBoss reacts to this fact of a duplicate Content-Type header by throwing a SOAPFaultException. </p>
<pre class="brush: java;">
javax.xml.rpc.soap.SOAPFaultException: Could not parse content type:javax.mail.internet.ParseException: Expected ';', got &quot;,&quot;
        at org.jboss.ws.jaxrpc.SOAPFaultExceptionHelper.exceptionToFaultMessage(SOAPFaultExceptionHelper.java:194)
        at org.jboss.ws.server.ServiceEndpoint.handleRequest(ServiceEndpoint.java:223)
        at org.jboss.ws.server.ServiceEndpointManager.processSOAPRequest(ServiceEndpointManager.java:355)
        at org.jboss.ws.server.StandardEndpointServlet.doPost(StandardEndpointServlet.java:115)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.jboss.ws.server.StandardEndpointServlet.service(StandardEndpointServlet.java:76)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        ...
</pre>
<p>We followed the stacktrace, and identified the class that causes the Execption. It is org.jboss.ws.soap.MessageFactoryImpl located in JBOSS_DEPLOY /jbossws14.sar/jbossws14-core.jar. After some source code alanysis, we found several problems in the implementation:</p>
<pre class="brush: java;">
private static ContentType getContentType(MimeHeaders headers) throws SOAPException
{
	ContentType contentType = null;
	try
	{
		String type[] = headers.getHeader(&quot;Content-Type&quot;);
		if (type != null)
		{
			contentType = new ContentType(type[0]);
		} else
		{
			contentType = new ContentType(&quot;text/xml&quot;);
		}
		return contentType;
	} catch (ParseException e)
	{
		String message = (new JBossStringBuilder()).append(&quot;Could not parse content type:&quot;).append(e).toString();
		throw new SOAPException(message);
	}
}
</pre>
<p>First of all, we would expect that the array length is proven, before the first element is accessed. Secondly, if multiple same headers are present in the request, their values are combined using a comma resulting in:</p>
<p>application/soap+xml; charset=utf-8, application/soap+xml; charset=utf-8</p>
<p>This combination could make sence if you put multiple Content-Lang headers in your request, but not for Content-Type. So we fixed the implementation by following code, taking only the first Content-Type value from the list, if present:</p>
<pre class="brush: java;">
...
String type[] = headers.getHeader(&quot;Content-Type&quot;);
if (type != null &amp;&amp; type.length != 0)
{
	int index = type[0].indexOf(',');
	String correctedType = (index != -1) ? type[0].substring(0, index) : type[0];

	contentType = new ContentType(correctedType);
} else
{
	contentType = new ContentType(&quot;text/xml&quot;);
}
return contentType;
...
</pre>
<h3>Summary</h3>
<p>Even if Nokia 6131 NFC offers some functionality for the developer, it does not provide the best platform for use case try-outs. Especially, the lack of supported standard (like full JSR 172) and a low-end OS (Symbian 40-Series), and some bugs / features in implementation of connectivity will lead to problems in prototype implementations of use cases&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techjava.de/topics/2008/02/using-soap-on-nokia-6131-nfc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
