code: public String constructServiceUrl(final HttpServletRequest request, final HttpServletResponse response,
        final String service, final String serverName) {
      if (Strings.isNotBlank(service)) { return response.encodeURL(service); }
  
      final StringBuilder buffer = new StringBuilder();
      if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) {
        buffer.append(request.isSecure() ? "https://" : "http://");
      }
  
      buffer.append(serverName);
      buffer.append(request.getRequestURI());
  
      Set<String> reservedKeys = CollectUtils.newHashSet();
      reservedKeys.add(config.getArtifactName());
      if (null != sessionIdReader) {
        reservedKeys.add(sessionIdReader.idName());
      }
      String queryString = request.getQueryString();
      if (Strings.isNotBlank(queryString)) {
        String[] parts = Strings.split(queryString, "&");
        Arrays.sort(parts);
        StringBuilder paramBuf = new StringBuilder();
        for (String part : parts) {
          int equIdx = part.indexOf('=');
          if (equIdx > 0) {
            String key = part.substring(0, equIdx);
            if (!reservedKeys.contains(key)) {
              paramBuf.append('&').append(key).append(part.substring(equIdx));
            }
          }
        }
        if (paramBuf.length() > 0) {
          paramBuf.setCharAt(0, '?');
          buffer.append(paramBuf);
        }
      }
  
      return response.encodeURL(buffer.toString());
    }
docs: Constructs a service url from the HttpServletRequest or from the given
  serviceUrl. Prefers the serviceUrl provided if both a serviceUrl and a
  serviceName.
  
  @param request the HttpServletRequest
  @param response the HttpServletResponse
  @param service the configured service url (this will be used if not null)
  @param serverName the server name to use to constuct the service url if service param is empty
  @param artifactParameterName the artifact parameter name to remove (i.e. ticket)
  @param encode whether to encode the url or not (i.e. Jsession).
  @return the service url to use.