|
1 |
| package acegifier; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.InputStream; |
|
5 |
| import java.util.List; |
|
6 |
| |
|
7 |
| import javax.xml.transform.Source; |
|
8 |
| import javax.xml.transform.Transformer; |
|
9 |
| import javax.xml.transform.TransformerConfigurationException; |
|
10 |
| import javax.xml.transform.TransformerException; |
|
11 |
| import javax.xml.transform.TransformerFactory; |
|
12 |
| import javax.xml.transform.stream.StreamSource; |
|
13 |
| |
|
14 |
| import org.dom4j.Document; |
|
15 |
| import org.dom4j.DocumentException; |
|
16 |
| import org.dom4j.DocumentHelper; |
|
17 |
| import org.dom4j.Node; |
|
18 |
| import org.dom4j.io.DocumentResult; |
|
19 |
| import org.dom4j.io.DocumentSource; |
|
20 |
| import org.dom4j.io.SAXReader; |
|
21 |
| import org.springframework.core.io.ClassPathResource; |
|
22 |
| import org.springframework.util.Assert; |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| public class WebXmlConverter { |
|
38 |
| private static final String WEB_TO_SPRING_XSL_FILE = "web-to-spring.xsl"; |
|
39 |
| private static final String NEW_WEB_XSLT_FILE = "acegi-web.xsl"; |
|
40 |
| |
|
41 |
| private Transformer acegiSecurityTransformer, newWebXmlTransformer; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| private String acegiOutputFileName = "applicationContext-acegi-security.xml"; |
|
48 |
| |
|
49 |
| |
|
50 |
| private Source xmlSource; |
|
51 |
| |
|
52 |
| private Document newWebXml, acegiBeansXml; |
|
53 |
| |
|
54 |
1
| public WebXmlConverter() throws IOException, TransformerConfigurationException {
|
|
55 |
1
| TransformerFactory tf = TransformerFactory.newInstance();
|
|
56 |
1
| Source source = createTransformerSource(WEB_TO_SPRING_XSL_FILE);
|
|
57 |
1
| System.out.println("1");
|
|
58 |
1
| acegiSecurityTransformer = tf.newTransformer(source);
|
|
59 |
1
| System.out.println("2");
|
|
60 |
1
| newWebXmlTransformer = tf.newTransformer(createTransformerSource(NEW_WEB_XSLT_FILE));
|
|
61 |
1
| System.out.println("3");
|
|
62 |
| } |
|
63 |
| |
|
64 |
2
| private Source createTransformerSource(String fileName) throws IOException {
|
|
65 |
2
| ClassPathResource resource = new ClassPathResource(fileName);
|
|
66 |
2
| Source source = new StreamSource(resource.getInputStream());
|
|
67 |
2
| return source;
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
1
| public void doConversion() throws IOException, TransformerException {
|
|
76 |
1
| Assert.notNull(xmlSource, "The XML input must be set");
|
|
77 |
| |
|
78 |
| |
|
79 |
1
| newWebXmlTransformer.setParameter("acegi-security-context-file", acegiOutputFileName);
|
|
80 |
| |
|
81 |
1
| DocumentResult result = new DocumentResult();
|
|
82 |
1
| newWebXmlTransformer.transform(xmlSource, result);
|
|
83 |
1
| newWebXml = result.getDocument();
|
|
84 |
| |
|
85 |
1
| result = new DocumentResult();
|
|
86 |
1
| acegiSecurityTransformer.transform(xmlSource, result);
|
|
87 |
1
| acegiBeansXml = result.getDocument();
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
0
| public void setInput(String xml) throws DocumentException {
|
|
92 |
0
| setInput(DocumentHelper.parseText(xml));
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
1
| public void setInput(InputStream in) throws DocumentException {
|
|
97 |
1
| SAXReader reader = new SAXReader();
|
|
98 |
1
| setInput(reader.read(in));
|
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
1
| public void setInput(Document document) throws DocumentException {
|
|
103 |
1
| validateWebXml(document);
|
|
104 |
1
| xmlSource = new DocumentSource(document);
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
1
| private void validateWebXml(Document document) throws DocumentException {
|
|
109 |
1
| Node authMethodNode =
|
|
110 |
| document.selectSingleNode("/web-app/login-config/auth-method"); |
|
111 |
1
| if(authMethodNode == null)
|
|
112 |
0
| throw new DocumentException("login-config and auth-method must be present");
|
|
113 |
1
| String authMethod = authMethodNode.getStringValue().toUpperCase();
|
|
114 |
1
| if(!authMethod.equals("BASIC") && !authMethod.equals("FORM")) {
|
|
115 |
0
| throw new DocumentException("unsupported auth-method: " + authMethod);
|
|
116 |
| } |
|
117 |
1
| List roles = document.selectNodes("/web-app/security-role");
|
|
118 |
1
| if(roles.isEmpty()) {
|
|
119 |
0
| throw new DocumentException("Each role used must be defined in a security-role element");
|
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
0
| public String getAcegiOutputFileName() {
|
|
124 |
0
| return acegiOutputFileName;
|
|
125 |
| } |
|
126 |
| |
|
127 |
0
| public void setAcegiOutputFileName(String acegiOutputFileName) {
|
|
128 |
0
| this.acegiOutputFileName = acegiOutputFileName;
|
|
129 |
| } |
|
130 |
| |
|
131 |
| |
|
132 |
1
| public Document getNewWebXml() {
|
|
133 |
1
| return newWebXml;
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
2
| public Document getAcegiBeans() {
|
|
141 |
2
| return acegiBeansXml;
|
|
142 |
| } |
|
143 |
| } |