View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.openam.service.client.rest;
2   
3   import java.io.IOException;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.apache.commons.lang.StringUtils;
8   
9   import com.fasterxml.jackson.core.JsonGenerationException;
10  import com.fasterxml.jackson.core.JsonParseException;
11  import com.fasterxml.jackson.core.JsonProcessingException;
12  import com.fasterxml.jackson.databind.JsonMappingException;
13  import com.fasterxml.jackson.databind.JsonNode;
14  import com.fasterxml.jackson.databind.ObjectMapper;
15  import com.fasterxml.jackson.databind.module.SimpleModule;
16  
17  import fr.paris.lutece.util.httpaccess.HttpAccess;
18  import fr.paris.lutece.util.httpaccess.HttpAccessException;
19  
20  public class OpenamAgentRestService {
21  	
22  	
23  	public static final String OPENAM_API_VERSION= "resource=2.0, protocol=1.0";
24  
25  	/**
26  	 * Returns the tokenId to access on openam api rest.
27  	 * @param strUrl The server openam url
28  	 * @param agent The OpenamAgentDTO
29  	 * @return return the openam API response
30  	 * @throws HttpAccessException
31  	 * @throws JsonMappingException
32  	 * @throws JsonParseException
33  	 * @throws IOException
34  	 */
35  	public static String getToken( String strUrl, OpenamAgentDTO agent) throws HttpAccessException, JsonMappingException, JsonParseException, IOException{
36  
37  		HttpAccess httpAccess = new HttpAccess();
38  		Map<String, String> headersRequest= new  HashMap<String, String>();
39  		headersRequest.put(OpenamAgentDTO.ACCEPT_API_VERSION, OPENAM_API_VERSION);
40  		headersRequest.put(OpenamAgentDTO.X_OPENAM_USERNAME, agent.getOpenamUserName( ));
41  		headersRequest.put(OpenamAgentDTO.X_OPENAM_PASSWORD, agent.getOpenamPassword( ));
42  		String response= httpAccess.doPost( strUrl+"/json/authenticate", null, null, null, headersRequest );
43  		
44  		ObjectMapper objectMapper = new ObjectMapper();
45  		JsonNode jsonNode = objectMapper.readTree(response);
46  		
47  		return jsonNode.get("tokenId").asText();
48  	}
49  
50  	/**
51  	 * Create Agent in the openam api
52  	 * @param strUrl The server openam url 
53  	 * @param strTokenId The tokenId
54  	 * @param agent The OpenamAgentDTO
55  	 * @return return the openam API response
56  	 * @throws JsonMappingException
57  	 * @throws JsonGenerationException
58  	 * @throws IOException
59  	 * @throws HttpAccessException
60  	 */
61  	public static String createAgentProfile( String strUrl, String strTokenId, OpenamAgentDTO agent, boolean bProd)  throws JsonMappingException, JsonGenerationException, IOException, HttpAccessException{
62  		
63  		HttpAccess httpAccess = new HttpAccess();
64  		Map<String, String> headersRequest= new  HashMap<String, String>();
65  		headersRequest.put(OpenamAgentDTO.ACCEPT_API_VERSION, OPENAM_API_VERSION);
66  		headersRequest.put(bProd?OpenamAgentDTO.COOKIE_MCPAUTH:OpenamAgentDTO.COOKIE_IPLANETDIRECTOTYPRO, strTokenId);
67  		ObjectMapper mapper = new ObjectMapper();
68  
69  		SimpleModule module = new SimpleModule();
70  		module.addSerializer(OpenamAgentDTO.class, new CreateAgentSerializer());
71  		mapper.registerModule(module);
72  		 
73  		String strJSON = mapper.writeValueAsString(agent);
74  		String response= httpAccess.doPostJSON(strUrl+"/json/agents/?_action=create", strJSON, headersRequest, null);
75  		
76  		return response;
77  	}
78  	
79  	/**
80  	 * Update Agent in the openam api
81  	 * @param strUrl The server openam url 
82  	 * @param strTokenId The tokenId
83  	 * @param agent The OpenamAgentDTO
84  	 * @return  return the openam API response
85  	 * @throws JsonProcessingException
86  	 * @throws HttpAccessException
87  	 */
88  	public static String updateAgentProfile(String strUrl, String strTokenId, OpenamAgentDTO agent, boolean bProd) throws JsonProcessingException, HttpAccessException {
89  		
90  		HttpAccess httpAccess = new HttpAccess();
91  		Map<String, String> headersRequest= new  HashMap<String, String>();
92  		headersRequest.put(OpenamAgentDTO.ACCEPT_API_VERSION, OPENAM_API_VERSION);
93  		headersRequest.put(bProd?OpenamAgentDTO.COOKIE_MCPAUTH:OpenamAgentDTO.COOKIE_IPLANETDIRECTOTYPRO, strTokenId);
94  		ObjectMapper mapper = new ObjectMapper();
95  		 
96  		SimpleModule module = new SimpleModule();
97  		module.addSerializer(OpenamAgentDTO.class, new UpdateAgentSerializer());
98  		mapper.registerModule(module);		 
99  		String strJSON = mapper.writeValueAsString(agent);
100 		String response= httpAccess.doPutJSON(strUrl+"/json/agents/"+agent.getName(), strJSON, headersRequest, null);
101 		
102 		return response;
103 	}
104 	
105 	/**
106 	 * Sign out 
107 	* @param strUrl The server openam url 
108 	 * @param strTokenId The tokenId
109 	 * @return return the openam API response
110 	 * @throws HttpAccessException
111 	 */
112 	public static String logout(String strUrl, String strTokenId,boolean bProd ) throws HttpAccessException {
113 
114 		HttpAccess httpAccess = new HttpAccess();
115 		Map<String, String> headersRequest= new  HashMap<String, String>();
116 		headersRequest.put(bProd?OpenamAgentDTO.COOKIE_MCPAUTH:OpenamAgentDTO.COOKIE_IPLANETDIRECTOTYPRO, strTokenId);
117 		String strJSON= StringUtils.EMPTY;
118 		String response= httpAccess.doPostJSON( strUrl+"/json/sessions/?_action=logout", strJSON, headersRequest, null );
119 		
120 		return response;
121 	}
122 }