1 package fr.paris.lutece.plugins.librarymdph.utils;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import com.fasterxml.jackson.core.JsonProcessingException;
7 import com.fasterxml.jackson.databind.ObjectMapper;
8 import com.google.gson.JsonElement;
9 import com.google.gson.JsonObject;
10 import com.google.gson.JsonParser;
11
12 import fr.paris.lutece.portal.service.util.AppLogService;
13 import fr.paris.lutece.portal.service.util.AppPropertiesService;
14 import fr.paris.lutece.util.httpaccess.HttpAccess;
15 import fr.paris.lutece.util.httpaccess.HttpAccessException;
16
17 public final class MdphConnection {
18
19 private final HttpAccess _clientHttp = new HttpAccess( );
20 private final ObjectMapper _mapper = new ObjectMapper();
21
22 private static final String URL = AppPropertiesService.getProperty( "mdph.ws.url" );
23 private static final String ENDPOINT_LOGIN = AppPropertiesService.getProperty( "mdph.ws.login.endpoint" );
24 private static final String ENDPOINT_MDPH = AppPropertiesService.getProperty( "mdph.ws.mdph.endpoint" );
25 private static final String LOGIN = AppPropertiesService.getProperty( "mdph.ws.login.user" );
26 private static final String PWD = AppPropertiesService.getProperty( "mdph.ws.login.pwd" );
27
28 public String callLogin( )
29 {
30 Map<String, String> headerMap = initHeaderMap();
31
32 Map<String, String> jsonMap = new HashMap<>( );
33 jsonMap.put( MdphConstant.JSON_KEY_USER.getValue(), LOGIN );
34 jsonMap.put( MdphConstant.JSON_KEY_PWD.getValue(), PWD );
35
36 try {
37 String jsonReponse = _clientHttp.doPostJSON( URL + ENDPOINT_LOGIN , _mapper.writeValueAsString( jsonMap ), headerMap, null );
38 JsonElement jelementLogin = new JsonParser( ).parse( jsonReponse );
39 JsonObject jobjectLogin = jelementLogin.getAsJsonObject( );
40 return jobjectLogin.get( MdphConstant.JSON_KEY_TOKEN.getValue( ) ).getAsString( );
41 } catch (JsonProcessingException | HttpAccessException e) {
42 AppLogService.error( "Mdph Login error", e );
43 return null;
44 }
45 }
46
47 public String callMdph( Map<String, String> jsonMap, String token )
48 {
49 Map<String, String> headerMap = initHeaderMap();
50 headerMap.put( MdphConstant.HEADER_KEY_AUTH.getValue( ), MdphConstant.HEADER_VALUE_AUTH_PREFIX.getValue( ) + token );
51
52 String jsonReponse = null;
53 try {
54 jsonReponse = _clientHttp.doPostJSON( URL + ENDPOINT_MDPH, _mapper.writeValueAsString( jsonMap ), headerMap, null );
55 } catch (JsonProcessingException | HttpAccessException e) {
56 AppLogService.error( "Mdph Call error", e );
57 return null;
58 }
59 return jsonReponse;
60 }
61
62 private Map<String, String> initHeaderMap( )
63 {
64 Map<String, String> headerMap = new HashMap<>( );
65 headerMap.put( MdphConstant.HEADER_KEY_CONTENT.getValue( ), MdphConstant.HEADER_VALUE_CONTENT.getValue( ) );
66 return headerMap;
67 }
68 }