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  package fr.paris.lutece.plugins.identitystore.modules.cnicertifier.web;
35  
36  import fr.paris.lutece.plugins.identitystore.modules.cnicertifier.business.CNI;
37  import fr.paris.lutece.plugins.identitystore.modules.cnicertifier.service.CNICertifierService;
38  import fr.paris.lutece.plugins.identitystore.modules.cnicertifier.service.ScannerException;
39  import fr.paris.lutece.plugins.identitystore.modules.cnicertifier.service.ScannerService;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.security.LuteceUser;
42  import fr.paris.lutece.portal.service.security.SecurityService;
43  import fr.paris.lutece.portal.service.security.UserNotSignedException;
44  import fr.paris.lutece.portal.service.util.AppLogService;
45  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
46  import fr.paris.lutece.portal.web.xpages.XPage;
47  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
48  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
49  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
50  import fr.paris.lutece.portal.web.l10n.LocaleService;
51  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
52  import fr.paris.lutece.util.httpaccess.HttpAccessException;
53  import java.util.HashMap;
54  import java.util.Locale;
55  import java.util.Map;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import org.apache.commons.fileupload.FileItem;
59  
60  /**
61   * This class provides a simple implementation of an XPage
62   */
63  @Controller( xpageName = "cnicertifier", pageTitleI18nKey = "module.identitystore.cnicertifier.xpage.cnicertifier.pageTitle", pagePathI18nKey = "module.identitystore.cnicertifier.xpage.cnicertifier.pagePathLabel" )
64  public class CNICertifierApp extends MVCApplication
65  {
66      private static final String TEMPLATE_XPAGE = "/skin/plugins/identitystore/modules/cnicertifier/cnicertifier.html";
67      private static final String TEMPLATE_CNI = "/skin/plugins/identitystore/modules/cnicertifier/cni.html";
68      private static final String VIEW_HOME = "home";
69      private static final String VIEW_CNI = "cni";
70      private static final String ACTION_SCAN = "scan";
71      private static final String ACTION_CERTIFY = "certify";
72      private static final String PARAMETER_IMAGE = "image";
73      private static final String MARK_CNI = "cni";
74      private static final String MESSAGE_NO_FILE_SELECTED = "module.identitystore.cnicertifier.message.noFileSelected";
75      private static final String MESSAGE_SCAN_FAILED = "module.identitystore.cnicertifier.message.scanFailed";
76  
77      private static CNICertifierService _certifierService = new CNICertifierService( );
78  
79      private CNI _cni;
80  
81      /**
82       * Returns the content of the page cnicertifier.
83       * 
84       * @param request
85       *            The HTTP request
86       * @return The view
87       */
88      @View( value = VIEW_HOME, defaultView = true )
89      public XPage viewHome( HttpServletRequest request )
90      {
91          return getXPage( TEMPLATE_XPAGE, request.getLocale( ) );
92      }
93  
94      /**
95       * Scan the uploaded image
96       * 
97       * @param request
98       *            The HTTP request
99       * @return The redirected page
100      */
101     @Action( ACTION_SCAN )
102     public XPage doScan( HttpServletRequest request )
103     {
104         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
105 
106         FileItem fileItem = multipartRequest.getFile( PARAMETER_IMAGE );
107         if ( fileItem.getName( ).equals( "" ) )
108         {
109             String strError = I18nService.getLocalizedString( MESSAGE_NO_FILE_SELECTED, LocaleService.getDefault( ) );
110             addError( strError );
111             return redirectView( request, VIEW_HOME );
112         }
113         Map<String, FileItem> mapFiles = new HashMap<>( );
114         mapFiles.put( PARAMETER_IMAGE, fileItem );
115         _cni = null;
116         try
117         {
118             _cni = ScannerService.scan( mapFiles );
119         }
120         catch( ScannerException ex )
121         {
122             String strError;
123             if( ex.getCode() != null )
124             {
125                 strError = ex.getUserMessage();
126             }
127             else
128             {
129                 strError = I18nService.getLocalizedString( MESSAGE_SCAN_FAILED, LocaleService.getDefault( ) );
130             }
131             addError( strError );
132             AppLogService.error( "Error scanning CNI : " + ex.getMessage( ), ex );
133             return redirectView( request, VIEW_HOME );
134         }
135         catch( HttpAccessException ex  )
136         {
137             String strError = I18nService.getLocalizedString( MESSAGE_SCAN_FAILED, LocaleService.getDefault( ) );
138             addError( strError );
139             AppLogService.error( "Error scanning CNI : " + ex.getMessage( ), ex );
140             return redirectView( request, VIEW_HOME );
141             
142         }
143         return redirectView( request, VIEW_CNI );
144     }
145 
146     /**
147      * View CNI
148      * 
149      * @param request
150      *            The HTTP request
151      * @return The page
152      */
153     @View( VIEW_CNI )
154     public XPage viewCNI( HttpServletRequest request )
155     {
156         if ( _cni != null )
157         {
158             Map<String, Object> model = getModel( );
159             model.put( MARK_CNI, _cni );
160             return getXPage( TEMPLATE_CNI, request.getLocale( ), model );
161         }
162         return redirectView( request, VIEW_HOME );
163     }
164 
165     @Action( ACTION_CERTIFY )
166     public XPage doCertify( HttpServletRequest request ) throws UserNotSignedException
167     {
168         LuteceUser user = checkUser( request );
169         _certifierService.certify( user, _cni );
170         addInfo( "Certify successfull" ); // FIXME
171         return redirectView( request, VIEW_HOME );
172     }
173 
174     private LuteceUser checkUser( HttpServletRequest request ) throws UserNotSignedException
175     {
176         LuteceUser user = SecurityService.getInstance( ).getRegisteredUser( request );
177         if ( user == null )
178         {
179             throw new UserNotSignedException( );
180         }
181         return user;
182     }
183 
184 }