package com.atlassian.sal.fisheye.auth;

import java.net.URI;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

import com.atlassian.sal.api.auth.LoginUriProvider;
import com.atlassian.sal.api.ApplicationProperties;

public class FisheyeLoginUriProvider implements LoginUriProvider
{
    private final ApplicationProperties applicationProperties;

    public FisheyeLoginUriProvider(ApplicationProperties applicationProperties)
    {
        this.applicationProperties = applicationProperties;
    }

    public URI getLoginUri(final URI returnUri)
    {
        try
        {
            return URI.create(applicationProperties.getBaseUrl() + "/login?origUrl=" + URLEncoder.encode(returnUri.toString(), "UTF-8"));
        }
        catch (UnsupportedEncodingException e)
        {
            throw new RuntimeException("Your JVM is broken", e);
        }
    }

}
@@ -646,16 +646,38 @@
 .create(
+%0D%0A                    
 applicat
@@ -708,28 +708,161 @@
 () +
- %22/login?origUrl=%22 +
+%0D%0A                    %22/login?origUrl=%22 +%0D%0A                    (!returnUri.isAbsolute() ? applicationProperties.getBaseUrl() : %22%22) +%0D%0A                   
  URL
@@ -897,16 +897,37 @@
 tring(),
+%0D%0A                   
  %22UTF-8%22

prepend base URL to non-absolute origUrls