/*
 * Copyright (c) Bounce Storage, Inc. All rights reserved.
 * For more information, please see COPYRIGHT in the top-level directory.
 */

package com.bouncestorage.swiftproxy.v1;

import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import com.bouncestorage.swiftproxy.BlobStoreLocator;
import com.bouncestorage.swiftproxy.BlobStoreResource;
import com.bouncestorage.swiftproxy.BounceResourceConfig;

@Path("/auth/v1.0")
public final class AuthResource extends BlobStoreResource {
    @GET
    public Response auth(@HeaderParam("X-Auth-User") String user,
                         @HeaderParam("X-Auth-Key") String key,
                         @HeaderParam("X-Storage-User") String storageUser,
                         @HeaderParam("X-Storage-Pass") String storagePass) {
        if (user == null && storageUser != null) {
            user = storageUser;
        }
        String endpoint = ((BounceResourceConfig) application).getEndPoint().toString();
        endpoint += "/v1/" + user;
        String authToken = user + BlobStoreLocator.TOKEN_SEPARATOR + "foobar";
        return Response.ok()
                .header("x-storage-url", endpoint)
                .header("x-auth-token", authToken)
                .build();
    }
}

@@ -257,16 +257,49 @@
 s.Path;%0A
+import javax.ws.rs.core.Context;%0A
 import j
@@ -445,57 +445,77 @@
 ort 
-com.bouncestorage.swiftproxy.BounceResourceConfig
+org.glassfish.grizzly.http.server.Request;%0A%0Aimport java.util.Optional
 ;%0A%0A@
@@ -882,16 +882,136 @@
 ragePass
+,%0A                         @HeaderParam(%22Host%22) Optional%3CString%3E host,%0A                         @Context Request request
 ) %7B%0A    
@@ -1099,16 +1099,17 @@
       %7D%0A
+%0A
         
@@ -1119,101 +1119,193 @@
 ing 
-endpoint = ((BounceResourceConfig) application).getEndPoint().toString();%0A        endpoint
+storageURL = host.orElseGet(() -%3E request.getLocalAddr() + %22:%22 + request.getLocalPort());%0A        String scheme = request.getScheme();%0A        storageURL = scheme + %22://%22 + storageURL
  +
-=
  %22/v
@@ -1312,24 +1312,25 @@
 1/%22 + user;%0A
+%0A
         Stri
@@ -1470,16 +1470,18 @@
 l%22, 
-endpoint
+storageURL
 )%0A  

use storage URL from request