View Javadoc
1   /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.init;
35  
36  import java.net.InetAddress;
37  import java.net.UnknownHostException;
38  
39  import javax.inject.Inject;
40  import javax.inject.Singleton;
41  import javax.servlet.ServletContext;
42  
43  import org.apache.commons.lang3.BooleanUtils;
44  import org.apache.commons.lang3.StringUtils;
45  
46  /**
47   * A simple wrapper about the few initial variables used to resolve {@link PropertySource}.
48   */
49  @Singleton
50  public class DefaultMagnoliaInitPaths implements MagnoliaInitPaths {
51      private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DefaultMagnoliaInitPaths.class);
52  
53      /**
54       * Context parameter name. If set to true in web.xml the server name resolved by magnolia will never contain the
55       * domain (the server "server.domain.com" will be simply resolved as "server").
56       */
57      protected static final String MAGNOLIA_UNQUALIFIED_SERVER_NAME = "magnolia.unqualified.server.name";
58  
59      private final String serverName;
60      private final String rootPath;
61      private final String webappFolderName;
62      private final String contextPath;
63      private final String serverInfo;
64  
65      @Inject
66      public DefaultMagnoliaInitPaths(final ServletContext servletContext) {
67          this.serverName = determineServerName(servletContext);
68          this.rootPath = determineRootPath(servletContext);
69          this.webappFolderName = determineWebappFolderName(rootPath, servletContext);
70          this.contextPath = determineContextPath(servletContext);
71          this.serverInfo = servletContext.getServerInfo();
72          log.info("servername is {}, rootPath is {}, webapp is {}, contextPath is {}", serverName, rootPath, webappFolderName, contextPath);
73      }
74  
75      /**
76       * @deprecated since 5.6, was only introduced in 4.5 for retro-compatibility with potential subclasses of former MgnlServletContextListener.
77       */
78      @Deprecated
79      public DefaultMagnoliaInitPaths(MagnoliaServletContextListener magnoliaServletContextListener, final ServletContext servletContext) {
80          this(servletContext);
81      }
82  
83      /**
84       * Figures out the local host name, makes sure it's lowercase, and use its unqualified name if the {@value #MAGNOLIA_UNQUALIFIED_SERVER_NAME} init parameter is set to true.
85       */
86      protected String determineServerName(ServletContext context) {
87          final boolean unqualifiedServerName = BooleanUtils.toBoolean(context.getInitParameter(MAGNOLIA_UNQUALIFIED_SERVER_NAME));
88  
89          try {
90              String serverName = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName());
91  
92              if (unqualifiedServerName && StringUtils.contains(serverName, ".")) {
93                  serverName = StringUtils.substringBefore(serverName, ".");
94              }
95              return serverName;
96          } catch (UnknownHostException e) {
97              log.warn("Failed to obtain server name, please check your configuration. Using 'default' as server name instead.");
98              return "default";
99          }
100     }
101 
102 
103     /**
104      * Figures out the root path where the webapp is deployed.
105      */
106     protected String determineRootPath(ServletContext context) {
107         String realPathFromContext = context.getRealPath(StringUtils.EMPTY);
108         if (realPathFromContext == null) {
109             // some App Servers like IBM Liberty always return null on getRealPath("") but support getRealPath("/") - unfortunately the spec isn't precise on what has to be supported how.
110             realPathFromContext = context.getRealPath("/");
111         }
112 
113         String realPath = StringUtils.replace(realPathFromContext, "\\", "/");
114         realPath = StringUtils.removeEnd(realPath, "/");
115         if (realPath == null) {
116             // don't use new java.io.File("x").getParentFile().getAbsolutePath() to find out real directory, could throw
117             // a NPE for unexpanded war
118             throw new RuntimeException("Magnolia is not configured properly and therefore unable to start: real path can't be obtained [ctx real path:" + context.getRealPath(StringUtils.EMPTY) + "]. Please refer to the Magnolia documentation for installation instructions specific to your environment.");
119         }
120         return realPath;
121     }
122 
123     protected String determineWebappFolderName(String determinedRootPath, ServletContext context) {
124         return StringUtils.substringAfterLast(determinedRootPath, "/");
125     }
126 
127     protected String determineContextPath(ServletContext context) {
128         return context.getContextPath();
129     }
130 
131     /**
132      * The server name  is resolved to the full name obtained by using InetAddress.getLocalHost().getHostName(), which
133      * may also contain the server domain, depending on your server configuration/operating system. You can set the
134      * optional context parameter "magnolia.unqualified.server.name" to true if you prefer using the unqualified name
135      * (the server "server.domain.com" will be simply resolved as "server").
136      *
137      * <pre>
138      * &lt;context-param>
139      *   &lt;param-name>magnolia.unqualified.server.name&lt;/param-name>
140      *   &lt;param-value>true&lt;/param-value>
141      * &lt;/context-param>
142      * </pre>
143      */
144     @Override
145     public String getServerName() {
146         return serverName;
147     }
148 
149     @Override
150     public String getRootPath() {
151         return rootPath;
152     }
153 
154     @Override
155     public String getWebappFolderName() {
156         return webappFolderName;
157     }
158 
159     @Override
160     public String getContextPath() {
161         return contextPath;
162     }
163 
164     @Override
165     public String getServerInfo() {
166         return serverInfo;
167     }
168 }