/*
(non-Javadoc)
@see javax.servlet.sip.ConvergedHttpSession#encodeURL(java.lang.String, java.lang.String)
167882
public String encodeURL(String relativePath, String scheme) {
		StringBuffer urlEncoded = new StringBuffer();
		//Context
		Context context = (Context)sipManager.getContainer();
		//Host
		Host host = (Host)context.getParent();
		//Service
		Service service = ((Engine)host.getParent()).getService();
		String[] aliases = host.findAliases();
		String hostname = null;
		
		// Needed for http://code.google.com/p/sipservlets/issues/detail?id=150
		// to pass TCK test com.bea.sipservlet.tck.agents.api.javax_servlet_sip.ConvergedHttpSessionTest.testEncodeURL002
		if(aliases.length < 1) {
			hostname = host.getName();
		} else {
			// FIXME would be better to try to match the alias by some path or IP but unaware if this is really needed at this point
			// or how it can be effectively done
			hostname = aliases[0];
		}
		
		//retrieving the port corresponding to the specified scheme
		//TODO ask EG what if the scheme is not supported on the server ?
		int port = -1;		
		Connector[] connectors = service.findConnectors();
		int i = 0;
		while (i < connectors.length && port < 0) {
			if(scheme != null && connectors[i].getProtocol().toLowerCase().contains(scheme.toLowerCase())) {
				port = connectors[i].getPort();
			}
			i++;
		}
		urlEncoded = urlEncoded.append(scheme);
		urlEncoded = urlEncoded.append("://");
		urlEncoded = urlEncoded.append(hostname);
		urlEncoded = urlEncoded.append(":");
		urlEncoded = urlEncoded.append(port);
		urlEncoded = urlEncoded.append(((Context)sipManager.getContainer()).getPath());
		urlEncoded = urlEncoded.append(encodeURL(relativePath));
		return urlEncoded.toString();
	}
1367562
public static String urlEncode(String s) {
		if (s == null) {
			return "";
		}
		String encoded = s;
		try {
			encoded = URLEncoder.encode(s, Config.DEFAULT_ENCODING);
		} catch (UnsupportedEncodingException ex) {
			logger.error(null, ex);
		}
		return encoded;
	}
88129
public static String urlEncode(String src) throws UnsupportedEncodingException {
		return URLEncoder.encode(src, Charsets.UTF_8.name()).replace("+", "%20");
	}
846486
public String encodeRedirectURL(String url) {
        if (isEncodeable(toAbsolute(url))) {
            return originalServletContext.getSessionConfig().rewriteUrl(url, servletContext.getSession(originalServletContext, exchange, true).getId());
        } else {
            return url;
        }
    }
968554
public static String urlEncode(final String part) {
		try {
			return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
		} catch (UnsupportedEncodingException e) {
			throw new ImpossibleException(e);
		}
	}
215497
@Nullable
  public String encodeRedirectURL (@Nullable final String sUrl)
  {
    if (StringHelper.hasText (m_sEncodeRedirectUrlSuffix))
      return StringHelper.getNotNull (sUrl) + m_sEncodeRedirectUrlSuffix;
    return sUrl;
  }
151216
public static String encodeURL(String url, SessionInfo info) {
        // could be /path/page#fragment?query
        // could be /page/page;session=existing#fragment?query
        // where fragment and query are both optional

        HttpSession session = info.getSession();
        if (null == session) {
            return url;
        }
        final String id = session.getId();
        final String target = info.getSessionConfig().getURLRewritingMarker();
        URLParser parser = new URLParser(url, target);

        StringBuilder sb = new StringBuilder();
        if (-1 != parser.idMarker) {
            // a session exists in the URL, overlay this ID
            sb.append(url);
            int start = parser.idMarker + target.length();
            if (start + 23 < url.length()) {
                sb.replace(start, start + 23, id);
            } else {
                // invalid length on existing session, just remove that
                // TODO: what if a fragment or query string was after the
                // invalid session data
                sb.setLength(parser.idMarker);
                sb.append(target).append(id);
            }
        } else {
            // add session data to the URL
            if (-1 != parser.fragmentMarker) {
                // prepend it before the uri fragment
                sb.append(url, 0, parser.fragmentMarker);
                sb.append(target).append(id);
                sb.append(url, parser.fragmentMarker, url.length());
            } else if (-1 != parser.queryMarker) {
                // prepend it before the query data
                sb.append(url, 0, parser.queryMarker);
                sb.append(target).append(id);
                sb.append(url, parser.queryMarker, url.length());
            } else {
                // just a uri
                sb.append(url).append(target).append(id);
            }
        }
        return sb.toString();
    }
688690
public String encodeURL(String relativePath, String scheme) {
        StringBuffer urlEncoded = new StringBuffer();
        // Context
        SipContextImpl context = (SipContextImpl) sipManager.getContainer();
        // ListenersService
        List<ListenerService<?>> webServerListeners = context.getWebServerListeners();
        // Host
        String hostname = context.getDeploymentInfoFacade().getDeploymentInfo().getHostName();
        // kakonyii: get aliases from undrertow's host service!
        Set<String> aliases = context.getHostOfDeployment().getAllAliases();

        // Needed for http://code.google.com/p/sipservlets/issues/detail?id=150
        // to pass TCK test com.bea.sipservlet.tck.agents.api.javax_servlet_sip.ConvergedHttpSessionTest.testEncodeURL002
        if (aliases.size() >= 1) {
            // FIXME would be better to try to match the alias by some path or IP but unaware if this is really needed at this
            // point
            // or how it can be effectively done
            hostname = (String) aliases.toArray()[0];
        }

        // retrieving the port corresponding to the specified scheme
        // TODO ask EG what if the scheme is not supported on the server ?
        // kakonyii: find out the port from undertow's web server listeners
        int port = -1;
        int i = 0;
        while (i < webServerListeners.size() && port < 0) {
            ListenerService<?> listener = webServerListeners.get(i);
            if (scheme != null && ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))
                    && listener instanceof HttpListenerService) {
                if (listener.getBinding().getValue() != null) {
                    port = listener.getBinding().getValue().getPort();
                    if (listener.getBinding().getValue().getAddress() != null) {
                        hostname = listener.getBinding().getValue().getAddress().getHostName();
                    }
                }
            }
            i++;
        }
        urlEncoded = urlEncoded.append(scheme);
        urlEncoded = urlEncoded.append("://");
        urlEncoded = urlEncoded.append(hostname);
        urlEncoded = urlEncoded.append(":");
        urlEncoded = urlEncoded.append(port);
        // check that at SipContextImpl.getPath():
        urlEncoded = urlEncoded.append(context.getPath());
        urlEncoded = urlEncoded.append(encodeURL(relativePath));
        return urlEncoded.toString();
    }
133120
public static String encodeAll(String url, Charset charset) throws UtilException {
		try {
			return java.net.URLEncoder.encode(url, charset.toString());
		} catch (UnsupportedEncodingException e) {
			throw new UtilException(e);
		}
	}
262153
private void encodeThis() throws IOException {
        DerOutputStream out = new DerOutputStream();
        DerOutputStream tmp = new DerOutputStream();

        if (ca) {
            tmp.putBoolean(ca);
            // Only encode pathLen when ca == true
            if (pathLen >= 0) {
                tmp.putInteger(pathLen);
            }
        }
        out.write(DerValue.tag_Sequence, tmp);
        this.extensionValue = out.toByteArray();
    }
162933
public static String urlEncode(String str) {
        try {
            String encodeForForm = URLEncoder.encode(str, "UTF-8");
            String encodeForUrl = encodeForForm.replace("+", "%20");
            return encodeForUrl;
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException("JVM does not support UTF-8");
        }
    }
459489
@Trivial
    public static URL createWSJPAURL(URL url) throws MalformedURLException {
        if (url == null) {
            return null;
        }

        // Encode the URL to be embedded into the wsjpa URL's path
        final String encodedURLPathStr = encode(url.toExternalForm());

        URL returnURL;
        try {
            returnURL = AccessController.doPrivileged(new PrivilegedExceptionAction<URL>() {
                @Override
                @Trivial
                public URL run() throws MalformedURLException {
                    return new URL(WSJPA_PROTOCOL_NAME + ":" + encodedURLPathStr);
                }
            });
        } catch (PrivilegedActionException e) {
            throw (MalformedURLException) e.getException();
        }

        return returnURL;
    }
265964
public static Base64Url encode(byte[] bytes) {
        if (bytes == null) {
            return new Base64Url(null);
        } else {
            return new Base64Url(BaseEncoding.base64Url().omitPadding().encode(bytes));
        }
    }
1050470
protected String encode(String value)
    {
        try
        {
            return URLEncoder.encode(value,"UTF-8");
        }
        catch (UnsupportedEncodingException e)
        {
            throw new ClientException(500,"Error encoding parameter.",e);
        }
    }
818109
public static String encodeURL(String uri)
  {
    CharBuffer cb = CharBuffer.allocate();

    for (int i = 0; i < uri.length(); i++) {
      char ch = uri.charAt(i);

      switch (ch) {
      case '<':
      case '>':
      case ' ':
      case '%':
      case '\'':
      case '\"':
        cb.append('%');
        cb.append(encodeHex(ch >> 4));
        cb.append(encodeHex(ch));
        break;

      default:
        cb.append(ch);
      }
    }

    return cb.close();
  }
153468
static String urlencode(final String s) {
		try {
			return URLEncoder.encode(s, CHARSET_NAME);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
	}
1145281
public String encodeURL(String url) {
        StringBuffer urlEncoded = new StringBuffer();
        int indexOfQuestionMark = url.indexOf("?");
        if (indexOfQuestionMark > 0) {
            // Handles those cases :
            // http://forums.searchenginewatch.com/showthread.php?t=9817
            // http://forums.searchenginewatch.com/showthread.php?p=72232#post72232
            String urlBeforeQuestionMark = url.substring(0, indexOfQuestionMark);
            String urlAfterQuestionMark = url.substring(indexOfQuestionMark);
            urlEncoded = urlEncoded.append(urlBeforeQuestionMark);
            urlEncoded = urlEncoded.append(";jsessionid=");
            urlEncoded = urlEncoded.append(httpSession.getId());
            urlEncoded = urlEncoded.append(urlAfterQuestionMark);
        } else {
            // Handles those cases :
            // http://www.seroundtable.com/archives/003204.html#more
            // http://www.seroundtable.com/archives#more
            int indexOfPoundSign = url.indexOf("#");
            if (indexOfPoundSign > 0) {
                String urlBeforePoundSign = url.substring(0, indexOfPoundSign);
                String urlAfterPoundSign = url.substring(indexOfPoundSign);
                urlEncoded = urlEncoded.append(urlBeforePoundSign);
                urlEncoded = urlEncoded.append(";jsessionid=");
                urlEncoded = urlEncoded.append(httpSession.getId());
                urlEncoded = urlEncoded.append(urlAfterPoundSign);
            } else {
                // Handles the rest
                // http://www.seroundtable.com/archives/003204.html
                // http://www.seroundtable.com/archives
                urlEncoded = urlEncoded.append(url);
                urlEncoded = urlEncoded.append(";jsessionid=");
                urlEncoded = urlEncoded.append(httpSession.getId());
            }
        }

        return urlEncoded.toString();
    }