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  
64      @Inject
65      public DefaultMagnoliaInitPaths(final ServletContext servletContext) {
66          this.serverName = determineServerName(servletContext);
67          this.rootPath = determineRootPath(servletContext);
68          this.webappFolderName = determineWebappFolderName(rootPath, servletContext);
69          this.contextPath = determineContextPath(servletContext);
70          log.info("servername is {}, rootPath is {}, webapp is {}, contextPath is {}", serverName, rootPath, webappFolderName, contextPath);
71      }
72  
73      /**
74       * @deprecated since 5.6, was only introduced in 4.5 for retro-compatibility with potential subclasses of former MgnlServletContextListener.
75       */
76      @Deprecated
77      public DefaultMagnoliaInitPaths(MagnoliaServletContextListener magnoliaServletContextListener, final ServletContext servletContext) {
78          this(servletContext);
79      }
80  
81      /**
82       * 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.
83       */
84      protected String determineServerName(ServletContext context) {
85          final boolean unqualifiedServerName = BooleanUtils.toBoolean(context.getInitParameter(MAGNOLIA_UNQUALIFIED_SERVER_NAME));
86  
87          try {
88              String serverName = StringUtils.lowerCase(InetAddress.getLocalHost().getHostName());
89  
90              if (unqualifiedServerName && StringUtils.contains(serverName, ".")) {
91                  serverName = StringUtils.substringBefore(serverName, ".");
92              }
93              return serverName;
94          } catch (UnknownHostException e) {
95              log.warn("Failed to obtain server name, please check your configuration. Using 'default' as server name instead.");
96              return "default";
97          }
98      }
99  
100 
101     /**
102      * Figures out the root path where the webapp is deployed.
103      */
104     protected String determineRootPath(ServletContext context) {
105         String realPathFromContext = context.getRealPath(StringUtils.EMPTY);
106         if (realPathFromContext == null) {
107             // 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.
108             realPathFromContext = context.getRealPath("/");
109         }
110 
111         String realPath = StringUtils.replace(realPathFromContext, "\\", "/");
112         realPath = StringUtils.removeEnd(realPath, "/");
113         if (realPath == null) {
114             // don't use new java.io.File("x").getParentFile().getAbsolutePath() to find out real directory, could throw
115             // a NPE for unexpanded war
116             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.");
117         }
118         return realPath;
119     }
120 
121     protected String determineWebappFolderName(String determinedRootPath, ServletContext context) {
122         return StringUtils.substringAfterLast(determinedRootPath, "/");
123     }
124 
125     protected String determineContextPath(ServletContext context) {
126         return context.getContextPath();
127     }
128 
129     /**
130      * The server name  is resolved to the full name obtained by using InetAddress.getLocalHost().getHostName(), which
131      * may also contain the server domain, depending on your server configuration/operating system. You can set the
132      * optional context parameter "magnolia.unqualified.server.name" to true if you prefer using the unqualified name
133      * (the server "server.domain.com" will be simply resolved as "server").
134      *
135      * <pre>
136      * &lt;context-param>
137      *   &lt;param-name>magnolia.unqualified.server.name&lt;/param-name>
138      *   &lt;param-value>true&lt;/param-value>
139      * &lt;/context-param>
140      * </pre>
141      */
142     @Override
143     public String getServerName() {
144         return serverName;
145     }
146 
147     @Override
148     public String getRootPath() {
149         return rootPath;
150     }
151 
152     @Override
153     public String getWebappFolderName() {
154         return webappFolderName;
155     }
156 
157     @Override
158     public String getContextPath() {
159         return contextPath;
160     }
161 
162 }