Archive for webservice

Spring MVC and Restlet Integration

The first thing needs to be done is adding RestletFrameworkServlet to web.xml as follows :

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext-business.xml
        </param-value>
    </context-param>

    <!-- Loads the root application context of this web app at startup -->
    <listener>
       <listener-class>
              org.springframework.web.context.ContextLoaderListener
       </listener-class>
   </listener>


    <!-- Spring Dispatcher Servlet which handles http requests
          and invokes the appropriate controller -->
   <servlet>
        <servlet-name>springrest</servlet-name>
        <servlet-class>
                  org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springrest</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>


    <!-- Restlet adapter -->
    <servlet>
        <servlet-name>rservlet</servlet-name>
        <servlet-class>
             com.noelios.restlet.ext.spring.RestletFrameworkServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Catch all request URIs starting with  prefix /rest-->
    <servlet-mapping>
        <servlet-name>rservlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

RestletFrameworkServlet extends from FrameworkServlet which is basically created to provide integration with Spring application context. Therefore for the requests ending with .html suffix are handled by Dispatcher Servlet and the requests starting with the prefix /rest are handled by RestletFrameworkServlet. Service definitions for the business logic are kept in applicationContext.business.xml file which is loaded at the startup of the application and all the bean definitions in this file are accessible from both Spring controllers and Restlet resources.

The next should be done is creating the rservlet-servlet.xml file which will contain restlets and resources in it.This file is parsed by RestletFrameworkServlet.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

	<bean id="root" class="org.restlet.ext.spring.SpringRouter">
        <property name="attachments">
            <map>
                <entry key="/users/{user}">
                    <bean class="org.restlet.ext.spring.SpringFinder">
                        <lookup-method name="createResource" bean="userResource" />
                    </bean>
                </entry>
            </map>
        </property>
    </bean>

    <bean id="userResource" class="net.springrest.user.UserResource" scope="prototype" >
	<!-- userService is a service which is used by both your web controller and rest resource
                and it is defined in applicationContext-business.xml file -->
    	<property name="userService" ref="userService" />
    </bean>

</beans>

Comments (1)

SSL and Web Service

SOAP doesn’t deal with whether the connection is SSL or not. This is a HTTP issue,so there is nothing to do in web service implementation to secure the connection.
If a secure connection is needed to establish then there are thing to do. Assume that the web container is Tomcat server then in TOMCAT_HOME/conf/server.xml the connection attibute has to be uptaded to ensure that Tomcat server accepts ssl connection requests.
After that, the certificate has to be loaded to the client, to achieve this, keytool command is used
keytool -import -trustcacerts -alias FOO -file “FOO_PATH\foo.cer” -storetype JKS -keystore “FOO_PATH\foo.keystore”
Secondly the code below has to be added to just before getting connetion to the server.


System.setProperty("javax.net.ssl.keyStore", "FOO_PATH/foo.keystore");
System.setProperty("javax.net.ssl.keyStorePassword","changeit");
System.setProperty("javax.net.ssl.keyStoreType","JKS");
System.setProperty("javax.net.ssl.trustStore", "FOO_PATH/foo.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Leave a Comment