View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.identitystore.web;
35  
36  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplication;
37  import fr.paris.lutece.plugins.identitystore.business.application.ClientApplicationHome;
38  import fr.paris.lutece.plugins.identitystore.business.contract.ServiceContract;
39  import fr.paris.lutece.plugins.identitystore.service.contract.ServiceContractService;
40  import fr.paris.lutece.plugins.identitystore.web.exception.IdentityStoreException;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46  import fr.paris.lutece.util.url.UrlItem;
47  import org.apache.commons.lang3.StringUtils;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.stream.Collectors;
53  
54  /**
55   * This class provides the user interface to manage ClientApplication management features ( manage, create, modify, remove )
56   */
57  @Controller( controllerJsp = "ManageClientApplications.jsp", controllerPath = "jsp/admin/plugins/identitystore/", right = "IDENTITYSTORE_ADMIN_MANAGEMENT" )
58  public class ManageClientApplicationJspBean extends ManageIdentitiesJspBean
59  {
60      private static final long serialVersionUID = 1L;
61  
62      // Templates
63      private static final String TEMPLATE_MANAGE_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/manage_clientapplications.html";
64      private static final String TEMPLATE_DISPLAY_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/view_clientapplication.html";
65      private static final String TEMPLATE_CREATE_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/create_clientapplication.html";
66      private static final String TEMPLATE_MODIFY_CLIENTAPPLICATION = "/admin/plugins/identitystore/clientapplication/modify_clientapplication.html";
67  
68      // Query param
69      private static final String QUERY_PARAM_CLIENT_APPLICATION_NAME = "client_application_name";
70      private static final String QUERY_PARAM_APPLICATION_CODE = "application_code";
71      private static final String QUERY_PARAM_CLIENT_CODE = "client_code";
72  
73      // Parameters
74      private static final String PARAMETER_ID_CLIENTAPPLICATION = "id";
75  
76      private static final String PARAMETER_CERTIFIERS_AUTH = "certif_auth";
77      private static final String PARAMETER_CERTIFIER_CODE = "certif_code";
78  
79      // Properties for page titles
80      private static final String PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS = "identitystore.manage_clientapplications.pageTitle";
81      private static final String PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION = "identitystore.modify_clientapplication.pageTitle";
82      private static final String PROPERTY_PAGE_TITLE_CREATE_CLIENTAPPLICATION = "identitystore.create_clientapplication.pageTitle";
83      private static final String PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION_CERTIFICATOR = "identitystore.modify_clientapplication_certificator.pageTitle";
84  
85      // Markers
86      private static final String MARK_CLIENTAPPLICATION_LIST = "clientapplication_list";
87      private static final String MARK_CLIENTAPPLICATION = "clientapplication";
88      private static final String MARK_SERVICECONTRACTS = "servicecontract_list";
89      private static final String MARK_ACTIVECONTRACT = "servicecontract_active";
90      private static final String MARK_CLIENTAPPLICATION_CERTIF_LIST = "clientapplication_certifs";
91      private static final String MARK_CLIENTAPPLICATION_CERTIF_CODE_MAP = "map_clientapplication_certifs";
92      private static final String MARK_CERTIFIERS = "certifiers";
93      private static final String MARK_CLIENTAPPLICATION_RIGHT_LIST = "clientapplication_rights_list";
94      private static final String JSP_MANAGE_CLIENTAPPLICATIONS = "jsp/admin/plugins/identitystore/ManageClientApplications.jsp";
95  
96      // Properties
97      private static final String MESSAGE_CONFIRM_REMOVE_CLIENTAPPLICATION = "identitystore.message.confirmRemoveClientApplication";
98  
99      // Validations
100     private static final String VALIDATION_ATTRIBUTES_PREFIX = "identitystore.model.entity.clientapplication.attribute.";
101 
102     // Views
103     private static final String VIEW_MANAGE_CLIENTAPPLICATIONS = "manageClientApplications";
104     private static final String VIEW_DISPLAY_CLIENTAPPLICATIONS = "displayClientApplication";
105     private static final String VIEW_CREATE_CLIENTAPPLICATION = "createClientApplication";
106     private static final String VIEW_MODIFY_CLIENTAPPLICATION = "modifyClientApplication";
107     private static final String VIEW_MANAGE_CLIENTAPPLICATION_CERTIFICATOR = "manageClientApplicationCertificate";
108 
109     // Actions
110     private static final String ACTION_CREATE_CLIENTAPPLICATION = "createClientApplication";
111     private static final String ACTION_MODIFY_CLIENTAPPLICATION = "modifyClientApplication";
112     private static final String ACTION_MANAGE_CLIENTAPPLICATION_CERTIFICATOR = "manageClientApplicationCertificate";
113     private static final String ACTION_REMOVE_CLIENTAPPLICATION = "removeClientApplication";
114     private static final String ACTION_CONFIRM_REMOVE_CLIENTAPPLICATION = "confirmRemoveClientApplication";
115     private static final String ACTION_REMOVE_CLIENTAPPLICATION_CERTIFICATOR = "removeClientApplicationCertificate";
116 
117     // Infos
118     private static final String INFO_CLIENTAPPLICATION_CREATED = "identitystore.info.clientapplication.created";
119     private static final String INFO_CLIENTAPPLICATION_UPDATED = "identitystore.info.clientapplication.updated";
120     private static final String INFO_CLIENTAPPLICATION_REMOVED = "identitystore.info.clientapplication.removed";
121 
122     // Session variable to store working values
123     private ClientApplication _clientApplication;
124 
125     /**
126      * Build the Manage View
127      *
128      * @param request
129      *            The HTTP request
130      * @return The page
131      */
132     @View( value = VIEW_MANAGE_CLIENTAPPLICATIONS, defaultView = true )
133     public String getManageClientApplications( HttpServletRequest request )
134     {
135         _clientApplication = null;
136 
137         final String name = request.getParameter( QUERY_PARAM_CLIENT_APPLICATION_NAME );
138         final String appCode = request.getParameter( QUERY_PARAM_APPLICATION_CODE );
139         final String clientCode = request.getParameter( QUERY_PARAM_CLIENT_CODE );
140 
141         final List<ClientApplication> listClientApplications = ClientApplicationHome.selectApplicationList( ).stream( )
142                 .filter( ca -> StringUtils.isBlank( name ) || ( ca.getName( ) != null && ca.getName( ).toLowerCase( ).contains( name ) ) )
143                 .filter( ca -> StringUtils.isBlank( appCode )
144                         || ( ca.getApplicationCode( ) != null && ca.getApplicationCode( ).toLowerCase( ).contains( appCode ) ) )
145                 .filter( ca -> StringUtils.isBlank( clientCode )
146                         || ( ca.getClientCode( ) != null && ca.getClientCode( ).toLowerCase( ).contains( clientCode ) ) )
147                 .sorted( ( a, b ) -> {
148                     final int compare = StringUtils.compare( a.getApplicationCode( ), b.getApplicationCode( ), false );
149                     if ( compare == 0 )
150                     {
151                         return StringUtils.compare( a.getClientCode( ), b.getClientCode( ), false );
152                     }
153                     return compare;
154                 } ).collect( Collectors.toList( ) );
155 
156         Map<String, Object> model = getPaginatedListModel( request, MARK_CLIENTAPPLICATION_LIST, listClientApplications, JSP_MANAGE_CLIENTAPPLICATIONS );
157         model.put( QUERY_PARAM_CLIENT_APPLICATION_NAME, name );
158         model.put( QUERY_PARAM_CLIENT_CODE, clientCode );
159         model.put( QUERY_PARAM_APPLICATION_CODE, appCode );
160 
161         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS, TEMPLATE_MANAGE_CLIENTAPPLICATION, model );
162     }
163 
164     /**
165      * Build the Manage View
166      *
167      * @param request
168      *            The HTTP request
169      * @return The page
170      */
171     @View( value = VIEW_DISPLAY_CLIENTAPPLICATIONS )
172     public String getDisplayClientApplications( HttpServletRequest request )
173     {
174         _clientApplication = null;
175 
176         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
177 
178         Map<String, Object> model = getModel( );
179 
180         _clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
181         List<ServiceContract> serviceContracts = ClientApplicationHome.selectServiceContracts( _clientApplication );
182         _clientApplication.setServiceContracts( serviceContracts );
183 
184         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
185 
186         return getPage( PROPERTY_PAGE_TITLE_MANAGE_CLIENTAPPLICATIONS, TEMPLATE_DISPLAY_CLIENTAPPLICATION, model );
187     }
188 
189     /**
190      * Returns the form to create a clientapplication
191      *
192      * @param request
193      *            The Http request
194      * @return the html code of the clientapplication form
195      */
196     @View( VIEW_CREATE_CLIENTAPPLICATION )
197     public String getCreateClientApplication( HttpServletRequest request )
198     {
199         _clientApplication = new ClientApplication( );
200 
201         Map<String, Object> model = getModel( );
202         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
203 
204         return getPage( PROPERTY_PAGE_TITLE_CREATE_CLIENTAPPLICATION, TEMPLATE_CREATE_CLIENTAPPLICATION, model );
205     }
206 
207     /**
208      * Process the data capture form of a new clientapplication
209      *
210      * @param request
211      *            The Http Request
212      * @return The Jsp URL of the process result
213      */
214     @Action( ACTION_CREATE_CLIENTAPPLICATION )
215     public String doCreateClientApplication( HttpServletRequest request )
216     {
217         populate( _clientApplication, request );
218 
219         // Check constraints
220         if ( !validateBean( _clientApplication, VALIDATION_ATTRIBUTES_PREFIX ) )
221         {
222             return redirectView( request, VIEW_CREATE_CLIENTAPPLICATION );
223         }
224 
225         ClientApplicationHome.create( _clientApplication );
226 
227         addInfo( INFO_CLIENTAPPLICATION_CREATED, getLocale( ) );
228 
229         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
230     }
231 
232     /**
233      * Manages the removal form of a clientapplication whose identifier is in the http request
234      *
235      * @param request
236      *            The Http request
237      * @return the html code to confirm
238      */
239     @Action( ACTION_CONFIRM_REMOVE_CLIENTAPPLICATION )
240     public String getConfirmRemoveClientApplication( HttpServletRequest request )
241     {
242         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
243         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CLIENTAPPLICATION ) );
244         url.addParameter( PARAMETER_ID_CLIENTAPPLICATION, nId );
245 
246         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CLIENTAPPLICATION, url.getUrl( ),
247                 AdminMessage.TYPE_CONFIRMATION );
248 
249         return redirect( request, strMessageUrl );
250     }
251 
252     /**
253      * Handles the removal form of a clientapplication
254      *
255      * @param request
256      *            The Http request
257      * @return the jsp URL to display the form to manage clientapplications
258      */
259     @Action( ACTION_REMOVE_CLIENTAPPLICATION )
260     public String doRemoveClientApplication( HttpServletRequest request )
261     {
262         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
263 
264         ClientApplication clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
265         try
266         {
267             ServiceContractService.instance( ).deleteApplication( clientApplication );
268         }
269         catch( IdentityStoreException e )
270         {
271             addError( e.getMessage( ) );
272             return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
273         }
274         addInfo( INFO_CLIENTAPPLICATION_REMOVED, getLocale( ) );
275 
276         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
277     }
278 
279     /**
280      * Returns the form to update info about a clientapplication
281      *
282      * @param request
283      *            The Http request
284      * @return The HTML form to update info
285      */
286     @View( VIEW_MODIFY_CLIENTAPPLICATION )
287     public String getModifyClientApplication( HttpServletRequest request )
288     {
289         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CLIENTAPPLICATION ) );
290 
291         if ( ( _clientApplication == null ) || ( _clientApplication.getId( ) != nId ) )
292         {
293             _clientApplication = ClientApplicationHome.findByPrimaryKey( nId );
294         }
295 
296         Map<String, Object> model = getModel( );
297         model.put( MARK_CLIENTAPPLICATION, _clientApplication );
298 
299         return getPage( PROPERTY_PAGE_TITLE_MODIFY_CLIENTAPPLICATION, TEMPLATE_MODIFY_CLIENTAPPLICATION, model );
300     }
301 
302     /**
303      * Process the change form of a clientapplication
304      *
305      * @param request
306      *            The Http request
307      * @return The Jsp URL of the process result
308      */
309     @Action( ACTION_MODIFY_CLIENTAPPLICATION )
310     public String doModifyClientApplication( HttpServletRequest request )
311     {
312         populate( _clientApplication, request );
313 
314         // Check constraints
315         if ( !validateBean( _clientApplication, VALIDATION_ATTRIBUTES_PREFIX ) )
316         {
317             return redirect( request, VIEW_MODIFY_CLIENTAPPLICATION, PARAMETER_ID_CLIENTAPPLICATION, _clientApplication.getId( ) );
318         }
319 
320         ClientApplicationHome.update( _clientApplication );
321         addInfo( INFO_CLIENTAPPLICATION_UPDATED, getLocale( ) );
322 
323         return redirectView( request, VIEW_MANAGE_CLIENTAPPLICATIONS );
324     }
325 
326 }