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.mylutece.modules.wssodatabase.service;
35
36 import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.WssoUser;
37 import fr.paris.lutece.plugins.mylutece.modules.wssodatabase.authentication.business.WssoUserHome;
38 import fr.paris.lutece.portal.service.plugin.Plugin;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40 import fr.paris.lutece.portal.service.util.AppPathService;
41 import fr.paris.lutece.portal.service.util.AppPropertiesService;
42 import fr.paris.lutece.util.xml.XmlUtil;
43
44 import java.io.File;
45 import java.io.FileWriter;
46 import java.io.IOException;
47
48 import java.text.DateFormat;
49 import java.text.SimpleDateFormat;
50
51 import java.util.Collection;
52 import java.util.Date;
53 import java.util.HashMap;
54
55
56
57
58
59
60 public class WssoUsersFileGeneratorService
61 {
62
63
64 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
65
66
67
68 private static final String PROPERTY_APP_ID = "mylutece-wssodatabase.appID";
69 private static final String PROPERTY_APP_RESPONSABLE = "mylutece-wssodatabase.appResponsable";
70 private static final String PROPERTY_XML_STORAGE_FOLDER_PATH = "mylutece-wssodatabase.path";
71 private static final String PROPERTY_XML_FILE_NAME = "mylutece-wssodatabase.fileName";
72 private static final String PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO = "mylutece-wssodatabase.wssofileformat.tag_autorisationWSSO";
73 private static final String PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO = "mylutece-wssodatabase.wssofileformat.tag_applicationWSSO";
74 private static final String PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID = "mylutece-wssodatabase.wssofileformat.tag_appID";
75 private static final String PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE = "mylutece-wssodatabase.wssofileformat.tag_transmissionDate";
76 private static final String PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE = "mylutece-wssodatabase.wssofileformat.tag_date";
77 private static final String PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE = "mylutece-wssodatabase.wssofileformat.tag_appResponsable";
78 private static final String PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL = "mylutece-wssodatabase.wssofileformat.tag_mail";
79 private static final String PROPERTY_XMLFILEFORMAT_ALLOWED_USER = "mylutece-wssodatabase.wssofileformat.tag_allowedUser";
80 private static final String PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID = "mylutece-wssodatabase.wssofileformat.tag_wssoGUID";
81 private static final String LOG_MESSAGE_OK = "\nWssoUserFileGeneratorService : Update OK for file ";
82 private static final String LOG_MESSAGE_NOK = "\nWssoUserFileGeneratorService : Error when updating file ";
83
84
85
86
87
88
89
90 public static String getXml( Plugin plugin )
91 {
92 StringBuffer strXml = new StringBuffer( );
93 Date date = new Date( );
94 HashMap attrList = new HashMap( );
95 Collection<WssoUser> userList = WssoUserHome.findWssoUsersList( plugin );
96
97
98 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
99
100
101 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID ),
102 AppPropertiesService.getProperty( PROPERTY_APP_ID ) );
103 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ),
104 attrList );
105 attrList.clear( );
106
107
108 DateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
109 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE ),
110 dateFormat.format( date ) );
111 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE ),
112 attrList );
113 attrList.clear( );
114
115
116 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL ),
117 AppPropertiesService.getProperty( PROPERTY_APP_RESPONSABLE ) );
118 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE ),
119 attrList );
120 attrList.clear( );
121
122
123 for ( WssoUser user : userList )
124 {
125 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID ),
126 user.getGuid( ) );
127 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ALLOWED_USER ),
128 attrList );
129 attrList.clear( );
130 }
131
132
133 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
134
135
136 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
137
138 return strXml.toString( );
139 }
140
141
142
143
144
145
146
147 public static String createXmlFile( Plugin plugin )
148 {
149
150 StringBuffer sbLogs = new StringBuffer( );
151 String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
152 String strFolderPath = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" );
153
154 try
155 {
156
157 if ( !new File( strFolderPath ).exists( ) )
158 {
159 File fileFolder = new File( strFolderPath );
160 fileFolder.mkdir( );
161 }
162
163
164 File fileXml = new File( strFolderPath + strFileName );
165 File fileXmlDirectory = new File( strFolderPath );
166 File fileXmlTemp = File.createTempFile( "tmp", null, fileXmlDirectory );
167 FileWriter fileXmlWriter = new FileWriter( fileXmlTemp );
168 fileXmlWriter.write( getXml( plugin ) );
169 fileXmlWriter.flush( );
170 fileXmlWriter.close( );
171
172
173 removeXmlFile( );
174 fileXmlTemp.renameTo( fileXml );
175 sbLogs.append( LOG_MESSAGE_OK + strFileName );
176 }
177 catch ( IOException e )
178 {
179 AppLogService.error( e.getMessage( ), e );
180 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
181 }
182 catch ( NullPointerException e )
183 {
184 AppLogService.error( e.getMessage( ), e );
185 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
186 }
187
188 return sbLogs.toString( );
189 }
190
191
192
193
194
195 public static void removeXmlFile( )
196 {
197 String strFileXml = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" ) +
198 AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
199 File file = new File( strFileXml );
200
201
202 if ( file.exists( ) )
203 {
204 file.delete( );
205 }
206 }
207 }