From: Roland Häder Date: Wed, 18 May 2016 12:15:58 +0000 (+0200) Subject: added FacesUtils which will contain methods for dealing with JSF, such as generating... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d90e8575f19be9141df3eddac34b3fd7c4d22e03;p=jcoreee.git added FacesUtils which will contain methods for dealing with JSF, such as generating the "base URL" for mail templates and other various places. You cannot use this from your EJBs as they don't have a faces context ... ;-) --- diff --git a/src/org/mxchange/jcoreee/utils/FacesUtils.java b/src/org/mxchange/jcoreee/utils/FacesUtils.java new file mode 100644 index 0000000..cb3aede --- /dev/null +++ b/src/org/mxchange/jcoreee/utils/FacesUtils.java @@ -0,0 +1,74 @@ +/* + * 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 . + */ +package org.mxchange.jcoreee.utils; + +import javax.faces.context.ExternalContext; +import javax.faces.context.FacesContext; + +/** + * An utilities class for JSF + *

+ * @author Roland Haeder + */ +public class FacesUtils { + + /** + * Generates a "base URL" for for example mail templates. For JSF + * pages/templates this is not needed. + *

+ * @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 + } + } + +}