class UrlSessionTracking extends BaseSessionTracking implements SessionTracking {

  @Override
  public String encodeUrl(RequestWithSession request, String url) {
    RepositoryBackedSession session = request.getRepositoryBackedSession(false);
    if (session == null || !session.isValid()) {
      return url;
    }
    String encodedSessionAlias;
    try {
      encodedSessionAlias = URLEncoder.encode(session.getId(), "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException("This exception should never happen!", e);
    }

    int queryStart = url.indexOf('?');
    if (queryStart < 0) {
      return url + sessionIdPathItem + encodedSessionAlias;
    }
    String path = url.substring(0, queryStart);
    String query = url.substring(queryStart + 1, url.length());
    path += sessionIdPathItem + encodedSessionAlias;

    return path + '?' + query;
  }

}