/*
 * This is part of Geomajas, a GIS framework, http://www.geomajas.org/.
 *
 * Copyright 2008-2011 Geosparc nv, http://www.geosparc.com/, Belgium.
 *
 * The program is available in open source according to the GNU Affero
 * General Public License. All contributions in this program are covered
 * by the Geomajas Contributors License Agreement. For full licensing
 * details, see LICENSE.txt in the project root.
 */
package org.geomajas.gwt.server.mvc;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.geomajas.command.CommandDispatcher;
import org.geomajas.command.CommandResponse;
import org.geomajas.gwt.client.GeomajasService;
import org.geomajas.gwt.client.command.GwtCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * Geomajas GWT controller, implements communication between GWT face and back-end.
 * 
 * @author Jan De Moerloose
 * @since 1.0.0
 */
@Controller("/geomajasService")
public class GeomajasController extends RemoteServiceServlet implements GeomajasService {

	private static final long serialVersionUID = 100L;

	@Autowired
	private CommandDispatcher commandDispatcher;

	@Autowired
	private ServletContext servletContext;

	/**
	 * Implements Spring Controller interface method.
	 * 
	 * Call GWT's RemoteService doPost() method and return null.
	 * 
	 * @param request current HTTP request
	 * @param response current HTTP response
	 * @return a ModelAndView to render, or null if handled directly
	 * @throws Exception in case of errors
	 */
	@RequestMapping("/geomajasService")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		doPost(request, response);
		return null;
	}

	/**
	 * Execute a GWT RPC command request, and return the response. These request come from the client, and the response
	 * is sent back to the client. We use a {@link CommandDispatcher} to actually execute the command.
	 */
	public CommandResponse execute(GwtCommand request) {
		if (request != null) {
			return commandDispatcher.execute(request.getCommandName(), request.getCommandRequest(),
					request.getUserToken(), request.getLocale());
		}
		return null;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

}
@@ -1482,32 +1482,50 @@
 ;%0D%0A%0D%0A%09@Autowired
+(required = false)
 %0D%0A%09private Servl
@@ -2616,16 +2616,144 @@
 xt() %7B%0D%0A
+%09%09if (servletContext == null) %7B%0D%0A%09%09%09throw new RuntimeException(%22getServletContext() cannot be used outside web context%22);%0D%0A%09%09%7D%0D%0A
 %09%09return

set required to false + added runtime exception