1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.oauth2.service;
35
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.ConcurrentMap;
38
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpSession;
41
42 import org.apache.commons.lang3.StringUtils;
43 import org.apache.log4j.Logger;
44
45 import fr.paris.lutece.plugins.oauth2.dataclient.DataClient;
46 import fr.paris.lutece.plugins.oauth2.web.Constants;
47 import fr.paris.lutece.portal.service.spring.SpringContextService;
48 import fr.paris.lutece.portal.service.util.AppPathService;
49 import fr.paris.lutece.util.url.UrlItem;
50
51
52
53
54 public final class DataClientService
55 {
56 private static DataClientService _singleton;
57 private static ConcurrentMap<String, DataClient> _mapClients;
58 private static Logger _logger = Logger.getLogger( Constants.LOGGER_OAUTH2 );
59
60
61 private DataClientService( )
62 {
63 }
64
65
66
67
68
69
70 public static synchronized DataClientService instance( )
71 {
72 if ( _singleton == null )
73 {
74 _singleton = new DataClientService( );
75 initClients( );
76 }
77
78 return _singleton;
79 }
80
81
82
83
84 private static void initClients( )
85 {
86 _mapClients = new ConcurrentHashMap<>( );
87
88 for ( DataClient client : SpringContextService.getBeansOfType( DataClient.class ) )
89 {
90 _mapClients.put( client.getName( ), client );
91 _logger.info( "New Oaut2 Data Client registered : " + client.getName( ) );
92 }
93 }
94
95
96
97
98
99
100
101
102 public DataClient getClient( String strName )
103 {
104 return _mapClients.get( strName );
105 }
106
107
108
109
110
111
112
113
114 public DataClient getClient( HttpServletRequest request )
115 {
116
117 HttpSession session = request.getSession( true );
118 DataClient dataClient = null;
119 String strDataClientName = request.getParameter( Constants.PARAMETER_DATA_CLIENT );
120 if ( !StringUtils.isEmpty( strDataClientName ) )
121 {
122 dataClient = getClient( strDataClientName );
123 }
124 else
125 {
126 session = request.getSession( true );
127 dataClient = (DataClient) session.getAttribute( Constants.SESSION_ATTRIBUTE_DATACLIENT );
128
129 }
130 if ( dataClient != null )
131 {
132
133 session.setAttribute( Constants.SESSION_ATTRIBUTE_DATACLIENT, dataClient );
134
135 }
136 else
137 {
138
139
140 dataClient = getDefaultClient( request );
141 }
142
143 return dataClient;
144 }
145
146
147
148
149
150
151
152
153 public DataClient getDefaultClient( HttpServletRequest request )
154 {
155
156 return _mapClients.entrySet( ).stream( ).filter( x -> x.getValue( ).isDefault( ) ).map( x -> x.getValue( ) ).findFirst( )
157 .orElse( _mapClients.entrySet( ).stream( ).map( x -> x.getValue( ) ).findFirst( ).orElse( null ) );
158
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172 public String getDataClientUrl( HttpServletRequest request, String strDataClientName, String strHandlerName )
173 {
174 String strCallBackUrlUrl = AppPathService.getAbsoluteUrl( request, Constants.CALL_BACK_SERVLET_URI );
175
176 UrlItem url = new UrlItem( strCallBackUrlUrl );
177 url.addParameter( Constants.PARAMETER_DATA_CLIENT, strDataClientName );
178 if ( strHandlerName != null )
179 {
180 url.addParameter( Constants.PARAMETER_HANDLER_NAME, strHandlerName );
181 }
182 return url.getUrl( );
183 }
184
185 }