2 * Copyright (C) 2016, 2017 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jcoreee.utils;
19 import javax.faces.context.ExternalContext;
20 import javax.faces.context.FacesContext;
23 * An utilities class for JSF
25 * @author Roland Häder<roland@mxchange.org>
27 public class FacesUtils {
30 * Generates a "base URL" for for example mail templates. For JSF
31 * pages/templates this is not needed.
35 public static String generateBaseUrl () {
36 // Get external context
37 ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
39 // Get request scheme and such ...
40 String scheme = context.getRequestScheme();
41 String serverName = context.getRequestServerName();
42 String contextPath = context.getRequestContextPath();
43 String servletPath = context.getRequestServletPath();
44 int port = context.getRequestServerPort();
49 throw new NullPointerException("context.requestScheme is null"); //NOI18N
50 } else if (null == serverName) {
51 // And throw again ...
52 throw new NullPointerException("context.requestServerName is null"); //NOI18N
55 if (null == contextPath) {
56 // Set to empty string
57 contextPath = ""; //NOI18N
60 if (null == servletPath) {
61 // Set to empty string
62 servletPath = ""; //NOI18N
68 // Unusual port found?
69 if ((port != 80) && (port != 443)) {
71 baseUrl = String.format("%s://%s:%d%s%s", scheme, serverName, port, contextPath, servletPath); //NOI18N
74 baseUrl = String.format("%s://%s%s%s", scheme, serverName, contextPath, servletPath); //NOI18N
82 * No instances from this class are required
84 private FacesUtils () {
86 if (FacesContext.getCurrentInstance() == null) {
87 // Okay, don't allow any usage
88 throw new NullPointerException("Cannot access FacesContext, maybe you have tried to use this class from an EJB?"); //NOI18N