/** * Returns the base url for a given request. * * @param request the original http request. * @param baseUrl the base URL to return if it fails to build the base URL from the request. * @return the base url of this request. */ public static URI getBaseURLFromRequest(HttpServletRequest request, final URI baseUrl) { try { final StringBuilder urlBuilder = new StringBuilder(); final String scheme = request.getScheme(); urlBuilder.append(scheme); urlBuilder.append("://"); urlBuilder.append(request.getServerName()); final int port = request.getServerPort(); if (!isStandardPort(scheme, port)) { urlBuilder.append(":"); urlBuilder.append(port); } urlBuilder.append(request.getContextPath()); return new URI(urlBuilder.toString()); } catch (Exception ex) { return baseUrl; } }
/** * Returns the base url for a given request. * * @param request the original http request. * @param baseUrl the base URL to return if it fails to build the base URL from the request. * @return the base url of this request. */ public static URI getBaseURLFromRequest(HttpServletRequest request, final URI baseUrl) { try { final StringBuilder urlBuilder = new StringBuilder(); final String scheme = request.getScheme(); urlBuilder.append(scheme); urlBuilder.append("://"); urlBuilder.append(request.getServerName()); final int port = request.getServerPort(); if (!isStandardPort(scheme, port)) { urlBuilder.append(":"); urlBuilder.append(port); } urlBuilder.append(request.getContextPath()); return new URI(urlBuilder.toString()); } catch (Exception ex) { return baseUrl; } }