Abrakadabra - here is your SpringServerServlet - feel free to use it ;-)

usage:

<!-- The Restlet-Application Spring bean that will used by the Restlet SpringServerServlet. -->
   <context-param>
       <param-name>org.restlet.application</param-name>
       <param-value>beanNameOfTheApplicationToUse</param-value>
   </context-param>
<!-- The Restlet-Component Spring bean that will used by the Restlet SpringServerServlet. -->
   <context-param>
       <param-name>org.restlet.component</param-name>
       <param-value>beanNameOfTheComponentToUse</param-value>
   </context-param>
<!-- Restlet adapter -->
   <servlet>
       <servlet-name>springRestletServlet</servlet-name>
<servlet-class>org.restlet.ext.spring.SpringServerServlet</servlet-class>
   </servlet>

   <!-- Mapping to the Restlet stuff -->
   <servlet-mapping>
       <servlet-name>springRestletServlet</servlet-name>
       <url-pattern>/ws/rest/v1.0/*</url-pattern>
   </servlet-mapping>






package org.restlet.ext.spring;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Context;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.noelios.restlet.application.ApplicationContext;
import com.noelios.restlet.ext.servlet.ServerServlet;
import com.noelios.restlet.ext.servlet.ServletContextAdapter;

import infrastructure.errorhandling.exception.InitializationException;

/**
* This class is similiar to the ServerServlet, but instead of creating the used Application-Restlet and
* Component-Restlet it lookups them from a Spring WebApplicationContext.
*
* @author Nebu
*/
public class SpringServerServlet extends ServerServlet {

   private static final long serialVersionUID = 110030403435929871L;

   /**
* Name of the attribute key containing a bean-id of the application to use.
    */
   public static final String APPLICATION_KEY = "org.restlet.application";
/** * Name of the attribute key containing a bean-id of the component to use.
    */
   public static final String COMPONENT_KEY = "org.restlet.component";
/** * Lookups the single Restlet-Application used by this Servlet from the SpringContext inside the ServletContext.
    *
    * @param context The Context for the Application.
    *
* @return The Restlet-Application to use or null if unable to lookup or create.
    */
   @Override
   public Application createApplication(Context context) {
       Application application = null;
String applicationBeanName = getInitParameter(SpringServerServlet.APPLICATION_KEY, null); application = (Application) getWebApplicationContext().getBean(applicationBeanName);

       if (application != null) {
           // Set the context based on the Servlet's context
ApplicationContext applicationContext = (ApplicationContext) application.getContext(); application.setContext(new ApplicationContext(application, new ServletContextAdapter(this, context), applicationContext.getLogger()));
       } else {
throw new InitializationException("Initialization of the Restlet application failed. Check your web.xml and your Spring-configuration");
       }

       return application;
   }
/** * Lookups the single RestletComponent used by this Servlet from the SpringContext inside the ServletContext.
    *
* @return The Restlet-Component to use or null if unable to lookup or create.
    */

   @Override
   public Component createComponent() {
       Component component = null;
String componentBeanName = getInitParameter(SpringServerServlet.COMPONENT_KEY, null); component = (Component) getWebApplicationContext().getBean(componentBeanName); if (component == null) throw new InitializationException("Initialization of the Restlet component failed. Check your web.xml and your Spring-configuration"); return component;
   }
public WebApplicationContext getWebApplicationContext() {
       // by hand would be
// webApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
   }
}



Barry schrieb:
Hi all,

I'm quite new to this so let me know if I'm missing something obvious.

I'm trying to create a Spring configured Restlet Application that will run in a servlet container.

As far as I can see I don't think this is supported (as the ServerServlet creates the application). Is there a way to do this?

If not I may create a SpringServerServlet that looks the Application that willget the application from the spring config.

Or alternatively create an an application that proxies its calls to a application looked up from the spring context.

Which sounds better (or are they both horrible)?

Barry



Reply via email to