]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/utils/FacesUtils.java
674b8f1bd1752619d63288876be96247ccbf6e7a
[jcore-utils.git] / src / org / mxchange / jcoreee / utils / FacesUtils.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcoreee.utils;
18
19 import javax.faces.context.ExternalContext;
20 import javax.faces.context.FacesContext;
21
22 /**
23  * An utilities class for JSF
24  * <p>
25  * @author Roland Häder<roland@mxchange.org>
26  */
27 public class FacesUtils {
28
29         /**
30          * Generates a "base URL" for for example mail templates. For JSF
31          * pages/templates this is not needed.
32          * <p>
33          * @return Base URL
34          */
35         public static String generateBaseUrl () {
36                 // Get external context
37                 ExternalContext context = FacesContext.getCurrentInstance().getExternalContext();
38
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();
45
46                 // Is the path null?
47                 if (null == scheme) {
48                         // Throw NPE
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
53                 }
54
55                 if (null == contextPath) {
56                         // Set to empty string
57                         contextPath = ""; //NOI18N
58                 }
59
60                 if (null == servletPath) {
61                         // Set to empty string
62                         servletPath = ""; //NOI18N
63                 }
64
65                 // Init variable
66                 String baseUrl;
67
68                 // Unusual port found?
69                 if ((port != 80) && (port != 443)) {
70                         // Construct full URL
71                         baseUrl = String.format("%s://%s:%d%s%s", scheme, serverName, port, contextPath, servletPath); //NOI18N
72                 } else {
73                         // Construct full URL
74                         baseUrl = String.format("%s://%s%s%s", scheme, serverName, contextPath, servletPath); //NOI18N
75                 }
76
77                 // Return it
78                 return baseUrl;
79         }
80
81         /**
82          * Compares both string with null-safety. This method is based on the
83          * example from
84          * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator
85          * <p>
86          * @param str0 First string
87          * @param str1 Second string
88          * <p>
89          * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and
90          *         2 means str0 bigger str2
91          */
92         @SuppressWarnings ("null")
93         public static int comareTo (final String str0, final String str1) {
94                 // Check both strings for null and empty
95                 boolean isStr0Empty = (str0 == null || str0.isEmpty());
96                 boolean isStr1Empty = (str1 == null || str1.isEmpty());
97
98                 // Check conditions
99                 if (isStr0Empty && isStr1Empty) {
100                         return 0;
101                 } else if (isStr0Empty) {
102                         return -1;
103                 } else if (isStr1Empty) {
104                         return 1;
105                 }
106
107                 // Compare both
108                 return str0.compareTo(str1);
109         }
110
111         /**
112          * Compares both string with null-safety, ignoring case-sensitivity. This
113          * method is based on the example from
114          * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator
115          * <p>
116          * @param str0 First string
117          * @param str1 Second string
118          * <p>
119          * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and
120          *         2 means str0 bigger str2
121          */
122         @SuppressWarnings ("null")
123         public static int comareToIgnoreCase (final String str0, final String str1) {
124                 // Check both strings for null and empty
125                 boolean isStr0Empty = (str0 == null || str0.isEmpty());
126                 boolean isStr1Empty = (str1 == null || str1.isEmpty());
127
128                 // Check conditions
129                 if (isStr0Empty && isStr1Empty) {
130                         return 0;
131                 } else if (isStr0Empty) {
132                         return -1;
133                 } else if (isStr1Empty) {
134                         return 1;
135                 }
136
137                 // Compare both
138                 return str0.compareToIgnoreCase(str1);
139         }
140
141         /**
142          * No instances from this class are required
143          */
144         private FacesUtils () {
145                 // Is this null?
146                 if (FacesContext.getCurrentInstance() == null) {
147                         // Okay, don't allow any usage
148                         throw new NullPointerException("Cannot access FacesContext, maybe you have tried to use this class from an EJB?"); //NOI18N
149                 }
150         }
151
152 }