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.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
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
83
84
85
86
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
96
97
98
99
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
148
149
150
151
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" );
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 }