SpringHttpRequestProcessorServlet.java

  1. /*
  2.  * Copyright (C) 2012-2025 RRiBbit.org
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * https://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.rribbit.processing;

  17. import org.rribbit.execution.ListenerObjectExecutor;
  18. import org.rribbit.retrieval.ListenerObjectRetriever;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.context.ApplicationContext;
  22. import org.springframework.web.context.WebApplicationContext;
  23. import org.springframework.web.context.support.WebApplicationContextUtils;

  24. /**
  25.  * This implementation of {@link HttpRequestProcessorServlet} tries to retrieve a {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} from the
  26.  * Spring {@link WebApplicationContext}, if one is available. Use this class if you use Spring in combination with RRiBbit HTTP remoting. Then, you only have to wire up a
  27.  * {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} in your {@link ApplicationContext} and declare this class in your web.xml and the server side is done.
  28.  *
  29.  * @author G.J. Schouten
  30.  *
  31.  */
  32. public class SpringHttpRequestProcessorServlet extends HttpRequestProcessorServlet {

  33.     private static final Logger log = LoggerFactory.getLogger(SpringHttpRequestProcessorServlet.class);

  34.     protected ListenerObjectRetriever listenerObjectRetriever;
  35.     protected ListenerObjectExecutor listenerObjectExecutor;

  36.     @Override
  37.     protected ListenerObjectRetriever createListenerObjectRetriever() {
  38.         return listenerObjectRetriever;
  39.     }

  40.     @Override
  41.     protected ListenerObjectExecutor createListenerObjectExecutor() {
  42.         return listenerObjectExecutor;
  43.     }

  44.     @Override
  45.     public void init() {

  46.         log.info("Retrieving WebApplicationContext from ServletContext and getting ListenerObjectRetriever and ListenerObjectExecutor");

  47.         //First, retrieve the WebApplicationContext via the Servlet Container and get the ListenerObjectRetriever and ListenerObjectExecutor
  48.         WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
  49.         listenerObjectRetriever = ctx.getBean(ListenerObjectRetriever.class);
  50.         listenerObjectExecutor = ctx.getBean(ListenerObjectExecutor.class);

  51.         //Then call super, which in turn calls the createListenerObjectRetriever() and createListenerObjectExecutor() methods
  52.         super.init();
  53.     }
  54. }