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 java.net.InetAddress;
14  import java.net.UnknownHostException;
15  import java.util.List;
16  import java.util.Locale;
17  import java.util.Properties;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.springframework.beans.factory.annotation.Value;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.web.bind.annotation.RequestMapping;
25  import org.springframework.web.servlet.ModelAndView;
26  
27  import psiprobe.PostParameterizableViewController;
28  import psiprobe.Utils;
29  
30  /**
31   * The Class DecoratorController.
32   */
33  @Controller
34  public class DecoratorController extends PostParameterizableViewController {
35  
36    /** The messages basename. */
37    private String messagesBasename;
38  
39    /**
40     * Gets the messages basename.
41     *
42     * @return the messages basename
43     */
44    public String getMessagesBasename() {
45      return messagesBasename;
46    }
47  
48    /**
49     * Sets the messages basename.
50     *
51     * @param messagesBasename the new messages basename
52     */
53    @Value("/WEB-INF/messages")
54    public void setMessagesBasename(String messagesBasename) {
55      this.messagesBasename = messagesBasename;
56    }
57  
58    @RequestMapping(path = "/decorator.htm")
59    @Override
60    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
61        throws Exception {
62      return super.handleRequest(request, response);
63    }
64  
65    @Override
66    protected ModelAndView handleRequestInternal(HttpServletRequest request,
67        HttpServletResponse response) throws Exception {
68  
69      try {
70        request.setAttribute("hostname", InetAddress.getLocalHost().getHostName());
71      } catch (UnknownHostException e) {
72        request.setAttribute("hostname", "unknown");
73        logger.trace("", e);
74      }
75  
76      Properties version = (Properties) getApplicationContext().getBean("version");
77      request.setAttribute("version", version.getProperty("probe.version"));
78  
79      //
80      // Work out the language of the interface by matching resource files that we have
81      // to the request locale.
82      //
83      String lang = "en";
84      for (String fileName : getMessageFileNamesForLocale(request.getLocale())) {
85        if (getServletContext().getResource(fileName + ".properties") != null) {
86          lang = fileName.substring(messagesBasename.length() + 1);
87          break;
88        }
89      }
90  
91      request.setAttribute("lang", lang);
92  
93      return super.handleRequestInternal(request, response);
94    }
95  
96    /**
97     * Gets the message file names for locale.
98     *
99     * @param locale the locale
100    *
101    * @return the message file names for locale
102    */
103   private List<String> getMessageFileNamesForLocale(Locale locale) {
104     return Utils.getNamesForLocale(messagesBasename, locale);
105   }
106 
107   @Value("decorators/probe")
108   @Override
109   public void setViewName(String viewName) {
110     super.setViewName(viewName);
111   }
112 
113 }