package org.xcolab.entity.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import org.xcolab.client.admin.attributes.platform.PlatformAttributeKey;

import javax.servlet.http.HttpServletRequest;

public final class LinkUtils {

    public static final String COLAB_URL = PlatformAttributeKey.COLAB_URL.get();

    private LinkUtils() {
    }

    public static String getBaseUri(HttpServletRequest request) {
        String baseUri = String.format("%s://%s", request.getScheme(), request.getServerName());
        int port = request.getServerPort();
        if (port != 80 && port != 443) {
            baseUri += ":" + port;
        }
        return baseUri;
    }

    public static String getNonBlankStringOrDefault(String string, String defaultString) {
        if (StringUtils.isNotBlank(string)) {
            return string;
        }
        return defaultString;
    }

    public static String getAbsoluteUrl(String relativeUri) {
        return COLAB_URL + relativeUri;
    }

    /**
     * Takes an absolute URI and removes the scheme, host, and port (if present).
     *
     * @param uri some uri - absolute or relative
     * @return a relative uri
     */
    public static String getRelativeUri(String uri) {
        if (uri == null) {
            return null;
        }
        final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(uri);
        final UriComponents uriComponents = uriBuilder.build();
        StringBuilder relativeUri = new StringBuilder();
        relativeUri.append(uriComponents.getPath() != null ? uriComponents.getPath() : "/");
        if (uriComponents.getQuery() != null) {
            relativeUri.append("?").append(uriComponents.getQuery());
        }
        return relativeUri.toString();
    }

    public static String getLocalUrl(String url) {
        return getLocalUrl(url, "/");
    }

    public static String getLocalUrl(String url, String defaultRedirect) {
        if (StringUtils.isNotBlank(url) && isLocalUrl(url)) {
            return url;
        }
        return defaultRedirect;
    }

    public static boolean isLocalUrl(String url) {
        return isRelativeUrl(url) || url.startsWith(PlatformAttributeKey.COLAB_URL.get());
    }

    public static boolean isRelativeUrl(String url) {
        return url.startsWith("/");
    }

    public static boolean isLoginPageLink(String url) {
        final UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(url);
        return uriBuilder.build().getPath().startsWith("/login");
    }
}

@@ -76,59 +76,8 @@
 ls;%0A
-import org.springframework.web.util.UriComponents;%0A
 impo
@@ -1433,24 +1433,25 @@
 g(uri);%0A
+%0A
         
 final Ur
@@ -1446,43 +1446,47 @@
     
-final UriComponents uriComponents =
+// Clear scheme, host, and port%0A       
  uri
@@ -1493,22 +1493,27 @@
 Builder.
-build(
+scheme(null
 );%0A     
@@ -1519,158 +1519,69 @@
     
-String
+uri
 Builder
- relativeUri = new StringBuilder();%0A        relativeUri.append(uriComponents.getPath() != null ? uriComponents.getPath() : %22/%22
+.host(null);%0A        uriBuilder.port(-1
 );%0A
+%0A
         
 if (
@@ -1580,165 +1580,113 @@
     
-if (uriComponents.getQuery() != null) %7B%0A            relativeUri.append(%22?%22).append(uriComponents.getQuery());%0A        %7D%0A        return relativeUri.toString()
+final String newUri = uriBuilder.toUriString();%0A        return StringUtils.isEmpty(newUri) ? %22/%22 : newUri
 ;%0A  

Improve getRelativeUri method