X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjcoreee%2Futils%2FFacesUtils.java;h=674b8f1bd1752619d63288876be96247ccbf6e7a;hb=1f141c043a053621b86a5701420ca71f17a64e7b;hp=cb3aede8f505423c353a87439731cdc83bca840d;hpb=d90e8575f19be9141df3eddac34b3fd7c4d22e03;p=jcore-utils.git diff --git a/src/org/mxchange/jcoreee/utils/FacesUtils.java b/src/org/mxchange/jcoreee/utils/FacesUtils.java index cb3aede..674b8f1 100644 --- a/src/org/mxchange/jcoreee/utils/FacesUtils.java +++ b/src/org/mxchange/jcoreee/utils/FacesUtils.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 Cho-Time GmbH + * Copyright (C) 2016 - 2018 Free Software Foundation * * 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 @@ -22,7 +22,7 @@ import javax.faces.context.FacesContext; /** * An utilities class for JSF *

- * @author Roland Haeder + * @author Roland Häder */ public class FacesUtils { @@ -38,28 +38,106 @@ public class FacesUtils { // Get request scheme and such ... String scheme = context.getRequestScheme(); - String hostName = context.getRequestServerName(); - String path = context.getRequestServletPath(); + String serverName = context.getRequestServerName(); + String contextPath = context.getRequestContextPath(); + String servletPath = context.getRequestServletPath(); + int port = context.getRequestServerPort(); // Is the path null? if (null == scheme) { // Throw NPE throw new NullPointerException("context.requestScheme is null"); //NOI18N - } else if (null == hostName) { + } else if (null == serverName) { // And throw again ... throw new NullPointerException("context.requestServerName is null"); //NOI18N - } else if (null == path) { + } + + if (null == contextPath) { // Set to empty string - path = ""; //NOI18N + contextPath = ""; //NOI18N } - // Construct full URL - String baseUrl = String.format("%s://%s%s", scheme, hostName, path); //NOI18N + if (null == servletPath) { + // Set to empty string + servletPath = ""; //NOI18N + } + + // Init variable + String baseUrl; + + // Unusual port found? + if ((port != 80) && (port != 443)) { + // Construct full URL + baseUrl = String.format("%s://%s:%d%s%s", scheme, serverName, port, contextPath, servletPath); //NOI18N + } else { + // Construct full URL + baseUrl = String.format("%s://%s%s%s", scheme, serverName, contextPath, servletPath); //NOI18N + } // Return it return baseUrl; } + /** + * Compares both string with null-safety. This method is based on the + * example from + * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator + *

+ * @param str0 First string + * @param str1 Second string + *

+ * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and + * 2 means str0 bigger str2 + */ + @SuppressWarnings ("null") + public static int comareTo (final String str0, final String str1) { + // Check both strings for null and empty + boolean isStr0Empty = (str0 == null || str0.isEmpty()); + boolean isStr1Empty = (str1 == null || str1.isEmpty()); + + // Check conditions + if (isStr0Empty && isStr1Empty) { + return 0; + } else if (isStr0Empty) { + return -1; + } else if (isStr1Empty) { + return 1; + } + + // Compare both + return str0.compareTo(str1); + } + + /** + * Compares both string with null-safety, ignoring case-sensitivity. This + * method is based on the example from + * https://codereview.stackexchange.com/questions/20191/comparing-two-strings-which-could-be-null-or-blank-in-a-comparator + *

+ * @param str0 First string + * @param str1 Second string + *

+ * @return Comparison value, 0 means equals, -1 means str0 smaller str2 and + * 2 means str0 bigger str2 + */ + @SuppressWarnings ("null") + public static int comareToIgnoreCase (final String str0, final String str1) { + // Check both strings for null and empty + boolean isStr0Empty = (str0 == null || str0.isEmpty()); + boolean isStr1Empty = (str1 == null || str1.isEmpty()); + + // Check conditions + if (isStr0Empty && isStr1Empty) { + return 0; + } else if (isStr0Empty) { + return -1; + } else if (isStr1Empty) { + return 1; + } + + // Compare both + return str0.compareToIgnoreCase(str1); + } + /** * No instances from this class are required */