]> git.mxchange.org Git - jcore-utils.git/commitdiff
added FacesUtils which will contain methods for dealing with JSF, such as generating...
authorRoland Häder <roland@mxchange.org>
Wed, 18 May 2016 12:15:58 +0000 (14:15 +0200)
committerRoland Häder <roland@mxchange.org>
Wed, 18 May 2016 15:49:45 +0000 (17:49 +0200)
You cannot use this from your EJBs as they don't have a faces context ... ;-)

src/org/mxchange/jcoreee/utils/FacesUtils.java [new file with mode: 0644]

diff --git a/src/org/mxchange/jcoreee/utils/FacesUtils.java b/src/org/mxchange/jcoreee/utils/FacesUtils.java
new file mode 100644 (file)
index 0000000..cb3aede
--- /dev/null
@@ -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 <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
+               }
+       }
+
+}