diff --git a/samples/acegifier/src/java/acegifier/web/AcegifierController.java b/samples/acegifier/src/java/acegifier/web/AcegifierController.java new file mode 100644 index 0000000000..47b7d12f34 --- /dev/null +++ b/samples/acegifier/src/java/acegifier/web/AcegifierController.java @@ -0,0 +1,81 @@ +package acegifier.web; + +import org.springframework.web.servlet.mvc.SimpleFormController; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.validation.BindException; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.BeansException; +import net.sf.acegisecurity.util.InMemoryResource; +import org.w3c.dom.Document; +import org.xml.sax.SAXParseException; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.ByteArrayInputStream; +import java.util.HashMap; +import java.util.Map; + +import net.sf.acegisecurity.util.WebXmlToAcegiSecurityConverter; + +/** + * Takes a submitted web.xml, applies the transformer to it and returns the resulting + * modified web.xml and acegi-app-context.xml file contents. + * + * @author Luke Taylor + * @version $Id$ + */ +public class AcegifierController extends SimpleFormController { + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + + public AcegifierController() { + dbf.setValidating(false); + } + + public ModelAndView onSubmit( + HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) + throws Exception { + + AcegifierForm conversion = (AcegifierForm)command; + + ByteArrayInputStream in = new ByteArrayInputStream(conversion.getWebXml().getBytes()); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document doc = null; + WebXmlToAcegiSecurityConverter converter = null; + int nBeans = 0; + + try { + doc = db.parse(in); + converter = new WebXmlToAcegiSecurityConverter(); + converter.setInput(doc); + converter.doConversion(); + nBeans = createBeanFactory(converter.getAcegiBeansXml()); + } catch (SAXParseException spe) { + errors.rejectValue("webXml","parseFailure","Your Web XML Document failed to parse: " + spe.getMessage()); + } catch (BeansException be) { + errors.rejectValue("webXml","invalidBeans","There was a problem validating the Spring beans: " + be.getMessage()); + } + + if(errors.hasErrors()) { + return showForm(request, response, errors); + } + + Map model = new HashMap(); + model.put("webXml", converter.getNewWebXml()); + model.put("acegiBeansXml", converter.getAcegiBeansXml()); + model.put("nBeans", new Integer(nBeans)); + + return new ModelAndView("acegificationResults", model); + } + + /** Creates a BeanFactory from the transformed XML to make sure the results are valid */ + private int createBeanFactory(String beansXml) { + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf); + + return beanReader.loadBeanDefinitions(new InMemoryResource(beansXml)); + } + +} diff --git a/samples/acegifier/src/java/acegifier/web/AcegifierForm.java b/samples/acegifier/src/java/acegifier/web/AcegifierForm.java new file mode 100644 index 0000000000..dfb6730242 --- /dev/null +++ b/samples/acegifier/src/java/acegifier/web/AcegifierForm.java @@ -0,0 +1,19 @@ +package acegifier.web; + +/** + * Form backing object for the Acegifier controller. + * + * @author Luke Taylor + * @version $Id$ + */ +public class AcegifierForm { + private String webXml; + + public String getWebXml() { + return webXml; + } + + public void setWebXml(String webXml) { + this.webXml = webXml; + } +} diff --git a/samples/acegifier/src/webapp/WEB-INF/acegifier-servlet.xml b/samples/acegifier/src/webapp/WEB-INF/acegifier-servlet.xml new file mode 100644 index 0000000000..470ada63fd --- /dev/null +++ b/samples/acegifier/src/webapp/WEB-INF/acegifier-servlet.xml @@ -0,0 +1,56 @@ + + + + + + + + messages + + + + + + + + + + + + conversionController + + + + + + /WEB-INF/freemarker/ + + + + + + + + + + + true + true + + .ftl + + + \ No newline at end of file diff --git a/samples/acegifier/src/webapp/WEB-INF/applicationContext.xml b/samples/acegifier/src/webapp/WEB-INF/applicationContext.xml new file mode 100644 index 0000000000..98bf4d0d5f --- /dev/null +++ b/samples/acegifier/src/webapp/WEB-INF/applicationContext.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationForm.ftl b/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationForm.ftl new file mode 100644 index 0000000000..205463d778 --- /dev/null +++ b/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationForm.ftl @@ -0,0 +1,20 @@ + + + +<#import "spring.ftl" as spring /> + + + + Acegi Security Web.xml Converter + + +
+ <@spring.bind "command.webXml" /> + +
+ <@spring.showErrors "
"/> + +
+ + + \ No newline at end of file diff --git a/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationResults.ftl b/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationResults.ftl new file mode 100644 index 0000000000..eb16ab760b --- /dev/null +++ b/samples/acegifier/src/webapp/WEB-INF/freemarker/acegificationResults.ftl @@ -0,0 +1,39 @@ + + + + +Acegi Security Web.xml Converter + + + +

Congratulations! Your web.xml file has been "Acegified" successfully.

+ +

Web.xml

+

+This is the converted web.xml file which you should use in your Acegi-Secured +Spring application. It should contain the mechanism for loading the Spring application +context file which defines your security configuration as well as the +necessary filters to apply this configuration. +

+ +
+${webXml?xml}
+
+ +

Acegi Security Beans

+

+This is the file which defines your security configuration (a standard Spring +application context file). It should be named "applicationContext-acegi-security.xml" +and placed in your WEB-INF directory. +

+ +
+${acegiBeansXml?xml}
+
+ +

Note that these files may require some manual changes before they work as expected and are +intended as a guide only :).

+ + + + \ No newline at end of file diff --git a/samples/acegifier/src/webapp/WEB-INF/web.xml b/samples/acegifier/src/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..98c395fc9b --- /dev/null +++ b/samples/acegifier/src/webapp/WEB-INF/web.xml @@ -0,0 +1,46 @@ + + + + + + contextConfigLocation + + /WEB-INF/applicationContext.xml + + + + + + + org.springframework.web.context.ContextLoaderListener + + + + org.springframework.web.context.ContextLoaderListener + + + + + acegifier + org.springframework.web.servlet.DispatcherServlet + 1 + + + + acegifier + *.htm + + +