View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.controllers;
12  
13  import com.thoughtworks.xstream.XStream;
14  
15  import jakarta.inject.Inject;
16  import jakarta.servlet.http.HttpServletRequest;
17  import jakarta.servlet.http.HttpServletResponse;
18  
19  import org.springframework.beans.factory.annotation.Value;
20  import org.springframework.web.bind.annotation.RequestMapping;
21  import org.springframework.web.servlet.ModelAndView;
22  import org.springframework.web.servlet.mvc.AbstractController;
23  import org.springframework.web.servlet.mvc.Controller;
24  
25  import psiprobe.model.TransportableModel;
26  
27  /**
28   * The Class BeanToXmlController.
29   */
30  @org.springframework.stereotype.Controller
31  public class BeanToXmlController extends AbstractController {
32  
33    /** The xml marker. */
34    private String xmlMarker;
35  
36    /** The xstream. */
37    @Inject
38    private XStream xstream;
39  
40    /**
41     * Gets the xml marker.
42     *
43     * @return the xml marker
44     */
45    public String getXmlMarker() {
46      return xmlMarker;
47    }
48  
49    /**
50     * Sets the xml marker.
51     *
52     * @param xmlMarker the new xml marker
53     */
54    @Value(".oxml")
55    public void setXmlMarker(String xmlMarker) {
56      this.xmlMarker = xmlMarker;
57    }
58  
59    @RequestMapping(path = "/*.oxml.htm")
60    @Override
61    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
62        throws Exception {
63      return super.handleRequest(request, response);
64    }
65  
66    @Override
67    protected ModelAndView handleRequestInternal(HttpServletRequest request,
68        HttpServletResponse response) throws Exception {
69  
70      String path = request.getServletPath();
71      String internalPath = path.replaceAll(xmlMarker, "");
72  
73      Controller controller = (Controller) getApplicationContext().getBean(internalPath);
74      if (controller != null) {
75        ModelAndView modelAndView = controller.handleRequest(request, response);
76        if (modelAndView.getModel() != null) {
77          TransportableModel tm = new TransportableModel();
78          tm.putAll(modelAndView.getModel());
79          xstream.toXML(tm, response.getWriter());
80        }
81      }
82      return null;
83    }
84  }