|
1 |
| package acegifier.web; |
|
2 |
| |
|
3 |
| import java.io.ByteArrayOutputStream; |
|
4 |
| import java.io.IOException; |
|
5 |
| import java.util.HashMap; |
|
6 |
| import java.util.Map; |
|
7 |
| |
|
8 |
| import javax.servlet.http.HttpServletRequest; |
|
9 |
| import javax.servlet.http.HttpServletResponse; |
|
10 |
| import javax.xml.transform.TransformerException; |
|
11 |
| |
|
12 |
| import org.acegisecurity.util.FilterChainProxy; |
|
13 |
| import org.acegisecurity.util.InMemoryResource; |
|
14 |
| import org.dom4j.Document; |
|
15 |
| import org.dom4j.DocumentException; |
|
16 |
| import org.dom4j.io.OutputFormat; |
|
17 |
| import org.dom4j.io.XMLWriter; |
|
18 |
| import org.springframework.beans.BeansException; |
|
19 |
| import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
|
20 |
| import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; |
|
21 |
| import org.springframework.validation.BindException; |
|
22 |
| import org.springframework.validation.Errors; |
|
23 |
| import org.springframework.web.servlet.ModelAndView; |
|
24 |
| import org.springframework.web.servlet.mvc.SimpleFormController; |
|
25 |
| |
|
26 |
| import acegifier.WebXmlConverter; |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| public class AcegifierController extends SimpleFormController { |
|
36 |
| |
|
37 |
0
| public AcegifierController() {
|
|
38 |
| } |
|
39 |
| |
|
40 |
0
| public ModelAndView onSubmit(
|
|
41 |
| HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) |
|
42 |
| throws Exception { |
|
43 |
| |
|
44 |
0
| AcegifierForm conversion = (AcegifierForm)command;
|
|
45 |
0
| WebXmlConverter converter = new WebXmlConverter();
|
|
46 |
0
| int nBeans = 0;
|
|
47 |
0
| Document newWebXml = null, acegiBeans = null;
|
|
48 |
| |
|
49 |
0
| try {
|
|
50 |
0
| converter.setInput(conversion.getWebXml());
|
|
51 |
0
| converter.doConversion();
|
|
52 |
0
| newWebXml = converter.getNewWebXml();
|
|
53 |
0
| acegiBeans = converter.getAcegiBeans();
|
|
54 |
0
| nBeans = validateAcegiBeans(conversion, acegiBeans, errors);
|
|
55 |
| } catch (DocumentException de) { |
|
56 |
0
| errors.rejectValue("webXml","webXmlDocError","There was a problem with your web.xml: " + de.getMessage());
|
|
57 |
| } catch (TransformerException te) { |
|
58 |
0
| errors.rejectValue("webXml","transFailure","There was an error during the XSL transformation: " + te.getMessage());
|
|
59 |
| } |
|
60 |
| |
|
61 |
0
| if(errors.hasErrors()) {
|
|
62 |
0
| return showForm(request, response, errors);
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| Map model = new HashMap();
|
|
66 |
0
| model.put("webXml", prettyPrint(newWebXml));
|
|
67 |
0
| model.put("acegiBeansXml", prettyPrint(acegiBeans));
|
|
68 |
0
| model.put("nBeans", new Integer(nBeans));
|
|
69 |
| |
|
70 |
0
| return new ModelAndView("acegificationResults", model);
|
|
71 |
| } |
|
72 |
| |
|
73 |
| |
|
74 |
0
| private String prettyPrint(Document document) throws IOException {
|
|
75 |
0
| ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
76 |
0
| OutputFormat format = OutputFormat.createPrettyPrint();
|
|
77 |
0
| format.setTrimText(false);
|
|
78 |
0
| XMLWriter writer = new XMLWriter(output, format);
|
|
79 |
0
| writer.write(document);
|
|
80 |
0
| writer.flush();
|
|
81 |
0
| writer.close();
|
|
82 |
0
| return output.toString();
|
|
83 |
| } |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
0
| private int validateAcegiBeans(AcegifierForm conversion, Document beans, Errors errors) {
|
|
90 |
0
| DefaultListableBeanFactory bf = createBeanFactory(beans);
|
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
0
| try {
|
|
95 |
0
| bf.getBean("filterChainProxy", FilterChainProxy.class);
|
|
96 |
| } catch (BeansException be) { |
|
97 |
0
| errors.rejectValue("webXml","beansInvalid","There was an error creating or accessing the bean factory " + be.getMessage());
|
|
98 |
| } |
|
99 |
0
| return bf.getBeanDefinitionCount();
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
0
| private DefaultListableBeanFactory createBeanFactory(Document beans) {
|
|
104 |
0
| DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
|
105 |
0
| XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf);
|
|
106 |
0
| beanReader.loadBeanDefinitions(new InMemoryResource(beans.asXML().getBytes()));
|
|
107 |
| |
|
108 |
0
| return bf;
|
|
109 |
| } |
|
110 |
| |
|
111 |
| } |