View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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  
35  package fr.paris.lutece.plugins.grukeydiversification.web;
36  
37  import fr.paris.lutece.plugins.grukeydiversification.business.EncryptionKey;
38  import fr.paris.lutece.plugins.grukeydiversification.business.EncryptionKeyHome;
39  import fr.paris.lutece.plugins.grukeydiversification.business.exception.EncryptionKeyAlreadyExistException;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
43  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
44  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
45  import fr.paris.lutece.util.url.UrlItem;
46  
47  import java.util.List;
48  import java.util.Map;
49  import javax.servlet.http.HttpServletRequest;
50  
51  /**
52   * This class provides the user interface to manage EncryptionKey features ( manage, create, modify, remove )
53   */
54  @Controller( controllerJsp = "ManageEncryptionKeys.jsp", controllerPath = "jsp/admin/plugins/grukeydiversification/", right = "GRUKEYDIVERSIFICATION_MANAGEMENT" )
55  public class EncryptionKeyJspBean extends AbstractManageEncryptionKeyJspBean
56  {
57      /**
58       * Generated serial ID
59       */
60      private static final long serialVersionUID = 3101755353106198571L;
61  
62      // Templates
63      private static final String TEMPLATE_MANAGE_ENCRYPTIONKEYS = "/admin/plugins/grukeydiversification/manage_encryptionkeys.html";
64      private static final String TEMPLATE_CREATE_ENCRYPTIONKEY = "/admin/plugins/grukeydiversification/create_encryptionkey.html";
65      private static final String TEMPLATE_MODIFY_ENCRYPTIONKEY = "/admin/plugins/grukeydiversification/modify_encryptionkey.html";
66  
67      // Parameters
68      private static final String PARAMETER_ID_ENCRYPTIONKEY = "id";
69  
70      // Properties for page titles
71      private static final String PROPERTY_PAGE_TITLE_MANAGE_ENCRYPTIONKEYS = "grukeydiversification.manage_encryptionkeys.pageTitle";
72      private static final String PROPERTY_PAGE_TITLE_MODIFY_ENCRYPTIONKEY = "grukeydiversification.modify_encryptionkey.pageTitle";
73      private static final String PROPERTY_PAGE_TITLE_CREATE_ENCRYPTIONKEY = "grukeydiversification.create_encryptionkey.pageTitle";
74  
75      // Markers
76      private static final String MARK_ENCRYPTIONKEY_LIST = "encryptionkey_list";
77      private static final String MARK_ENCRYPTIONKEY = "encryptionkey";
78  
79      private static final String JSP_MANAGE_ENCRYPTIONKEYS = "jsp/admin/plugins/grukeydiversification/ManageEncryptionKeys.jsp";
80  
81      // Properties
82      private static final String MESSAGE_CONFIRM_REMOVE_ENCRYPTIONKEY = "grukeydiversification.message.confirmRemoveEncryptionKey";
83  
84      // Validations
85      private static final String VALIDATION_ATTRIBUTES_PREFIX = "grukeydiversification.model.entity.encryptionkey.attribute.";
86  
87      // Views
88      private static final String VIEW_MANAGE_ENCRYPTIONKEYS = "manageEncryptionKeys";
89      private static final String VIEW_CREATE_ENCRYPTIONKEY = "createEncryptionKey";
90      private static final String VIEW_MODIFY_ENCRYPTIONKEY = "modifyEncryptionKey";
91  
92      // Actions
93      private static final String ACTION_CREATE_ENCRYPTIONKEY = "createEncryptionKey";
94      private static final String ACTION_MODIFY_ENCRYPTIONKEY = "modifyEncryptionKey";
95      private static final String ACTION_REMOVE_ENCRYPTIONKEY = "removeEncryptionKey";
96      private static final String ACTION_CONFIRM_REMOVE_ENCRYPTIONKEY = "confirmRemoveEncryptionKey";
97  
98      // Infos
99      private static final String INFO_ENCRYPTIONKEY_CREATED = "grukeydiversification.info.encryptionkey.created";
100     private static final String INFO_ENCRYPTIONKEY_UPDATED = "grukeydiversification.info.encryptionkey.updated";
101     private static final String INFO_ENCRYPTIONKEY_REMOVED = "grukeydiversification.info.encryptionkey.removed";
102 
103     // Errors
104     private static final String ERROR_ENCRYPTIONKEY_ALREADY_EXISTS = "grukeydiversification.error.encryptionkey.alreadyExists";
105 
106     // Session variable to store working values
107     private EncryptionKey _encryptionkey;
108 
109     /**
110      * Build the Manage View
111      * 
112      * @param request
113      *            The HTTP request
114      * @return The page
115      */
116     @View( value = VIEW_MANAGE_ENCRYPTIONKEYS, defaultView = true )
117     public String getManageEncryptionKeys( HttpServletRequest request )
118     {
119         _encryptionkey = null;
120         List<EncryptionKey> listEncryptionKeys = EncryptionKeyHome.getEncryptionKeysList( );
121         Map<String, Object> model = getPaginatedListModel( request, MARK_ENCRYPTIONKEY_LIST, listEncryptionKeys, JSP_MANAGE_ENCRYPTIONKEYS );
122 
123         return getPage( PROPERTY_PAGE_TITLE_MANAGE_ENCRYPTIONKEYS, TEMPLATE_MANAGE_ENCRYPTIONKEYS, model );
124     }
125 
126     /**
127      * Returns the form to create a encryptionkey
128      *
129      * @param request
130      *            The Http request
131      * @return the html code of the encryptionkey form
132      */
133     @View( VIEW_CREATE_ENCRYPTIONKEY )
134     public String getCreateEncryptionKey( HttpServletRequest request )
135     {
136         _encryptionkey = ( _encryptionkey != null ) ? _encryptionkey : new EncryptionKey( );
137 
138         Map<String, Object> model = getModel( );
139         model.put( MARK_ENCRYPTIONKEY, _encryptionkey );
140 
141         return getPage( PROPERTY_PAGE_TITLE_CREATE_ENCRYPTIONKEY, TEMPLATE_CREATE_ENCRYPTIONKEY, model );
142     }
143 
144     /**
145      * Process the data capture form of a new encryptionkey
146      *
147      * @param request
148      *            The Http Request
149      * @return The Jsp URL of the process result
150      */
151     @Action( ACTION_CREATE_ENCRYPTIONKEY )
152     public String doCreateEncryptionKey( HttpServletRequest request )
153     {
154         populate( _encryptionkey, request );
155 
156         // Check constraints
157         if ( !validateBean( _encryptionkey, VALIDATION_ATTRIBUTES_PREFIX ) )
158         {
159             return redirectView( request, VIEW_CREATE_ENCRYPTIONKEY );
160         }
161 
162         try
163         {
164             EncryptionKeyHome.create( _encryptionkey );
165         }
166         catch( EncryptionKeyAlreadyExistException e )
167         {
168             addError( ERROR_ENCRYPTIONKEY_ALREADY_EXISTS, getLocale( ) );
169             return redirectView( request, VIEW_CREATE_ENCRYPTIONKEY );
170         }
171 
172         addInfo( INFO_ENCRYPTIONKEY_CREATED, getLocale( ) );
173 
174         return redirectView( request, VIEW_MANAGE_ENCRYPTIONKEYS );
175     }
176 
177     /**
178      * Manages the removal form of a encryptionkey whose identifier is in the http request
179      *
180      * @param request
181      *            The Http request
182      * @return the html code to confirm
183      */
184     @Action( ACTION_CONFIRM_REMOVE_ENCRYPTIONKEY )
185     public String getConfirmRemoveEncryptionKey( HttpServletRequest request )
186     {
187         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENCRYPTIONKEY ) );
188         UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_ENCRYPTIONKEY ) );
189         url.addParameter( PARAMETER_ID_ENCRYPTIONKEY, nId );
190 
191         String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_ENCRYPTIONKEY, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
192 
193         return redirect( request, strMessageUrl );
194     }
195 
196     /**
197      * Handles the removal form of a encryptionkey
198      *
199      * @param request
200      *            The Http request
201      * @return the jsp URL to display the form to manage encryptionkeys
202      */
203     @Action( ACTION_REMOVE_ENCRYPTIONKEY )
204     public String doRemoveEncryptionKey( HttpServletRequest request )
205     {
206         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENCRYPTIONKEY ) );
207         EncryptionKeyHome.remove( nId );
208         addInfo( INFO_ENCRYPTIONKEY_REMOVED, getLocale( ) );
209 
210         return redirectView( request, VIEW_MANAGE_ENCRYPTIONKEYS );
211     }
212 
213     /**
214      * Returns the form to update info about a encryptionkey
215      *
216      * @param request
217      *            The Http request
218      * @return The HTML form to update info
219      */
220     @View( VIEW_MODIFY_ENCRYPTIONKEY )
221     public String getModifyEncryptionKey( HttpServletRequest request )
222     {
223         int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_ENCRYPTIONKEY ) );
224 
225         if ( _encryptionkey == null || ( _encryptionkey.getId( ) != nId ) )
226         {
227             _encryptionkey = EncryptionKeyHome.findByPrimaryKey( nId );
228         }
229 
230         Map<String, Object> model = getModel( );
231         model.put( MARK_ENCRYPTIONKEY, _encryptionkey );
232 
233         return getPage( PROPERTY_PAGE_TITLE_MODIFY_ENCRYPTIONKEY, TEMPLATE_MODIFY_ENCRYPTIONKEY, model );
234     }
235 
236     /**
237      * Process the change form of a encryptionkey
238      *
239      * @param request
240      *            The Http request
241      * @return The Jsp URL of the process result
242      */
243     @Action( ACTION_MODIFY_ENCRYPTIONKEY )
244     public String doModifyEncryptionKey( HttpServletRequest request )
245     {
246         populate( _encryptionkey, request );
247 
248         // Check constraints
249         if ( !validateBean( _encryptionkey, VALIDATION_ATTRIBUTES_PREFIX ) )
250         {
251             return redirect( request, VIEW_MODIFY_ENCRYPTIONKEY, PARAMETER_ID_ENCRYPTIONKEY, _encryptionkey.getId( ) );
252         }
253 
254         try
255         {
256             EncryptionKeyHome.update( _encryptionkey );
257         }
258         catch( EncryptionKeyAlreadyExistException e )
259         {
260             addError( ERROR_ENCRYPTIONKEY_ALREADY_EXISTS, getLocale( ) );
261             return redirect( request, VIEW_MODIFY_ENCRYPTIONKEY, PARAMETER_ID_ENCRYPTIONKEY, _encryptionkey.getId( ) );
262         }
263 
264         addInfo( INFO_ENCRYPTIONKEY_UPDATED, getLocale( ) );
265 
266         return redirectView( request, VIEW_MANAGE_ENCRYPTIONKEYS );
267     }
268 }