View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * DataClientService
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      /** Private constructor */
61      private DataClientService( )
62      {
63      }
64  
65      /**
66       * Return the unique instance
67       * 
68       * @return The unique instance
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       * Init clients
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       * Gets a DataClient object for a given name
97       * 
98       * @param strName
99       *            The Data Client name
100      * @return The Data Client
101      */
102     public DataClient getClient( String strName )
103     {
104         return _mapClients.get( strName );
105     }
106 
107     /**
108      * Gets a DataClient object for a given name
109      * 
110      * @param strName
111      *            The Data Client name
112      * @return The Data Client
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             // get Default data client
140             dataClient = getDefaultClient( request );
141         }
142 
143         return dataClient;
144     }
145 
146     /**
147      * Gets a DataClient object for a given name
148      * 
149      * @param strName
150      *            The Data Client name
151      * @return The Data Client
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      * Gets the dataclient URL
163      * 
164      * @param request
165      *            the httpservlet Request
166      * @param strDataClientName
167      *            The data client name
168      * @param strHandlerName
169      *            the HandlerName
170      * @return The URL
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 }