<?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>Minimizr.com &#187; Spring</title>
	<atom:link href="http://minimizr.com/blog/category/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://minimizr.com</link>
	<description>More With Less</description>
	<lastBuildDate>Sun, 28 Oct 2007 13:39:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3707</generator>
		<item>
		<title>Minimal Web Services with XFire, Spring and PHP</title>
		<link>http://minimizr.com/blog/2006/11/minimal-web-services-with-xfire-spring-and-php/</link>
		<comments>http://minimizr.com/blog/2006/11/minimal-web-services-with-xfire-spring-and-php/#comments</comments>
		<pubDate>Mon, 13 Nov 2006 19:27:17 +0000</pubDate>
		<dc:creator>kristian</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[XFire]]></category>

		<guid isPermaLink="false">http://www.minimizr.com/blog/2006/11/minimal-web-services-with-xfire-spring-and-php/</guid>
		<description><![CDATA[If you have a Java web application implemented with Java 5 and Spring Framework, it is really easy to expose your POJOs as web services. In this example I use XFire and JSR 181 annotations for that. I&#8217;ll also make a small web service client example with PHP. The goal is to add web services [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>If you have a Java web application implemented with Java 5 and <strong><a href="http://www.springframework.org/">Spring</a></strong> Framework, it is really easy to expose your POJOs as web services. In this example I use <strong><a href="http://xfire.codehouse.org/">XFire</a></strong> and JSR 181 annotations for that. I&#8217;ll also make a small web service client example with <strong><a href="http://www.php.net/">PHP</a></strong>. The goal is to add web services to the existing Java code with absolute minimal code addition. I was about to add web service authentication with Acegi Security, but instead for now, there is no authentication in this example.</p>
<p>XFire has a quite versatile but scarce <a href="http://xfire.codehaus.org/User%27s+Guide">user&#8217;s guide</a>. But it is a good start, so start with <a href="http://xfire.codehaus.org/Overview">overview</a> and quick start. Add XFire libraries and the depencies with the help of a <a href="http://xfire.codehaus.org/Dependency+Guide">Depency quide</a>. This example works at least with the following libraries:</p>
<table>
<tr>
<td valign="top">
<ul>
<li>xfire-all-1.2.2</li>
<li>activation-1.1</li>
<li>commons-codec-1.3</li>
<li>commons-httpclient-3.0</li>
<li>commons.logging-1.0.4</li>
<li>mail-1.4</li>
<li>jaxen-1.1-beta-9</li>
<li>jdom-1.0</li>
</ul>
</td>
<td valign="top">
<ul>
<li>junit-3.8.1</li>
<li>servlet-api-2.3</li>
<li>spring-2.0</li>
<li>stax-api-1.0.1</li>
<li>wsdl4j-1.5.2</li>
<li>xbean-spring-2.5</li>
<li>wstx-3.0.1</li>
<li>XmlSchema-1.1</li>
</ul>
</td>
<td valign="top">
<ul>
<li>xfire-jsr181-api-1.0-M1</li>
<li>jaxb-xjc-2.0.1</li>
<li>jaxb-impl-2.0.1</li>
<li>jaxb-api-2.0</li>
<li>aopalliance-1.0</li>
<li>commons-beanutils-1.7.0</li>
</ul>
</td>
</tr>
</table>
<p>XFire 1.2.2 package comes with xbean-spring-2.6. There can be some <a href="http://forum.springframework.org/showthread.php?t=29867">problems</a> with that version but at least version 2.5 is working with Spring 2.0.</p>
<p>First, add <strong>xfire-servlet.xml</strong> into WEB-INF directory. Here are the default settings from the user&#8217;s manual:</p>
<pre lang="xml">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
    <bean id="jaxbTypeMappingRegistry"
        class="org.codehaus.xfire.jaxb2.JaxbTypeRegistry"
        init-method="createDefaultMappings" singleton="true"/>
    <bean id="webAnnotations"
        class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>
    <bean id="handlerMapping"
        class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="typeMappingRegistry">
        <ref bean="jaxbTypeMappingRegistry"/>
      </property>
<property name="xfire">
        <ref bean="xfire"/>
      </property>
<property name="webAnnotations">
        <ref bean="webAnnotations"/>
      </property>
    </bean>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
          <entry key="/">
            <ref bean="handlerMapping"/>
          </entry>
        </map>
</property>
    </bean>
</beans>
</pre>
<p>Add xfire-servlet.xml  into the Spring&#8217;s contextConfigLocation and XFireServlet in <strong>web.xml</strong> file:</p>
<pre lang="xml">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
    /WEB-INF/applicationContext.xml
    /WEB-INF/xfire-servlet.xml
  </param-value>
</context-param>

<servlet>
  <servlet-name>XFireServlet</servlet-name>
  <display-name>XFire Servlet</display-name>
  <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>XFireServlet</servlet-name>
  <url-pattern>/services/v1/*</url-pattern>
</servlet-mapping>
</pre>
<p>Let&#8217;s have a simply <strong>MinimizrFacade.java</strong> Java interface:</p>
<pre lang="java">
package com.minimizr.service;

import java.util.List;
import com.minimizr.service.domain.ExampleObject;

public interface MinimizrFacade {
  String getString();
  String echoString(String string);
  ExampleObject echoObject(ExampleObject exampleObject);
  List<exampleObject> loadExampleObjectList();
}
</pre>
<p>And let&#8217;s have another <strong>MinimizrService.java</strong> Java interface for web services:</p>
<pre lang="java">
package com.minimizr.service;

import java.util.List;
import javax.jws.WebService;
import com.minimizr.service.domain.ExampleObject;

@WebService
public interface MinimizrService {
  String getString();
  String echoString(String string);
  ExampleObject echoObject(ExampleObject exampleObject);
  List<exampleObject> loadExampleObjectList();
}
</pre>
<p>And for this example a <strong>ExampleObject.java</strong> Java object:</p>
<pre lang="java">
package com.minimizr.service;

public class ExampleObject {
  private String name;
  private Integer age;

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
</pre>
<p>And finally a <strong>MinimizrImpl.java</strong> Java implementation for the interfaces:</p>
<pre lang="java">
package com.minimizr.domain.logic;

import java.util.List;
import javax.jws.WebService;
import com.minimizr.service.ExampleObject;
import com.minimizr.service.MinimizrService;

@WebService(serviceName = "MinimizrService",
    endpointInterface = "com.minimizr.service.MinimizrService")
public class MinimizrImpl implements MinimizrFacade, MinimizrService {
  public String getString() {
    return "Example string";
  }

  public String echoString(String string) {
    return string;
  }

  public ExampleObject echoObject(ExampleObject exampleObject) {
    return exampleObject;
  }

  public List loadExampleObjectList() {
    /* Here you would get list of ExampleObjects for example from database
    and return it instead of null */
    return null;
  }
}
</pre>
<p>XFire does not support <a href="http://jira.codehaus.org/browse/XFIRE-425">RPC-encoding</a> but you can use <a href="http://docs.codehaus.org/display/XFIRE/PHP+Interoperation">XFire web services with PHP</a> with document/literal style of SOAP.</p>
<p>Here is a really simple example to use all the exposed java web services in this example with <a href="http://sourceforge.net/projects/nusoap/">NuSOAP</a> PHP SOAP library. There are <ins datetime="2006-12-02T13:42:02+00:00">no</ins> checks for errors in the code:</p>
<pre lang="php">
<?php
require("../lib/nusoap.php");
$soapClient = new soapclient(
    "http://www.minimizr.com/ws/services/v1/MinimizrService?wsdl", "wsdl");
$proxyClass = $soapClient->getProxy();

// getString
$string = $proxyClass->getString();
print("<b>String:</b> " . $string["out"] . "
<hr/>");

// echoString
$string = $proxyClass->echoString(array("in0" => "ABC"));
print("<b>String:</b> " . $string["out"] . "
<hr/>");

// echoObject
$requestObject = array("name" => "John", "age" => 50);
$result = $proxyClass->echoObject(array("in0" => $requestObject));
$resultObject = $result["out"];
print("<b>Object:</b> name: " . $resultObject["name"]);
print(", age: " . $resultObject["age"] . "
<hr/>");

// loadExampleObjectList
$exampleObjectList = $proxyClass->loadExampleObjectList();
foreach ($exampleObjectList["out"]["ExampleObject"] as $key => $value) {
  print($value["name"] . " " . $value["age"] . "<br/>");
}
?>
</pre>
<h4>Authentication</h4>
<p><em>Added November 14, 2006</em>: Well, easiest and most straightforward way to secure web service is to use HTTP Authentication. It doesn&#8217;t need any additional code in the server side. While still looking for solution to use easily Acegi Security, I&#8217;ll add HTTP Authentication to this example. On the server side you&#8217;ll have to add security constraint into <strong>web.xml</strong>:</p>
<pre lang="xml">
<security-constraint>
  <web-resource-collection>
    <web-resource-name>Protected Minimizr Web Services</web-resource-name>
    <url-pattern>/services/v1/*</url-pattern>
  </web-resource-collection>
  <auth-constraint>
    <role-name>minimizr.webservices.client</role-name>
  </auth-constraint>
</security-constraint>

<login-config>
  <auth-method>BASIC</auth-method>
  <realm-name>Minimizr Realm</realm-name>
</login-config>

<security-role>
  <description>Required roles to use the Web Services</description>
  <role-name>minimizr.webservices.client</role-name>
</security-role>
</pre>
<p>And couple more lines into the PHP file. Credentials must be added into the wsdl url and proxy class. Notice that it is quite necessary to use SSL connection (https) with basic authentication since username and password are in clear text. You can use useHTTPPersistentConnection method to use persistent connection, if possible:</p>
<pre lang="php">
<?php
require("../lib/nusoap.php");

$username = "username";
$password = "password";
$method = "basic";

$soapClient = new soapclient(
  "https://$username:$password@www.minimizr.com/ws/services/v1/MinimizrService?wsdl",
  "wsdl");
$proxyClass = $soapClient->getProxy();
$proxyClass->setCredentials($username, $password, $method);
$proxyClass->useHTTPPersistentConnection();
...
</pre>
<h4>Conclusion</h4>
<p>It is no brainer to expose Java POJOs as web services with Spring, XFire and JSR-181 annotations. And it is as easy use those web services with Java or PHP or other platforms. I guess integrating <a href="http://www.acegisecurity.org/">Acegi Security</a> with XFire web services needs a little bit more work. Any suggestions for the easiest way to implement it?</p>
<h4>Additional recourses</h4>
<ul>
<li><a href="http://www.logemann.org/blojsom/blog/default/2006/05/26/Webservices-with-Spring-XFire-and-jsr181.html">Webservices with Spring, XFire and jsr181</a></li>
<li><a href="http://www.memestorm.com/blog/basic-spring-web-services-with-xfire-and-jsr-181/">Basic Spring Web services with XFire and JSR 181</a></li>
<li><a href="http://wiki.firebright.com/bin/view/Main/SpringHibernateXFire"> Spring, Hibernate, and XFire</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://minimizr.com/blog/2006/11/minimal-web-services-with-xfire-spring-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
