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       * Creates the XML string
86       *
87       * @param plugin the plugin
88       * @return String the XML content of the user list
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          //Open AutorisationWsso
98          XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
99  
100         //Open ApplicationWsso
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         //Add transmissionDate
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         //Add responsable
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         //Add list of allowedUser
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         //Close applicationWsso
133         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
134 
135         //Close autorisationWsso
136         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
137 
138         return strXml.toString(  );
139     }
140 
141     /**
142      * Create or update the XML file with the getXml content
143      *
144      * @param plugin the plugin
145      * @return the result of the creation
146      */
147     public static String createXmlFile( Plugin plugin )
148     {
149         //String buffer for building the response page
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             // Test if the pushXml directory exist and create it if it doesn't exist
157             if ( !new File( strFolderPath ).exists(  ) )
158             {
159                 File fileFolder = new File( strFolderPath );
160                 fileFolder.mkdir(  );
161             }
162 
163             // Creates a temporary XML file
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             // Deletes the file if the file exists and renames the temporary file into the file
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      * Delete the XML file on the file system
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         // Deletes the file if the file exists
202         if ( file.exists(  ) )
203         {
204             file.delete(  );
205         }
206     }
207 }