package org.gluu.oxtrust.ws.rs.passport;

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

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.gluu.oxtrust.ldap.service.PassportService;
import org.gluu.oxtrust.model.passport.PassportConfigResponse;
import org.gluu.oxtrust.service.filter.ProtectedApi;
import org.xdi.config.oxtrust.LdapOxPassportConfiguration;
import org.xdi.model.SimpleExtendedCustomProperty;
import org.xdi.service.JsonService;

/**
 * PassportConfigurationEndPoint Implementation
 * 
 * @author Shekhar L.
 * @author Yuriy Movchan Date: 12/06/2016
 */

@Named("PassportConfigurationEndPoint")
@Path("/passport/config")
public class PassportRestWebService {

	@Inject
	private PassportService passportService;

	@Inject
	private JsonService jsonService;

	@GET
	@Produces({ MediaType.APPLICATION_JSON })
    @ProtectedApi
	public Response getPassportConfig() {
		PassportConfigResponse passportConfigResponse = new PassportConfigResponse();
		
		Map <String,Map> strategies = new HashMap <String,Map>();

		LdapOxPassportConfiguration ldapOxPassportConfiguration = passportService.loadConfigurationFromLdap();
		if(ldapOxPassportConfiguration != null ){
			for (org.xdi.model.passport.PassportConfiguration passportConfiguration : ldapOxPassportConfiguration.getPassportConfigurations()) {
				if(passportConfiguration != null){
					Map<String, String> map = new HashMap<String, String>();
					List<SimpleExtendedCustomProperty>  passList = passportConfiguration.getFieldset();
					if (passList != null) {
    					for( SimpleExtendedCustomProperty fieldset :  passList ){
    						map.put(fieldset.getValue1(), fieldset.getValue2());
    					}
					}		
					
					strategies.put(passportConfiguration.getStrategy(),map);
				}
			}
		}
		passportConfigResponse.setPassportStrategies(strategies);

		String passportConfigResponseJson;
		try {
			passportConfigResponseJson = jsonService.objectToPerttyJson(passportConfigResponse);
		} catch (IOException ex) {
			return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Failed to prepare configuration");
		}

		return Response.status(Response.Status.OK).entity(passportConfigResponseJson).build();
	}


	protected Response getErrorResponse(Response.Status status, String detail) {
		return Response.status(status).entity(detail).build();
	}

}

@@ -592,36 +592,25 @@
 del.
-SimpleExtendedCustomProperty
+passport.FieldSet
 ;%0Aim
@@ -1325,20 +1325,27 @@
 Ldap();%0A
-%09%09
+        
 if
+ 
 (ldapOxP
@@ -1373,22 +1373,99 @@
 != null 
-)%7B%0A%09%09%09
+&& ldapOxPassportConfiguration.getPassportConfigurations() != null) %7B%0A%0A            
 for (org
@@ -1589,20 +1589,32 @@
 ns()) %7B%0A
-%09%09%09%09
+                
 if(passp
@@ -1640,21 +1640,36 @@
  null)%7B%0A
-%09%09%09%09%09
+                    
 Map%3CStri
@@ -1721,46 +1721,41 @@
 ();%0A
-%09%09%09%09%09List%3CSimpleExtendedCustomProperty
+                    List%3CFieldSet
 %3E  p
@@ -1801,21 +1801,37 @@
 dset();%0A
-%09%09%09%09%09
+%0A                    
 if (pass
@@ -1854,46 +1854,41 @@
     
-%09%09%09%09%09for( SimpleExtendedCustomProperty
+                    for (FieldSet
  fie
@@ -1908,22 +1908,40 @@
 List
- 
 )
+ 
 %7B%0A    
-%09%09%09%09%09%09
+                        
 map.
@@ -1960,14 +1960,11 @@
 .get
-Value1
+Key
 (), 
@@ -1980,17 +1980,16 @@
 getValue
-2
 ());%0A   
@@ -1993,35 +1993,72 @@
     
-%09%09%09%09%09%7D%0A%09%09%09%09%09%7D%09%09%0A%09%09%09%09%09%0A%09%09%09%09%09
+                    %7D%0A                    %7D%0A                    
 stra
@@ -2108,27 +2108,55 @@
 y(),
+ 
 map);%0A
-%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09
+                %7D%0A            %7D%0A        
 %7D%0A%09%09

Fix for https://github.com/GluuFederation/gluu-passport/issues/31 (master)