From d13faf08158fede93e3486d3dd6b3baf8dbcd494 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Thu, 30 Jun 2005 21:23:50 +0000 Subject: [PATCH] Renaming and refactoring of web.xml converter. --- ...WebXmlSecurityToSpringBeansTranslator.java | 108 ---------------- .../util/WebXmlToAcegiSecurityConverter.java | 115 ++++++++++++++++++ ... WebXmlToAcegiSecurityConverterTests.java} | 47 +++++-- 3 files changed, 154 insertions(+), 116 deletions(-) delete mode 100644 core/src/main/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslator.java create mode 100644 core/src/main/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverter.java rename core/src/test/java/org/acegisecurity/util/{WebXmlSecurityToSpringBeansTranslatorTests.java => WebXmlToAcegiSecurityConverterTests.java} (57%) diff --git a/core/src/main/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslator.java b/core/src/main/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslator.java deleted file mode 100644 index b57b0bf17d..0000000000 --- a/core/src/main/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslator.java +++ /dev/null @@ -1,108 +0,0 @@ -package net.sf.acegisecurity.util; - -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.DOMImplementation; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.FileSystemResource; -import org.springframework.beans.factory.BeanFactory; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; -import org.xml.sax.SAXException; - -import javax.xml.transform.stream.StreamSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.Source; -import javax.xml.transform.Result; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.dom.DOMResult; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.ParserConfigurationException; -import java.io.IOException; -import java.io.FileOutputStream; -import java.io.InputStream; - - -/** - * A utility to translate a web.xml file into a set of - * acegi security spring beans. - * - *

- * This class wraps the XSL transform which actually does - * most of the work. It also tests the result by - * loading it into a Spring bean factory. - *

- * - * @author Luke Taylor - * @version $Id$ - */ -public class WebXmlSecurityToSpringBeansTranslator { - private String webToSpringXsltFile = "web-to-spring.xsl"; - private String outputFileName = "applicationContext-acegi-security.xml"; - private Transformer transformer, identityTransformer; - private DefaultListableBeanFactory beanFactory; - DocumentBuilderFactory dbf; - - public WebXmlSecurityToSpringBeansTranslator() throws Exception { - ClassPathResource resource = new ClassPathResource(webToSpringXsltFile); - Source xsltSource = new StreamSource(resource.getInputStream()); - dbf = DocumentBuilderFactory.newInstance(); - dbf.setNamespaceAware(true); - TransformerFactory tf = TransformerFactory.newInstance(); - transformer = tf.newTransformer(xsltSource); - identityTransformer = tf.newTransformer(); - identityTransformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//SPRING//DTD BEAN//EN"); - identityTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.springframework.org/dtd/spring-beans.dtd"); - } - - public void translate(InputStream in) throws TransformerException, IOException, ParserConfigurationException, SAXException { - DocumentBuilder db = dbf.newDocumentBuilder(); - Document d = db.parse(in); - translate(d); - } - - /** - * Converts the web.xml supplied as a DOM Node - * - * @param webXml the web application xml - */ - public void translate(Node webXml) throws IOException, TransformerException, ParserConfigurationException { - Source xmlSource = new DOMSource(webXml); - DOMResult domResult = new DOMResult(); - - transformer.transform(xmlSource, domResult); - - // Obtain DOM for additional manipulation here. - Node document = domResult.getNode(); - - // Tranform DOM with identity transform to get the output file - Result streamResult = new StreamResult(new FileOutputStream(outputFileName)); - xmlSource = new DOMSource(document); - identityTransformer.transform(xmlSource, streamResult); - beanFactory = new DefaultListableBeanFactory(); - XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(beanFactory); - beanReader.loadBeanDefinitions(new FileSystemResource(outputFileName)); - } - - public String getOutputFileName() { - return outputFileName; - } - - public void setOutputFileName(String outputFileName) { - this.outputFileName = outputFileName; - } - - /** - * Mainly intended for testing - * @return the bean factory built from the created acegi security application context file - * - */ - public BeanFactory getBeanFactory() { - return beanFactory; - } -} diff --git a/core/src/main/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverter.java b/core/src/main/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverter.java new file mode 100644 index 0000000000..26f0c19bed --- /dev/null +++ b/core/src/main/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverter.java @@ -0,0 +1,115 @@ +package net.sf.acegisecurity.util; + +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.Assert; +import org.w3c.dom.Node; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * A utility to translate a web.xml file into a set of acegi security spring beans. + * + * Also produces a new "acegified" web.xml file with the necessary filters installed + * and the security elements defined by the servlet DTD removed. + * + *

+ * This class wraps the XSL transform which actually does most of the work. + *

+ * + * @author Luke Taylor + * @version $Id$ + */ +public class WebXmlToAcegiSecurityConverter { + private static final String WEB_TO_SPRING_XSL_FILE = "web-to-spring.xsl"; + private static final String NEW_WEB_XSLT_FILE = "acegi-web.xsl"; + + private Transformer acegiSecurityTransformer, newWebXmlTransformer; + private DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + + /** + * The name of the spring-beans file which the beans will be stored in. + * This is required when writing the new web.xml file. + */ + private String acegiOutputFileName = "applicationContext-acegi-security.xml"; + + /** The web.xml content to be converted */ + private DOMSource xmlSource; + /** The results of the conversion */ + private String newWebXml, acegiBeansXml; + + public WebXmlToAcegiSecurityConverter() throws Exception { + TransformerFactory tf = TransformerFactory.newInstance(); + + acegiSecurityTransformer = tf.newTransformer(createTransformerSource(WEB_TO_SPRING_XSL_FILE)); + newWebXmlTransformer = tf.newTransformer(createTransformerSource(NEW_WEB_XSLT_FILE)); + } + + private Source createTransformerSource(String fileName) throws IOException { + ClassPathResource resource = new ClassPathResource(fileName); + return new StreamSource(resource.getInputStream()); + } + + /** + * Performs the transformations on the input source. + * Creates new web.xml content and a set of acegi-security Spring beans which can be + * accessed through the appropriate getter methods. + */ + public void doConversion() throws IOException, TransformerException { + Assert.notNull(xmlSource, "The XML input must be set, either as a Node or an InputStream"); + + // Create the modified web.xml file + ByteArrayOutputStream output = new ByteArrayOutputStream(); + newWebXmlTransformer.transform(xmlSource, new StreamResult(output)); + newWebXml = output.toString(); + output.reset(); + +// acegiSecurityTransformer.setParameter("cas-proxy-url", "http://localhost:8433/cas/proxy"); + acegiSecurityTransformer.setParameter("acegi-security-context-file", acegiOutputFileName); + acegiSecurityTransformer.transform(xmlSource, new StreamResult(output)); + acegiBeansXml = output.toString(); + } + + /** set the input as an InputStream */ + public void setInput(InputStream xmlIn) throws Exception { + DocumentBuilder db = dbf.newDocumentBuilder(); + setInput(db.parse(xmlIn)); + } + + /** set the input as an XML node */ + public void setInput(Node webXml) { + this.xmlSource = new DOMSource(webXml); + } + + public String getAcegiOutputFileName() { + return acegiOutputFileName; + } + + public void setAcegiOutputFileName(String acegiOutputFileName) { + this.acegiOutputFileName = acegiOutputFileName; + } + + /** Returns the converted web.xml content */ + public String getNewWebXml() { + return newWebXml; + } + + /** + * Returns the created spring-beans xml content which should be used in + * the application context file. + */ + public String getAcegiBeansXml() { + return acegiBeansXml; + } + +} diff --git a/core/src/test/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslatorTests.java b/core/src/test/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverterTests.java similarity index 57% rename from core/src/test/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslatorTests.java rename to core/src/test/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverterTests.java index 1c8625c0ac..f19fe97691 100644 --- a/core/src/test/java/org/acegisecurity/util/WebXmlSecurityToSpringBeansTranslatorTests.java +++ b/core/src/test/java/org/acegisecurity/util/WebXmlToAcegiSecurityConverterTests.java @@ -3,7 +3,10 @@ package net.sf.acegisecurity.util; import junit.framework.TestCase; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; -import org.springframework.beans.factory.BeanFactory; +import org.springframework.core.io.AbstractResource; +import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.w3c.dom.Node; import net.sf.acegisecurity.providers.ProviderManager; import net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider; @@ -13,22 +16,30 @@ import net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter; import net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.IOException; + /** - * Tests the WebXmlSecurityToSpringBeansTranslator by applying it - * to a test sample web.xml file. + * Tests the WebXmlToAcegiSecurityConverter by applying it to a sample web.xml file. * * @author Luke Taylor * @version $Id$ */ -public class WebXmlSecurityToSpringBeansTranslatorTests extends TestCase { +public class WebXmlToAcegiSecurityConverterTests extends TestCase { - public void testFileTranslation() throws Exception { - WebXmlSecurityToSpringBeansTranslator t = new WebXmlSecurityToSpringBeansTranslator(); + public void testFileConversion() throws Exception { + WebXmlToAcegiSecurityConverter t = new WebXmlToAcegiSecurityConverter(); Resource r = new ClassPathResource("test-web.xml"); - t.translate(r.getInputStream()); + t.setInput(r.getInputStream()); + t.doConversion(); + + + DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); + XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf); - BeanFactory bf = t.getBeanFactory(); + int nBeans = beanReader.loadBeanDefinitions(new InMemoryResource(t.getAcegiBeansXml())); assertNotNull(bf.getBean("filterChainProxy")); ProviderManager pm = (ProviderManager) bf.getBean("authenticationManager"); @@ -56,4 +67,24 @@ public class WebXmlSecurityToSpringBeansTranslatorTests extends TestCase { FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor(); } + + private static class InMemoryResource extends AbstractResource { + ByteArrayInputStream in; + + public InMemoryResource(ByteArrayInputStream in) { + this.in = in; + } + + public InMemoryResource(String source) { + in = new ByteArrayInputStream(source.getBytes()); + } + + public String getDescription() { + return in.toString(); + } + + public InputStream getInputStream() throws IOException { + return in; + } + } }