<?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>Dev @ Work &#187; Web Services</title>
	<atom:link href="http://www.devatwork.nl/tag/web-services/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devatwork.nl</link>
	<description>A day in the life of a developer</description>
	<lastBuildDate>Tue, 18 Oct 2011 16:32:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Expose a Liferay Service as a Web Service</title>
		<link>http://www.devatwork.nl/2010/04/expose-a-liferay-service-as-a-web-service/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=expose-a-liferay-service-as-a-web-service</link>
		<comments>http://www.devatwork.nl/2010/04/expose-a-liferay-service-as-a-web-service/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 06:39:50 +0000</pubDate>
		<dc:creator>Bert Willems</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Liferay]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.devatwork.nl/?p=729</guid>
		<description><![CDATA[In my previous article I showed you how you can implement a reusable Liferay Service without using Ext or  the Service Builder utility. In this article we will take it one step further and expose the service we have created as web service again without using Ext or the Service Builder. Liferay exposes the services [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-656" title="Liferay" src="http://www.devatwork.nl/wp-content/uploads/2010/01/liferay-logo-block.jpg" alt="" width="99" height="96" />In my previous article I showed you how you can implement a reusable Liferay Service without using Ext or  the Service Builder utility. In this article we will take it one step further and expose the service we have created as web service again without using Ext or the Service Builder.</p>
<p>Liferay exposes the services based on Axis using a separate web application called tunnel-web. We will hook into tunnel-web so our service will be exposed in exactly the same way Liferay services are externalized. Let&#8217;s get started.<br />
<span id="more-729"></span></p>
<h3>0. Project Setup</h3>
<p>We will pick up from where we ended in the previous article. I have you haven&#8217;t followed the previous article you can <a href="http://www.devatwork.nl/wp-content/uploads/2010/04/hello-world-result.zip">download the initial project setup here</a>. Please take a minute to familiarize yourself with the project structure.</p>
<p>We need to add a new dependencies to the pom of the service contract library:</p>
<pre class="brush: xml; title: ; wrap-lines: false; notranslate">&lt;dependency&gt;
    &lt;groupId&gt;com.liferay.portal&lt;/groupId&gt;
    &lt;artifactId&gt;portal-service&lt;/artifactId&gt;
    &lt;version&gt;${liferay.version}&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.liferay.portal&lt;/groupId&gt;
    &lt;artifactId&gt;portal-kernel&lt;/artifactId&gt;
    &lt;version&gt;${liferay.version}&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.liferay.portal&lt;/groupId&gt;
    &lt;artifactId&gt;portal-impl&lt;/artifactId&gt;
    &lt;version&gt;${liferay.version}&lt;/version&gt;
    &lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;</pre>
<h3>1. Prepare the portal</h3>
<p>Make sure tunnel-web is installed in your application server. See the Liferay administration guide for details. The access to the web-service is restricted based on IP address. Open your portal-ext.properties and add the following lines to allow specified IP addresses to access to the web-services.</p>
<pre class="brush: plain; title: ; notranslate">axis.servlet.hosts.allowed=127.0.0.1,SERVER_IP
axis.servlet.https.required=false</pre>
<h3>2. Add the Service Classes</h3>
<p>Ok lets get down to business. The first step is to create the web service class in the service contract library. Create a new class called HelloWorldServiceSoap in the package nl.devatwork.hello.world.service.http and copy the following code into it:</p>
<pre class="brush: java; title: ; wrap-lines: false; notranslate">package nl.devatwork.hello.world.service.http;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import nl.devatwork.hello.world.service.HelloWorldServiceUtil;

import java.rmi.RemoteException;

public class HelloWorldServiceSoap {
    private static Log _log = LogFactoryUtil.getLog(HelloWorldServiceSoap.class);

    public static String sayHello(String name)
            throws RemoteException {
        String result;
        try {
            result = HelloWorldServiceUtil.sayHello(name);
        } catch (Exception e) {
            _log.error(e, e);

            throw new RemoteException(e.getMessage());
        }

        return result;
    }
}</pre>
<p>This class wraps SOAP calls and maps them to the appropriate service call.</p>
<p>You can now build the service contract library by executing the following command in the /hello-world-service/ folder:</p>
<pre class="brush: bash; light: true; title: ; notranslate">mvn clean package</pre>
<p>Copy the resulting JAR file (/hello-world-service/target/hello-world-service-1.0.0-SNAPSHOT.jar) to the /lib/ext folder of your application server.</p>
<h3>3. Apply Glue</h3>
<p>The final step is a bit of handwork. We need to manually register the new service to the tunnel-web application. Open the server-config.wsdd located in the deploy directory/tunnel-web/WEB-INF folder of your application server. Add the following code just above the end of the XML:</p>
<pre class="brush: xml; title: ; wrap-lines: false; notranslate">&lt;service name=&quot;Portlet_DevAtWork_HelloWorldService&quot; provider=&quot;java:RPC&quot; style=&quot;rpc&quot; use=&quot;encoded&quot;&gt;
	&lt;parameter name=&quot;wsdlTargetNamespace&quot; value=&quot;urn:http.service.world.hello.devatwork.nl&quot;/&gt;
	&lt;parameter name=&quot;wsdlServiceElement&quot; value=&quot;HelloWorldServiceSoapService&quot;/&gt;
	&lt;parameter name=&quot;wsdlServicePort&quot; value=&quot;Portlet_DevAtWork_HelloWorldService&quot;/&gt;
	&lt;parameter name=&quot;className&quot; value=&quot;nl.devatwork.hello.world.service.http.HelloWorldServiceSoap&quot;/&gt;
	&lt;parameter name=&quot;wsdlPortType&quot; value=&quot;HelloWorldServiceSoap&quot;/&gt;
	&lt;parameter name=&quot;typeMappingVersion&quot; value=&quot;1.2&quot;/&gt;
	&lt;operation xmlns:operNS=&quot;urn:http.service.world.hello.devatwork.nl&quot; name=&quot;sayHello&quot; qname=&quot;operNS:sayHello&quot; soapAction=&quot;&quot;&gt;
		&lt;parameter xmlns:tns=&quot;http://www.w3.org/2001/XMLSchema&quot; qname=&quot;name&quot; type=&quot;tns:string&quot;&gt;&lt;/parameter&gt;
	&lt;/operation&gt;
	&lt;parameter name=&quot;allowedMethods&quot; value=&quot;sayHello&quot;/&gt;
&lt;/service&gt;</pre>
<p>This will register our service within tunnel-web.</p>
<p>Start your application server and navigate to <a href="http://127.0.0.1:8080/tunnel-web/axis">http://127.0.0.1:8080/tunnel-web/axis</a>. Here you will find a list of all the services currently exposed by Liferay. In the list you will find our service (Portlet_DevAtWork_HelloWorldService). You can view the WSDL by clicking on the link behind the service.</p>
<p>That wasn&#8217;t to bad now was it? If you are to lazy to copy the code you can <a href="http://www.devatwork.nl/wp-content/uploads/2010/04/hello-world-remote-result.zip">download the complete project here</a>.</p>
<p>I hope you learned how you can expose services as web services in Liferay. In the upcoming article I will show you how you can add security to the web service.</p>
<p>That is it for now. Let me know if you have any comments or questions.</p>

	Tags: <a href="http://www.devatwork.nl/tag/java/" title="Java" rel="tag">Java</a>, <a href="http://www.devatwork.nl/tag/liferay/" title="Liferay" rel="tag">Liferay</a>, <a href="http://www.devatwork.nl/tag/spring/" title="Spring" rel="tag">Spring</a>, <a href="http://www.devatwork.nl/tag/web-services/" title="Web Services" rel="tag">Web Services</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.devatwork.nl/2010/04/expose-a-liferay-service-as-a-web-service/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

