package org.swellrt.server.box.servlet;

import com.google.common.base.Preconditions;

import javax.servlet.http.HttpServletRequest;

public class ServiceUtils {


  public static String getSessionUrlRewrite(HttpServletRequest request) {
    Preconditions.checkNotNull(request, "Request can't be null");

    if (request.getPathInfo() == null || request.getPathInfo().isEmpty()) return "";

    // The ';sid=' syntax is jetty specific.
    int indexSid = request.getPathInfo().indexOf(";sid=");

    if (indexSid >= 0) {
      return request.getPathInfo().substring(indexSid, request.getPathInfo().length());
    }

    return "";
  }


  public static UrlBuilder getUrlBuilder(final HttpServletRequest request) {
    return getUrlBuilder(request, SwellRtServlet.SERVLET_CONTEXT);
  }

  public static UrlBuilder getUrlBuilder(final HttpServletRequest request, final String context) {

    return new UrlBuilder() {

      @Override
      public String build(String relativePath, String queryString) {
        Preconditions.checkNotNull(relativePath, "Path can't be null");

        String base =
            request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();

        String sessionRewrite = getSessionUrlRewrite(request);

        if (!relativePath.startsWith("/")) relativePath = "/" + relativePath;

        if (queryString == null) queryString = "";

        String absolute =
            base + context + relativePath + sessionRewrite + queryString;
        return absolute;
      }

    };
  }


  public static void completeRelativeUrls(StringBuilder sb, String urlFieldName,
      UrlBuilder urlBuilder) {

    int st = sb.indexOf("\"" + urlFieldName, 0);
    while (st >= 0) {

      int su = sb.indexOf("\"", st + urlFieldName.length() + 2);
      int eu = sb.indexOf("\"", su + 1);

      String u = sb.substring(su + 1, eu);

      String nu = urlBuilder.build(u, null);

      sb.replace(su, eu + 1, "\"" + nu + "\"");

      st = sb.indexOf(urlFieldName, su + nu.length() + 2);
    }

  }

}

@@ -1087,85 +1087,394 @@
 ing 
-base =%0A            request.getScheme() + %22://%22 + request.getServerName()
+scheme;%0A%0A        if (request.getHeader(%22X-Forwarded-Proto%22) != null) %7B%0A          scheme = request.getHeader(%22X-Forwarded-Proto%22);%0A        %7D else %7B%0A          scheme = request.getScheme();%0A        %7D%0A%0A        String port =%0A            request.getServerPort() == 80 %7C%7C request.getServerPort() == 443 ? %22%22 : %22:%22%0A                + request.getServerPort();%0A%0A        String base = scheme
  + %22:
+//
 %22 + 
@@ -1482,38 +1482,45 @@
 equest.getServer
-P
+Name() + p
 ort
-()
 ;%0A%0A        Strin

Fix #127 Handle request URL properly behind proxies