/*
* Copyright 2002-2007 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.internna.iwebmvc.spring.mvc.controllers;
import javax.servlet.ServletContext;
import org.internna.iwebmvc.core.crypto.Decipherer;
import org.internna.iwebmvc.utils.Assert;
import org.internna.iwebmvc.utils.ClassUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.support.ServletContextResource;
/**
* A simple controller that just redirects to search view.
*
* @author Jose Noheda
* @since 1.0
*/
@Controller
@RequestMapping("/filter.iwebmvc")
public class SearchController implements ServletContextAware {
@Autowired protected Decipherer decipherer;
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
protected String getView(String className) {
String path = "crud/" + className.replaceAll("\\.", "/").toLowerCase() + "_search";
ServletContextResource resource = new ServletContextResource(servletContext, "WEB-INF/jsp/" + path + ".jsp");
return resource.exists() ? path : "crud/template_search";
}
@RequestMapping(method = RequestMethod.GET)
public String redirect(@RequestParam("className") String className, ModelMap model) throws Exception {
Assert.isEncrypted(decipherer, className);
model.addAttribute("className", className);
String dec = decipherer.decrypt(className);
model.addAttribute("deciphered", dec);
model.addAttribute("entity", ClassUtils.instantiateClass(dec));
return getView(dec);
}
}