package com.emc.ecs.serviceBroker.service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.servicebroker.exception.ServiceBrokerException;
import org.springframework.cloud.servicebroker.exception.ServiceInstanceBindingExistsException;
import org.springframework.cloud.servicebroker.model.CreateServiceInstanceAppBindingResponse;
import org.springframework.cloud.servicebroker.model.CreateServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.model.CreateServiceInstanceBindingResponse;
import org.springframework.cloud.servicebroker.model.DeleteServiceInstanceBindingRequest;
import org.springframework.cloud.servicebroker.service.ServiceInstanceBindingService;
import org.springframework.stereotype.Service;

import com.emc.ecs.managementClient.model.UserSecretKey;
import com.emc.ecs.serviceBroker.EcsManagementClientException;
import com.emc.ecs.serviceBroker.EcsManagementResourceNotFoundException;
import com.emc.ecs.serviceBroker.EcsService;
import com.emc.ecs.serviceBroker.repository.ServiceInstanceBinding;
import com.emc.ecs.serviceBroker.repository.ServiceInstanceBindingRepository;

@Service
public class EcsServiceInstanceBindingService implements ServiceInstanceBindingService {

	@Autowired
	private EcsService ecs;

	@Autowired
	private ServiceInstanceBindingRepository repository;

	public EcsServiceInstanceBindingService()
			throws EcsManagementClientException, EcsManagementResourceNotFoundException, URISyntaxException {
		super();
	}

	@Override
	public CreateServiceInstanceBindingResponse createServiceInstanceBinding(
			CreateServiceInstanceBindingRequest request)
					throws ServiceInstanceBindingExistsException, ServiceBrokerException {
		UserSecretKey userSecret;
		String instanceId = request.getServiceInstanceId();
		String bindingId = request.getBindingId();
		ServiceInstanceBinding binding = new ServiceInstanceBinding(request);
		Map<String, Object> credentials = new HashMap<String, Object>();
		Map<String, Object> parameters = request.getParameters();
		credentials.put("accessKey", ecs.prefix(bindingId));
		credentials.put("bucket", ecs.prefix(instanceId));
		try {
			if (ecs.userExists(bindingId))
				throw new ServiceInstanceBindingExistsException(instanceId, bindingId);
			userSecret = ecs.createUser(bindingId);
			if (parameters != null) {
				@SuppressWarnings("unchecked")
				List<String> permissions = (List<String>) parameters.get("permissions");
				ecs.addUserToBucket(instanceId, bindingId, permissions);				
			} else {
				ecs.addUserToBucket(instanceId, bindingId);
			}
			credentials.put("secretKey", userSecret.getSecretKey());
			credentials.put("endpoint", ecs.getObjectEndpoint());
			credentials.put("baseUrl", ecs.getBaseUrl());
			binding.setBindingId(bindingId);
			binding.setCredentials(credentials);
			repository.save(binding);
		} catch (IOException | JAXBException | EcsManagementResourceNotFoundException
				| EcsManagementClientException e) {
			e.printStackTrace();
			throw new ServiceBrokerException(e.getMessage());
		}
		return new CreateServiceInstanceAppBindingResponse().withCredentials(credentials);
	}

	@Override
	public void deleteServiceInstanceBinding(DeleteServiceInstanceBindingRequest request)
			throws ServiceBrokerException {
		String bindingId = request.getBindingId();
		String instanceId = request.getServiceInstanceId();
	 	try {
			ecs.removeUserFromBucket(instanceId, bindingId);
			ecs.deleteUser(bindingId);
			repository.delete(bindingId);
		} catch (Exception e) {
			throw new ServiceBrokerException(e.getMessage());
		}
	}
}

@@ -93,32 +93,53 @@
 yntaxException;%0A
+import java.net.URL;%0A
 import java.util
@@ -1933,36 +1933,8 @@
 n %7B%0A
-%09%09UserSecretKey userSecret;%0A
 %09%09St
@@ -1979,24 +1979,24 @@
 stanceId();%0A
+
 %09%09String bin
@@ -2456,16 +2456,30 @@
 Id);%0A%09%09%09
+UserSecretKey 
 userSecr
@@ -2781,16 +2781,291 @@
 );%0A%09%09%09%7D%0A
+%09%09%09URL baseUrl = new URL(ecs.getBaseUrl());%0A%09%09%09String userInfo = bindingId + %22:%22 + userSecret.getSecretKey();%0A%09%09%09String s3Url = baseUrl.getProtocol() + %22://%22 + ecs.prefix(userInfo)%0A%09%09%09%09%09+ %22@%22 + baseUrl.getHost() + %22:%22 + baseUrl.getPort() + %22/%22%0A%09%09%09%09%09+ ecs.prefix(instanceId);%0A
 %09%09%09crede
@@ -3169,24 +3169,24 @@
 ndpoint());%0A
-
 %09%09%09credentia
@@ -3222,16 +3222,52 @@
 Url());%0A
+%09%09%09credentials.put(%22s3Url%22, s3Url);%0A
 %09%09%09bindi

Add s3Url param to binding response