001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package acegifier.web;
017:
018: import acegifier.WebXmlConverter;
019:
020: import org.acegisecurity.util.FilterChainProxy;
021: import org.acegisecurity.util.InMemoryResource;
022:
023: import org.dom4j.Document;
024: import org.dom4j.DocumentException;
025:
026: import org.dom4j.io.OutputFormat;
027: import org.dom4j.io.XMLWriter;
028:
029: import org.springframework.beans.BeansException;
030: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
031: import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
032:
033: import org.springframework.validation.BindException;
034: import org.springframework.validation.Errors;
035:
036: import org.springframework.web.servlet.ModelAndView;
037: import org.springframework.web.servlet.mvc.SimpleFormController;
038:
039: import java.io.ByteArrayOutputStream;
040: import java.io.IOException;
041:
042: import java.util.HashMap;
043: import java.util.Map;
044:
045: import javax.servlet.http.HttpServletRequest;
046: import javax.servlet.http.HttpServletResponse;
047:
048: import javax.xml.transform.TransformerException;
049:
050: /**
051: * Takes a submitted web.xml, applies the transformer to it and returns the resulting modified web.xml and
052: * acegi-app-context.xml file contents.
053: *
054: * @author Luke Taylor
055: * @version $Id: AcegifierController.java 1496 2006-05-23 13:38:33Z benalex $
056: */
057: public class AcegifierController extends SimpleFormController {
058: //~ Constructors ===================================================================================================
059:
060: public AcegifierController() {
061: }
062:
063: //~ Methods ========================================================================================================
064:
065: /**
066: * Creates a BeanFactory from the spring beans XML document
067: *
068: * @param beans DOCUMENT ME!
069: *
070: * @return DOCUMENT ME!
071: */
072: private DefaultListableBeanFactory createBeanFactory(Document beans) {
073: DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
074: XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(
075: bf);
076: beanReader.loadBeanDefinitions(new InMemoryResource(beans
077: .asXML().getBytes()));
078:
079: return bf;
080: }
081:
082: public ModelAndView onSubmit(HttpServletRequest request,
083: HttpServletResponse response, Object command,
084: BindException errors) throws Exception {
085: AcegifierForm conversion = (AcegifierForm) command;
086: WebXmlConverter converter = new WebXmlConverter();
087: int nBeans = 0;
088: Document newWebXml = null;
089: Document acegiBeans = null;
090:
091: try {
092: converter.setInput(conversion.getWebXml());
093: converter.doConversion();
094: newWebXml = converter.getNewWebXml();
095: acegiBeans = converter.getAcegiBeans();
096: nBeans = validateAcegiBeans(conversion, acegiBeans, errors);
097: } catch (DocumentException de) {
098: errors.rejectValue("webXml", "webXmlDocError",
099: "There was a problem with your web.xml: "
100: + de.getMessage());
101: } catch (TransformerException te) {
102: errors.rejectValue("webXml", "transFailure",
103: "There was an error during the XSL transformation: "
104: + te.getMessage());
105: }
106:
107: if (errors.hasErrors()) {
108: return showForm(request, response, errors);
109: }
110:
111: Map model = new HashMap();
112: model.put("webXml", prettyPrint(newWebXml));
113: model.put("acegiBeansXml", prettyPrint(acegiBeans));
114: model.put("nBeans", new Integer(nBeans));
115:
116: return new ModelAndView("acegificationResults", model);
117: }
118:
119: /**
120: * Creates a formatted XML string from the supplied document
121: *
122: * @param document DOCUMENT ME!
123: *
124: * @return DOCUMENT ME!
125: *
126: * @throws IOException DOCUMENT ME!
127: */
128: private String prettyPrint(Document document) throws IOException {
129: ByteArrayOutputStream output = new ByteArrayOutputStream();
130: OutputFormat format = OutputFormat.createPrettyPrint();
131: format.setTrimText(false);
132:
133: XMLWriter writer = new XMLWriter(output, format);
134: writer.write(document);
135: writer.flush();
136: writer.close();
137:
138: return output.toString();
139: }
140:
141: /**
142: * Validates the acegi beans, based on the input form data, and returns the number of spring beans defined
143: * in the document.
144: *
145: * @param conversion DOCUMENT ME!
146: * @param beans DOCUMENT ME!
147: * @param errors DOCUMENT ME!
148: *
149: * @return DOCUMENT ME!
150: */
151: private int validateAcegiBeans(AcegifierForm conversion,
152: Document beans, Errors errors) {
153: DefaultListableBeanFactory bf = createBeanFactory(beans);
154:
155: //TODO: actually do some proper validation!
156: try {
157: bf.getBean("filterChainProxy", FilterChainProxy.class);
158: } catch (BeansException be) {
159: errors.rejectValue("webXml", "beansInvalid",
160: "There was an error creating or accessing the bean factory "
161: + be.getMessage());
162: }
163:
164: return bf.getBeanDefinitionCount();
165: }
166: }
|