001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 Echo Three, LLC 003// 004// Licensed under the Apache License, Version 2.0 (the "License"); 005// you may not use this file except in compliance with the License. 006// You may obtain a copy of the License at 007// 008// http://www.apache.org/licenses/LICENSE-2.0 009// 010// Unless required by applicable law or agreed to in writing, software 011// distributed under the License is distributed on an "AS IS" BASIS, 012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013// See the License for the specific language governing permissions and 014// limitations under the License. 015// -------------------------------------------------------------------------------- 016 017package com.echothree.util.common.string; 018 019import java.net.URLEncoder; 020import java.nio.charset.StandardCharsets; 021import java.util.Map; 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025public class UrlUtils { 026 027 private UrlUtils() { 028 super(); 029 } 030 031 private static class UrlUtilsHolder { 032 static UrlUtils instance = new UrlUtils(); 033 } 034 035 public static UrlUtils getInstance() { 036 return UrlUtilsHolder.instance; 037 } 038 039 public StringBuilder buildBaseUrl(HttpServletRequest request) { 040 var scheme = request.getScheme(); 041 var serverName = request.getServerName(); 042 var serverPort = request.getServerPort(); 043 var contextPath = request.getContextPath(); 044 var servletPath = request.getServletPath(); 045 var includeServerPort = (scheme.equals("http") && serverPort != 80) || (scheme.equals("https") && serverPort != 443); 046 047 return new StringBuilder(scheme).append("://").append(serverName).append(includeServerPort? ":" + serverPort: "").append(contextPath).append(servletPath); 048 } 049 050 public StringBuilder buildParams(StringBuilder url, Map<String, String> params) { 051 if(params != null && params.size() > 0) { 052 var entrySet = params.entrySet(); 053 var isFirst = true; 054 055 for(var entry : entrySet) { 056 url.append(isFirst ? '?' : '&'); 057 url.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)); 058 isFirst = false; 059 } 060 } 061 062 return url; 063 } 064 065 public String buildUrl(HttpServletRequest request, HttpServletResponse response, String path, Map<String, String> params) { 066 return response.encodeURL(buildParams(buildBaseUrl(request).append(path), params).toString()); 067 } 068 069}