--- /dev/null
+/*
+ * Copyright (C) 2016 Cho-Time GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.jcoreee.utils;
+
+import javax.faces.context.ExternalContext;
+import javax.faces.context.FacesContext;
+
+/**
+ * An utilities class for JSF
+ * <p>
+ * @author Roland Haeder<rhaeder@cho-time.de>
+ */
+public class FacesUtils {
+
+ /**
+ * Generates a "base URL" for for example mail templates. For JSF
+ * pages/templates this is not needed.
+ * <p>
+ * @return Base URL
+ */
+ public static String generateBaseUrl () {
+ // Get external context
+ ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
+
+ // Get request scheme and such ...
+ String scheme = context.getRequestScheme();
+ String hostName = context.getRequestServerName();
+ String path = context.getRequestServletPath();
+
+ // Is the path null?
+ if (null == scheme) {
+ // Throw NPE
+ throw new NullPointerException("context.requestScheme is null"); //NOI18N
+ } else if (null == hostName) {
+ // And throw again ...
+ throw new NullPointerException("context.requestServerName is null"); //NOI18N
+ } else if (null == path) {
+ // Set to empty string
+ path = ""; //NOI18N
+ }
+
+ // Construct full URL
+ String baseUrl = String.format("%s://%s%s", scheme, hostName, path); //NOI18N
+
+ // Return it
+ return baseUrl;
+ }
+
+ /**
+ * No instances from this class are required
+ */
+ private FacesUtils () {
+ // Is this null?
+ if (FacesContext.getCurrentInstance() == null) {
+ // Okay, don't allow any usage
+ throw new NullPointerException("Cannot access FacesContext, maybe you have tried to use this class from an EJB?"); //NOI18N
+ }
+ }
+
+}