SpringHttpRequestProcessorServlet.java

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

import org.rribbit.execution.ListenerObjectExecutor;
import org.rribbit.retrieval.ListenerObjectRetriever;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * This implementation of {@link HttpRequestProcessorServlet} tries to retrieve a {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} from the
 * 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
 * {@link ListenerObjectRetriever} and a {@link ListenerObjectExecutor} in your {@link ApplicationContext} and declare this class in your web.xml and the server side is done.
 *
 * @author G.J. Schouten
 *
 */
public class SpringHttpRequestProcessorServlet extends HttpRequestProcessorServlet {

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

	protected ListenerObjectRetriever listenerObjectRetriever;
	protected ListenerObjectExecutor listenerObjectExecutor;

	@Override
	protected ListenerObjectRetriever createListenerObjectRetriever() {
		return listenerObjectRetriever;
	}

	@Override
	protected ListenerObjectExecutor createListenerObjectExecutor() {
		return listenerObjectExecutor;
	}

	@Override
	public void init() {

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

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

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