View Javadoc

    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   * Takes a submitted web.xml, applies the transformer to it and returns the resulting
    30   * modified web.xml and acegi-app-context.xml file contents.
    31   *
    32   * @author Luke Taylor
    33   * @version $Id: AcegifierController.java,v 1.2 2005/11/29 17:14:56 luke_t Exp $
    34   */
    35  public class AcegifierController extends SimpleFormController {
    36
    37      public AcegifierController() {
    38      }
    39
    40      public ModelAndView onSubmit(
    41              HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    42                  throws Exception {
    43
    44          AcegifierForm conversion = (AcegifierForm)command;
    45          WebXmlConverter converter = new WebXmlConverter();
    46          int nBeans = 0;
    47          Document newWebXml = null, acegiBeans = null;
    48
    49          try {
    50              converter.setInput(conversion.getWebXml());
    51              converter.doConversion();
    52              newWebXml = converter.getNewWebXml();
    53              acegiBeans = converter.getAcegiBeans();
    54              nBeans = validateAcegiBeans(conversion, acegiBeans, errors);
    55          } catch (DocumentException de) {
    56              errors.rejectValue("webXml","webXmlDocError","There was a problem with your web.xml: " + de.getMessage());
    57          } catch (TransformerException te) {
    58              errors.rejectValue("webXml","transFailure","There was an error during the XSL transformation: " + te.getMessage());
    59          }
    60
    61          if(errors.hasErrors()) {
    62              return showForm(request, response, errors);
    63          }
    64
    65          Map model = new HashMap();
    66          model.put("webXml", prettyPrint(newWebXml));
    67          model.put("acegiBeansXml", prettyPrint(acegiBeans));
    68          model.put("nBeans", new Integer(nBeans));
    69
    70          return new ModelAndView("acegificationResults", model);
    71      }
    72
    73      /*** Creates a formatted XML string from the supplied document */
    74      private String prettyPrint(Document document) throws IOException {
    75          ByteArrayOutputStream output = new ByteArrayOutputStream();
    76          OutputFormat format = OutputFormat.createPrettyPrint();
    77          format.setTrimText(false);
    78          XMLWriter writer = new XMLWriter(output, format);
    79          writer.write(document);
    80          writer.flush();
    81          writer.close();
    82          return output.toString();
    83      }
    84
    85      /***
    86       * Validates the acegi beans, based on the input form data, and returns the number
    87       * of spring beans defined in the document.
    88       */
    89      private int validateAcegiBeans(AcegifierForm conversion, Document beans, Errors errors) {
    90          DefaultListableBeanFactory bf = createBeanFactory(beans);
    91
    92          //TODO: actually do some proper validation!
    93
    94          try {
    95              bf.getBean("filterChainProxy", FilterChainProxy.class);
    96          } catch (BeansException be) {
    97              errors.rejectValue("webXml","beansInvalid","There was an error creating or accessing the bean factory " + be.getMessage());
    98          }
    99          return bf.getBeanDefinitionCount();
    100     }
    101
    102     /*** Creates a BeanFactory from the spring beans XML document */
    103     private DefaultListableBeanFactory createBeanFactory(Document beans) {
    104         DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    105         XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf);
    106         beanReader.loadBeanDefinitions(new InMemoryResource(beans.asXML().getBytes()));
    107
    108         return bf;
    109     }
    110
    111 }