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 public WssoUsersFileGeneratorService( )
89 {
90 }
91
92
93
94
95
96
97
98 public static String getXml( Plugin plugin )
99 {
100 StringBuffer strXml = new StringBuffer( );
101 Date date = new Date( );
102 HashMap attrList = new HashMap( );
103 Collection<WssoUser> userList = WssoUserHome.findWssoUsersList( plugin );
104
105
106 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
107
108
109 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID ),
110 AppPropertiesService.getProperty( PROPERTY_APP_ID ) );
111 XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ),
112 attrList );
113 attrList.clear( );
114
115
116 DateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
117 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE ),
118 dateFormat.format( date ) );
119 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE ),
120 attrList );
121 attrList.clear( );
122
123
124 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL ),
125 AppPropertiesService.getProperty( PROPERTY_APP_RESPONSABLE ) );
126 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE ),
127 attrList );
128 attrList.clear( );
129
130
131 for ( WssoUser user : userList )
132 {
133 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID ),
134 user.getGuid( ) );
135 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ALLOWED_USER ),
136 attrList );
137 attrList.clear( );
138 }
139
140
141 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
142
143
144 XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
145
146 return strXml.toString( );
147 }
148
149
150
151
152
153
154 public static String createXmlFile( Plugin plugin )
155 {
156 FileWriter fileXmlWriter = null;
157
158
159 StringBuffer sbLogs = new StringBuffer( );
160 String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
161 String strFolderPath = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" );
162
163 try
164 {
165
166 if ( !new File( strFolderPath ).exists( ) )
167 {
168 File fileFolder = new File( strFolderPath );
169 fileFolder.mkdir( );
170 }
171
172
173 File fileXml = new File( strFolderPath + strFileName );
174 File fileXmlDirectory = new File( strFolderPath );
175 File fileXmlTemp = File.createTempFile( "tmp", null, fileXmlDirectory );
176 fileXmlWriter = new FileWriter( fileXmlTemp );
177 fileXmlWriter.write( getXml( plugin ) );
178 fileXmlWriter.flush( );
179 fileXmlWriter.close( );
180
181
182 removeXmlFile( );
183 fileXmlTemp.renameTo( fileXml );
184 sbLogs.append( LOG_MESSAGE_OK + strFileName );
185 }
186 catch ( IOException e )
187 {
188 AppLogService.error( e.getMessage( ), e );
189 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
190 }
191 catch ( NullPointerException e )
192 {
193 AppLogService.error( e.getMessage( ), e );
194 sbLogs.append( LOG_MESSAGE_NOK + strFileName );
195 }
196
197 return sbLogs.toString( );
198 }
199
200
201
202
203
204 public static void removeXmlFile( )
205 {
206 String strFileXml = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" ) +
207 AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
208 File file = new File( strFileXml );
209
210
211 if ( file.exists( ) )
212 {
213 file.delete( );
214 }
215 }
216 }