3 changed files with 154 additions and 116 deletions
@ -1,108 +0,0 @@
@@ -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. |
||||
* |
||||
* <p> |
||||
* 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. |
||||
* </p> |
||||
* |
||||
* @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; |
||||
} |
||||
} |
||||
@ -0,0 +1,115 @@
@@ -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. |
||||
* |
||||
* <p> |
||||
* This class wraps the XSL transform which actually does most of the work. |
||||
* </p> |
||||
* |
||||
* @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; |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue