
package utils;

import play.Configuration;

import java.net.URI;
import java.net.URISyntaxException;

public class Config {

    public static String getHostport(String defaultValue) {
        play.Configuration config = play.Configuration.root();

        if (config == null) {
            return defaultValue;
        }

        String hostname = play.Configuration.root().getString("application.hostname");

        if (hostname != null && !hostname.isEmpty()) {
            Integer port = play.Configuration.root().getInt("application.port");

            if (port != null) {
               return hostname + ":" + port.toString();
            } else {
               return hostname;
            }
        } else {
           return defaultValue;
        }
    }

    public static String getHostport() {
        return getHostport("localhost");
    }

    public static String getScheme(String defaultValue) {
        play.Configuration config = play.Configuration.root();

        if (config == null) {
            return defaultValue;
        }

        String scheme = config.getString("application.scheme");

        if (scheme == null || scheme.isEmpty()) {
            return defaultValue;
        } else {
            return scheme;
        }
    }

    public static String getScheme() {
        return getScheme("http");
    }

    public static String getEmailFromSmtp() {
        Configuration config = Configuration.root();
        String user = config.getString("smtp.user");

        if (user == null) {
            return null;
        }

        if (user.contains("@")) {
            return user;
        } else {
            return user + "@" + config.getString("smtp.domain");
        }
    }

    /**
     * Make a full URI from the given {@code uri} and the configuration.
     *
     * The new url is created by copying the given url and fill scheme and
     * authority if they are empty.
     *
     * @param uri
     * @return
     * @throws URISyntaxException
     */
    public static URI createFullURI(URI uri) throws URISyntaxException {
        String scheme = uri.getScheme();
        String authority = uri.getAuthority();

        scheme = (scheme != null) ? scheme : getScheme();
        authority = (authority != null) ? authority : getHostport();

        return new URI(
                scheme, authority, uri.getPath(), uri.getQuery(), uri.getFragment());
    }

    /**
     * Make URI from the given {@code uri} and the configuration.
     *
     * @return uri
     * @throws URISyntaxException
     * @see {@link #createFullURI(java.net.URI)}
     */
    public static URI createFullURI(String uri) throws URISyntaxException {
        return createFullURI(new URI(uri));
    }
}

@@ -36,16 +36,38 @@
 uration;
+%0Aimport play.mvc.Http;
 %0A%0Aimport
@@ -822,24 +822,194 @@
 ostport() %7B%0A
+        Http.Context context = Http.Context.current.get();%0A%0A        if (context != null) %7B%0A            return getHostport(context.request().host());%0A        %7D else %7B%0A    
         retu
@@ -1033,24 +1033,34 @@
 ocalhost%22);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -1510,33 +1510,452 @@
     
-return getScheme(%22http%22);
+Http.Context context = Http.Context.current.get();%0A%0A        if (context != null) %7B%0A            try %7B%0A                return Config.getScheme(new URI(context.request().uri()).getScheme());%0A            %7D catch (URISyntaxException e) %7B%0A                play.Logger.warn(%22Failed to get the scheme part from the request-uri%22, e);%0A                return getScheme(%22http%22);%0A            %7D%0A        %7D else %7B%0A            return getScheme(%22http%22);%0A        %7D
 %0A   

fix incorrect uri to issue list.