View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.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   * @author lutecer
59   */
60  public class WssoUsersFileGeneratorService
61  {
62      /////////////////////////////////////////////////////////////////////////////////
63      // Constants
64      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
65  
66      /////////////////////////////////////////////////////////////////////////////////
67      // Properties
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       * Creates a new instance of WssoUsersFileGeneratorService
87       */
88      public WssoUsersFileGeneratorService(  )
89      {
90      }
91  
92      /**
93       * Creates the XML string
94       *
95       * @param plugin the plugin
96       * @return String the XML content of the user list
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         //Open AutorisationWsso
106         XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
107 
108         //Open ApplicationWsso
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         //Add transmissionDate
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         //Add responsable
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         //Add list of allowedUser
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         //Close applicationWsso
141         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
142 
143         //Close autorisationWsso
144         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
145 
146         return strXml.toString(  );
147     }
148 
149     /**
150      * Create or update the XML file with the getXml content
151      *
152      * @param plugin the plugin
153      */
154     public static String createXmlFile( Plugin plugin )
155     {
156         FileWriter fileXmlWriter = null;
157 
158         //String buffer for building the response page
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             // Test if the pushXml directory exist and create it if it doesn't exist
166             if ( !new File( strFolderPath ).exists(  ) )
167             {
168                 File fileFolder = new File( strFolderPath );
169                 fileFolder.mkdir(  );
170             }
171 
172             // Creates a temporary XML file
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             // Deletes the file if the file exists and renames the temporary file into the file
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      * Delete the XML file on the file system
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         // Deletes the file if the file exists
211         if ( file.exists(  ) )
212         {
213             file.delete(  );
214         }
215     }
216 }