Monday 19 December 2011

WSDL Binding customization example


<jaxws:bindings
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxws http://java.sun.com/xml/ns/jaxws/wsdl_customizationschema_2_0.xsd
http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
wsdlLocation="http://www.webservicex.net/stockquote.asmx?WSDL">

  <!-- JAXWS -->
  <jaxws:package name="com.company.jaxwstest.client"/>

  <!-- JAXB -->
  <!-- <jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema"> --> 
  <jaxws:bindings node="//xsd:schema">
    <jxb:schemaBindings>
      <jxb:package name="com.company.jaxwstest.client.jaxb"/>
    </jxb:schemaBindings>
  </jaxws:bindings>
 
</jaxws:bindings>

wsgen & wsimport @ ant

<project>

  <!-- Define classpath -->

  <property name="glassfish.dir" value="C:\Programme\Java\Glassfish3\glassfish" />
  <property name="glassfish.modules.dir" value="${glassfish.dir}\modules" />

  <path id="glassfish.modules.path">
    <fileset dir="${glassfish.modules.dir}">
      <include name="**/*.jar" />
    </fileset>
  </path>

  <!-- Define tasks -->

  <taskdef name="wsgen" classname="com.sun.tools.ws.ant.WsGen">
    <classpath refid="glassfish.modules.path" />
  </taskdef>

  <taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport">
    <classpath refid="glassfish.modules.path" />
  </taskdef>

  <!-- Define targets -->

  <target name="server">
    <wsgen keep="true" verbose="true" xendorsed="true" genwsdl="true" fork="true"
      sei="com.company.jaxwstest.server.JavaToWsdl"
      classpath="build/classes"
      destdir="build/classes"
      resourcedestdir="WebContent/WEB-INF/wsdl"
      sourcedestdir="src"/>
  </target>

  <target name="client">
    <wsimport keep="true" verbose="true" xendorsed="true" fork="true"
      wsdl="http://www.webservicex.net/stockquote.asmx?WSDL"
      destdir="build/classes"
      sourcedestdir="src"
      package="com.company.jaxwstest.client"
      binding="binding.xml"/>
  </target>

</project>

Friday 16 December 2011

Wrapper/Bare/RPC example

RPC Java:

@WebMethod(operationName = "hello")
public String sayHello(
@WebParam(name = "firstname") String nachname,
@WebParam(name = "lastname") String vorname)

RPC WSDL:

<message name="hello">
  <part name="firstname" type="xsd:string" />
  <part name="lastname" type="xsd:string" />
</message>

<message name="helloResponse">
  <part name="return" type="xsd:string" />
</message>

<portType name="testService">
  <operation name="hello">
    <input message="tns:hello" />
    <output message="tns:helloResponse" />
  </operation>
</portType>

Document/Wrapped Java (like RPC, plus generated "jaxws" wrapper classes):

@WebMethod(operationName = "hello")
public String sayHello(
@WebParam(name = "firstname") String nachname,
@WebParam(name = "lastname") String vorname)

Document/Wrapped WSDL:

<types>
  <xs:element name="hello" nillable="true" type="tns:sayHello"/>
  <xs:element name="helloResponse" nillable="true" type="tns:sayHelloResponse"/>

  <xs:complexType name="sayHello">
    <xs:sequence>
      <xs:element name="firstname" type="xs:string" minOccurs="0"/>
      <xs:element name="lastname" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sayHelloResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</types>

<message name="hello">
  <part name="parameters" element="tns:hello"/>
</message>

<message name="helloResponse">
  <part name="parameters" element="tns:helloResponse"/>
</message>

<portType name="testService">
  <operation name="hello">
    <input message="tns:hello"/>
    <output message="tns:helloResponse"/>
  </operation>
</portType>

Document/Bare Java (no generated class, with additional "Person" bean):

@WebMethod(operationName = "hello")
public String sayHello(Person p)

Document/Bare WSDL:

<types>
  <xs:element name="hello" nillable="true" type="tns:person"/>
  <xs:element name="helloResponse" nillable="true" type="xs:string"/>

  <xs:complexType name="person">
    <xs:sequence>
      <xs:element name="nachname" type="xs:string" minOccurs="0"/>
      <xs:element name="vorname" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</types>

<message name="hello">
  <part name="hello" element="tns:hello"/>
</message>

<message name="helloResponse">
  <part name="helloResponse" element="tns:helloResponse"/>
</message>

<portType name="testService">
  <operation name="hello">
    <input message="tns:hello"/>
    <output message="tns:helloResponse"/>
  </operation>
</portType>

WS-I BP Clarifications

See: http://www.ws-i.org/deliverables/workinggroup.aspx?wg=basicprofile

Main points:
  • no SOAP encoding 
  • for rpc-literal bindigs, parts are defined with the "type" attribute 
  • for document-literal bindigs, parts are defined with the "element" attribute 
  • for document-literal bindings, at most one part is in the soap body
  • valid wsdl operations: one-way and request-response only
  • operation name overloading is forbidden (two operations with the same name)
  • an endpoint that supports multiple operations must unambiguously identify the operation being invoked based on the input message that it receives. This is only possible if all the operations specified in the wsdl:binding associated with an endpoint have a unique operation signature (i.e. one input message type = one operation)
  • with rpc-literal bindings, the response must have a wrapper element whose name is the corresponding wsdl:operation name suffixed with the string "Response"
  •  with rpc-literal bindings, the wrapper elements for parameters and return value have no namespace

WSDL History

WSDL 1.2 was renamed to WSDL 2.0 because it has substantial differences from WSDL 1.1:
  • infoset based
  • targetNamespace is a required attribute
  • capability of including and importing documents
  • other types than XSD allowed (DTD ...)
  • additional exchange patterns
  • removal of message constructs
  • no support for operator overloading
  • portTypes renamed to interfaces
  • ports renamed to endpoint
  • support for HTTP PUT/DELETE verbs
  • ...
WSDL 2.0 drawbacks:
  • not well supported by platforms
  • not supported by BPEL
  • WS-I 2.0 (November 2010)

Session Tracking Options

Wether a client will join a session initiated on server side:
  • cookies
  • URL rewriting (the new URL contains a session identifier)
  • SSL session ID

Thursday 15 December 2011

WSDL: SOAP vs. plain HTTP binding

  • SOAP transport bindings: HTTP, SMTP ...
  • SOAP header = metadata, authentication ...
  • SOAP message path processing (header attributes: actor/role, mustUnderstand)
  • SOAP body "document": set of independent body entries (usually one) with encodingStyle
  • SOAP body "rpc": method invocation and method response as "struct" with parameters and return value as "accessors"
  • SOAP encoding (instead of XSD) - DEPRECATED by WS-I Basic profile
  • SOAP fault vs. plain XML fault message with HTTP status code 500
  • SOAP attachment vs. plain WSDL MIME attachment:
    • SwA (SOAP with Attachment): SAAJ (SwA API for Java)
    • MTOM (SOAP Message Transmission Optimization Mechanism) using XOP (XML-binary Optimized Packaging)
    • Abstract model: SOAP 1.2 attachment feature

Note (SwA vs. MTOM):

"MTOM combines the efficiency of SOAP with Attachments, but it does so without having to break the binary data outside of the SOAP message. How can this be? The key is a technology called XOP.

XOP allows binary data to be included as part of the XML Infoset. In fact the XML Infoset becomes a superset of the traditional Infoset known as the XOP Infoset. It allows for the binary data to be stored outside of the XML document, just like in SOAP with Attachments. It uses a special XOP:include element to tell the processor to replace the contents with the referenced binary data, thus encapsulating the logic of discrete storage and retrieval of the binary data. This logic becomes inherent to the XML parser, and allows the SOAP parser to treat the binary data as part of the XML document, with no special retrieval logic. Similarly it allows for a SOAP server to create a SOAP message in a uniform way, no special logic for breaking out that binary data from the SOAP message."

JAX-WS Tutorial & Wrapper/Bare

See:

Wednesday 14 December 2011

JAX-WS WSDL-Java mapping

Java Annotations WSDL Comments
package - definitions "targetNamespace" attribute -
interface or implementation class @WebService @SOAPBinding @BindingType (default SOAP 1.1/HTTP) portType "Service Endpoint Interface" (SEI). Supported SOAP binding styles = document/literal/wrapped (default), document/literal/bare, RPC/literal/(wrapped)
method (synchronous and asynchronous) @WebMethod @WebResult @RequestWrapper @ResponseWrapper operation Only one-way (with @OneWay) and request-response. Neither notification nor solicit-response. Thus, one input message and zero or one output message
request/response wrapper bean, parameter and return value types JAXB annotation types Mapped by JAXB. With "wrapped" parameter mapping style, request/response wrapper beans are generated so JAXB can generate the XSD used in WSDL. With them, developers have complete control over the generated WSDL. Default package is "jaxws"
method parameters @WebParam (override argN names) messages or message parts "wrapper" parameter mapping style tells that the root element of the message, also called "wrapper element" represents the name of the operation. Service parameters are mapped to the children of the root element (like rpc). The "non-wrapper" style does not make this assumption: the entire message is passed as unique service parameter. Out and in/out parameters are mapped to javax.xml.ws.Holder (with public "value" property)
method return value - single output message part, otherwise void With wrapper mapping, the children name must be unique or named "return"
exception @WebFault fault message Mapped fault message is available via getFaultInfo()
class extending Service @WebServiceClient service -
service class getter method @WebEndPoint port Getter returns a proxy thet implements the mapped SEI

Name collision avoidance:
  • "_Type" suffix (class)
  • "_Exception" suffix (Exception class)
  • "_Service" suffix (Service class)
  • "_" prefix (method name)

Tuesday 13 December 2011

WSDL Overview

WSDL structure:
  • types = XSD definitions
  • message = abstract set of message parts of some type
  • operation = set of input/output/fault messages (one-way, request-response, solicit-response or notification)
  • port type = set of operations
  • binding = port type + protocol and concrete format for the abstract messages
  • port = binding + endpoint address
  • service = set of ports
WSDL 1.1 bindings:
  • SOAP (rpc/document operation, literal/encoded message parts, soap header, URI)
  • MIME (content type of a single or mutiple message parts)
  • HTTP (GET/POST binding, operation URI, URL encoded message parts, port URI)
SOAP binding:
  • style (for operations)
    • document: the message parts (usually one) contain documents and are assembled directly under the SOAP body
    • rpc: the message parts contain parameters or return values and are assembled inside a wrapper element for the operation invocation/response under the SOAP body
  • use (for message parts)
    • encoded: message parts are abstract type that require a specific encoding (SOAP encoding or other)
    • literal: message parts are concrete schema definitions

WSDL Example

<wsdl:definitions targetNamespace="http://www.webserviceX.NET/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET/">
      <s:element name="GetQuote">
        <s:complexType>
          <s:sequence><s:element minOccurs="0" maxOccurs="1" name="symbol" type="s:string" /></s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetQuoteResponse">
        <s:complexType>
          <s:sequence><s:element minOccurs="0" maxOccurs="1" name="GetQuoteResult" type="s:string" /></s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="string" nillable="true" type="s:string" />
    </s:schema>
  </wsdl:types>
  <wsdl:message name="GetQuoteSoapIn">
    <wsdl:part name="parameters" element="tns:GetQuote" />
  </wsdl:message>
  <wsdl:message name="GetQuoteSoapOut">
    <wsdl:part name="parameters" element="tns:GetQuoteResponse" />
  </wsdl:message>
  <wsdl:message name="GetQuoteHttpGetIn"></wsdl:message>
  <wsdl:message name="GetQuoteHttpGetOut"></wsdl:message>
  <wsdl:message name="GetQuoteHttpPostIn"></wsdl:message>
  <wsdl:message name="GetQuoteHttpPostOut"></wsdl:message>
  <wsdl:portType name="StockQuoteSoap">
    <wsdl:operation name="GetQuote">
      <wsdl:documentation>Get Stock quote for a company Symbol</wsdl:documentation>
      <wsdl:input message="tns:GetQuoteSoapIn" />
      <wsdl:output message="tns:GetQuoteSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:portType name="StockQuoteHttpGet"></wsdl:portType>
  <wsdl:portType name="StockQuoteHttpPost"></wsdl:portType>
  <wsdl:binding name="StockQuoteSoap" type="tns:StockQuoteSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetQuote">
      <soap:operation soapAction="http://www.webserviceX.NET/GetQuote" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="StockQuoteSoap12" type="tns:StockQuoteSoap"></wsdl:binding>
  <wsdl:binding name="StockQuoteHttpGet" type="tns:StockQuoteHttpGet"></wsdl:binding>
  <wsdl:binding name="StockQuoteHttpPost" type="tns:StockQuoteHttpPost"></wsdl:binding>
  <wsdl:service name="StockQuote">
    <wsdl:port name="StockQuoteSoap" binding="tns:StockQuoteSoap">
      <soap:address location="http://www.webservicex.net/stockquote.asmx" />
    </wsdl:port>
    <wsdl:port name="StockQuoteSoap12" binding="tns:StockQuoteSoap12"></wsdl:port>
    <wsdl:port name="StockQuoteHttpGet" binding="tns:StockQuoteHttpGet"></wsdl:port>
    <wsdl:port name="StockQuoteHttpPost" binding="tns:StockQuoteHttpPost"></wsdl:port>
  </wsdl:service>
</wsdl:definitions>

Java Web Services History

VersionJSRReleasedDescription
JAX-WS 2.0224May. 2006
  • JAXB binding
  • WSDL 1.1 (not 2.0)
  • SOAP 1.2 (HTTP, XML Information Set)
  • WS-I Profile 1.1
  • Literal instead of SOAP encoding
  • Asynchronous
  • J2SE 5.0 annotation
  • Security (JSR 183)
JAX-RPC 1.1101Oct. 2003Interoperability (WS-I Profile 1.0)
JAX-RPC 1.0101Jun. 2002Own XML binding

Friday 9 December 2011

Java Application Servers (Glassfish, WebLogic, Geronimo ...)

Server Vendor Current Version Features Comment
Glassfish Glassfish Community 3.0 JEE 6 Full Profile JEE RI
WebLogic Oracle 12c JEE 6 Full Profile Uses Glassfish
Geronimo Apache 3.0 JEE 6 Full Profile Supported by IBM
Websphere IBM 8.5 JEE 6 Full Profile -
 
JBoss Red Hat 7.0 JEE 6 Web Profile -
Resin Caucho 4.0 JEE 6 Web Profile -
JOnAS OW2 Consortium 5.3 JEE 6 Web Profile -
 
Netweaver SAP 7.3 JEE 5 -

Thursday 8 December 2011

Sun Application Server History

ServerFeaturesReleased
Glassfisch (GF) 4.0--
Glassfisch (GF) 3.0Java EE 6, OSGiDec. 2009
Glassfisch (GF) v2Java EE 5Sep. 2007
Glassfisch (GF) v1Java EE 5May. 2006
Sun Java System Application Server (SJSAS) 9.1 (GFv2)Java EE 5/Servlet 2.5Sep. 2007
Sun Java System Application Server (SJSAS) 9.0 (GFv1)Java EE 5/Servlet 2.5May. 2006
Sun Java System Application Server (SJSAS) 8J2EE 1.4/Servlet 2.4Mar. 2004
Sun Java System Application Server (SJSAS) 7J2EE 1.3/Servlet 2.3Oct. 2002
iPlanet/Sun ONE Application Server (iAS) 6.5J2EE 1.2/Servlet 2.2Mar. 2002
iPlanet/Sun ONE Application Server (iAS) 6.0J2EE 1.2/Servlet 2.2May. 2000
NetDynamics Application Server 5.0Servlet 2.1Apr. 1999
NetDynamics Application Server 4.0JDK 1.1Apr. 1998
NetDynamics Application Server 3.1JDK 1.1May. 1997
Netscape Application Server (NAS) 4.0Servlet 2.1Jun. 1999
Netscape Application Server (NAS) 2.1JDK 1.1Mar. 1998
Kiva Application Server 2.0-Dec. 1997
Kiva Application Server 1.5-Nov. 1997

Note: Jcaps 5.1.3:
  • logical host = SJSAS 8
  • emanager = Tomcat 5.0

Java History

Version Released
Java SE 7 Jul. 2011
Java SE 6 Dec. 2006
J2EE 5.0 Sep. 2004
J2SE 1.4 Feb. 2002
J2SE 1.3 May 2000
J2SE 1.2 Dec. 1998
JDK 1.1 Feb. 1997
JDK 1.0 Jan. 1996

Servlet History

API version Released Platform Reference
Servlet 3.0 Dec. 2009 JavaEE 6, JavaSE 6 JSR 315
Servlet 2.5 May 2006 JavaEE 5, JavaSE 5 JSR 154
Servlet 2.4 Nov. 2003 J2EE 1.4, J2SE 1.3 JSR 154
Servlet 2.3 Aug. 2001 J2EE 1.3, J2SE 1.2 JSR 53
Servlet 2.2 Aug. 1999 J2EE 1.2, J2SE 1.2 Part of J2EE
Servlet 2.1 Nov. 1998 Unspecified First official specification
Servlet 2.0 Apr. 1998 JDK 1.1 Java Servlet Development Kit 2.0 (jsdk.jar)
Servlet 1.0 Jun. 1997 - -

Wednesday 7 December 2011

Web Flow Spring MVC Config.

  <webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
    <webflow:flow-location id="filterWizzard.csv" path="/WEB-INF/flows/filterWizzard.xml" />
  </webflow:flow-registry>

  <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry" />

  <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
  </bean>

  <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry" />
  </bean>

  <!-- JSR 303 validation-->
  <webflow:flow-builder-services id="flowBuilderServices" validator="validator"
    view-factory-creator="mvcViewFactoryCreator" />

  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

  <!-- MVC logical view names -->
  <bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
    <property name="viewResolvers" ref="viewResolver" />
  </bean>

Spring Webflow JSR 303 error messages

instead of:
<sf:errors/>

use:
<c:forEach items="${flowRequestContext.messageContext.allMessages}" var="message">
    <div class="err">${message.text}</div>
</c:forEach>

Tuesday 6 December 2011

Spring MVC Form with Session Attribute

@Controller
@RequestMapping("/jcdFilterList.csv")
@SessionAttributes("jcdFilter")
public class JcdFormController
{

  @Autowired
  private JcdDao jcdDao;

  private List<DropdownItem> filterNames;

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  public JcdFormController()
  {
    logger.info("---------- instantiating ----------");

    filterNames = nameList();
  }

  private static List<DropdownItem> nameList()
  {
    ArrayList<DropdownItem> result = new ArrayList<DropdownItem>();

    // Assign labels
    [...]

    return result;
  }

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  // add reference data to model
  @ModelAttribute("filterNames")
  public List<DropdownItem> getFilterNames()
  {
    return filterNames;
  }

  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  @RequestMapping(method = RequestMethod.GET)
  public String showForm(Map<String, Object> model)
  {
    logger.fine("showForm");
    if (model.get("jcdFilter") == null)
    {
      model.put("jcdFilter", new Filter());
    }
    return "filterForm";
  }

  @RequestMapping(method = RequestMethod.POST)
  public String processForm(Map<String, Object> model, @Valid @ModelAttribute("jcdFilter") Filter filter, BindingResult binding) throws JcapsDocException
  {
    logger.fine("processForm");
    logger.fine(filter.toString());
    if (binding.hasErrors())
    {
      return "filterForm";
    }
    else
    {
      model.put(JcdListController.JCD_LIST_KEY, jcdDao.get(filter));
      return "csvList";
    }
  }

Spring MVC Form Processing

See http://levelup.lishman.com/spring/form-processing/controller.php

Thursday 1 December 2011

Spring properties, XML and annotation

  <util:properties id="config" location="classpath:config.properties" />

---

  <bean id="directoryDao" class="bla.bla.Bla">
    <constructor-arg>
      <value>#{config['jcdListPath']}</value>
    </constructor-arg>
  </bean>

---

  @Value("#{config['javadocTreeUrl']}")
  private String javadocTreeUrl;

  @RequestMapping("/buildJavadoc")
  public String buildJavadoc()  {
    return "redirect:" + javadocTreeUrl;
  }