<commit_before>/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * 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
 * 
 *      http://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.codehaus.groovy.grails.web.servlet.mvc;

import groovy.lang.Closure;
import groovy.lang.GroovyObject;

import java.util.Map;

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

import org.apache.commons.lang.WordUtils;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsControllerClass;
import org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.IncompatibleParameterCountException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.NoClosurePropertyForURIException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.NoViewNameDefinedException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.UnknownControllerException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.UnsupportedReturnValueException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.util.UrlPathHelper;

/**
 * <p>Base class for Grails controllers.
 * 
 * @author <NAME>jver
 * @since Jul 2, 2005
 */
public class SimpleGrailsController implements Controller, ApplicationContextAware {

	private static final String SLASH = "/";
	
	private UrlPathHelper urlPathHelper = new UrlPathHelper();
	private GrailsApplication application = null;
	private ApplicationContext applicationContext = null;
	
	public SimpleGrailsController() {
		super();
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void setGrailsApplication(GrailsApplication application) {
		this.application = application;
	}
	
	/**
	 * <p>This method wraps regular request and response objects into Grails request and response objects.
	 * 
	 * <p>It can handle maps as model types next to ModelAndView instances.
	 * 
	 * @param request HTTP request
	 * @param response HTTP response
	 * @return the model
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		// Step 1: determine the name of the controller.
		// This maps to a slash + the name of the controller.
		String uri = this.urlPathHelper.getRequestUri(request);
		
		if (uri.indexOf("?") > -1) {
			uri = uri.substring(0, uri.indexOf("?"));
		}
		
		// Step 2: lookup the controller in the application.
		GrailsControllerClass controllerClass = this.application.getControllerByURI(uri);
		if (controllerClass == null) {
			throw new UnknownControllerException("No controller found for URI [" + uri + "]!");
		}
		String controllerName = WordUtils.uncapitalize(controllerClass.getName());
		
		// Step 3: load controller from application context.
		GroovyObject controller = (GroovyObject)this.applicationContext.getBean(controllerClass.getFullName());
		
		// Step 4: get closure property name for URI.
		String closurePropertyName = controllerClass.getClosurePropertyName(uri);
		if (closurePropertyName == null) {<commit_msg>Fix problem with URI handling.<commit_after>/*
 * Copyright 2004-2005 the original author or authors.
 * 
 * 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
 * 
 *      http://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.codehaus.groovy.grails.web.servlet.mvc;

import groovy.lang.Closure;
import groovy.lang.GroovyObject;

import java.util.Map;

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

import org.apache.commons.lang.WordUtils;
import org.codehaus.groovy.grails.commons.GrailsApplication;
import org.codehaus.groovy.grails.commons.GrailsControllerClass;
import org.codehaus.groovy.grails.web.servlet.GrailsHttpServletRequest;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.IncompatibleParameterCountException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.NoClosurePropertyForURIException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.NoViewNameDefinedException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.UnknownControllerException;
import org.codehaus.groovy.grails.web.servlet.mvc.exceptions.UnsupportedReturnValueException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.util.UrlPathHelper;

/**
 * <p>Base class for Grails controllers.
 * 
 * @author <NAME>
 * @since Jul 2, 2005
 */
public class SimpleGrailsController implements Controller, ApplicationContextAware {

	private static final String SLASH = "/";
	
	private UrlPathHelper urlPathHelper = new UrlPathHelper();
	private GrailsApplication application = null;
	private ApplicationContext applicationContext = null;
	
	public SimpleGrailsController() {
		super();
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
	
	public void setGrailsApplication(GrailsApplication application) {
		this.application = application;
	}
	
	/**
	 * <p>This method wraps regular request and response objects into Grails request and response objects.
	 * 
	 * <p>It can handle maps as model types next to ModelAndView instances.
	 * 
	 * @param request HTTP request
	 * @param response HTTP response
	 * @return the model
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		// Step 1: determine the correct URI of the request.
		String uri = this.urlPathHelper.getLookupPathForRequest(request);
		
		if (uri.indexOf("?") > -1) {
			uri = uri.substring(0, uri.indexOf("?"));
		}
		
		// Step 2: lookup the controller in the application.
		GrailsControllerClass controllerClass = this.application.getControllerByURI(uri);
		if (controllerClass == null) {
			throw new UnknownControllerException("No controller found for URI [" + uri + "]!");
		}
		String controllerName = WordUtils.uncapitalize(controllerClass.getName());
		
		// Step 3: load controller from application context.
		GroovyObject controller = (GroovyObject)this.applicationContext.getBean(controllerClass.getFullName());
		
		// Step 4: get closure property name for URI.
		String closurePropertyName = controllerClass.getClosurePropertyName(uri);
		if (closurePropertyName == null) {
{'fraction_non_alphanumeric': 0.08840413318025259, 'fraction_numerical': 0.005868095420334226, 'mean_word_length': 5.003062787136294, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5454688', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2080, 'n_words': 595}