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.adminauthenticationwsso.service;
35  
36  import java.io.File;
37  import java.io.FileWriter;
38  import java.io.IOException;
39  import java.text.DateFormat;
40  import java.text.SimpleDateFormat;
41  import java.util.Collection;
42  import java.util.Date;
43  import java.util.HashMap;
44  
45  import fr.paris.lutece.portal.business.user.AdminUser;
46  import fr.paris.lutece.portal.business.user.AdminUserFilter;
47  import fr.paris.lutece.portal.business.user.AdminUserHome;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.util.AppLogService;
50  import fr.paris.lutece.portal.service.util.AppPathService;
51  import fr.paris.lutece.portal.service.util.AppPropertiesService;
52  import fr.paris.lutece.util.xml.XmlUtil;
53  import java.util.regex.Matcher;
54  import java.util.regex.Pattern;
55  
56  
57  /**
58   *
59   * @author lutecer
60   */
61  public class WssoAdminUsersFileGeneratorService
62  {
63      /////////////////////////////////////////////////////////////////////////////////
64      // Constants
65      private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
66  
67      /////////////////////////////////////////////////////////////////////////////////
68      // Properties
69      private static final String PROPERTY_APP_ID = "adminauthenticationwsso.appID";
70      private static final String PROPERTY_APP_RESPONSABLE = "adminauthenticationwsso.appResponsable";
71      private static final String PROPERTY_XML_STORAGE_FOLDER_PATH = "adminauthenticationwsso.path";
72      private static final String PROPERTY_XML_FILE_NAME = "adminauthenticationwsso.fileName";
73      private static final String PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO = "adminauthenticationwsso.wssofileformat.tag_autorisationWSSO";
74      private static final String PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO = "adminauthenticationwsso.wssofileformat.tag_applicationWSSO";
75      private static final String PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID = "adminauthenticationwsso.wssofileformat.tag_appID";
76      private static final String PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE = "adminauthenticationwsso.wssofileformat.tag_transmissionDate";
77      private static final String PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE = "adminauthenticationwsso.wssofileformat.tag_date";
78      private static final String PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE = "adminauthenticationwsso.wssofileformat.tag_appResponsable";
79      private static final String PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL = "adminauthenticationwsso.wssofileformat.tag_mail";
80      private static final String PROPERTY_XMLFILEFORMAT_ALLOWED_USER = "adminauthenticationwsso.wssofileformat.tag_allowedUser";
81      private static final String PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID = "adminauthenticationwsso.wssofileformat.tag_wssoGUID";
82      private static final String LOG_MESSAGE_OK = "\nWssoAdminUserFileGeneratorService : Update OK for file ";
83      private static final String LOG_MESSAGE_NOK = "\nWssoAdminUserFileGeneratorService : Error when updating file ";
84      
85      //Regex
86      private static final String REGEX_WSSO_ID = AppPropertiesService.getProperty( "adminauthenticationwsso.wssoid.regex" );
87      
88      /**
89       *
90       * Creates a new instance of WssoAdminUsersFileGeneratorService
91       */
92      public WssoAdminUsersFileGeneratorService(  )
93      {
94      }
95  
96      /**
97       * Creates the XML string
98       *
99       * @param plugin the plugin
100      * @return String the XML content of the admin user list
101      */
102     public static String getXml( Plugin plugin )
103     {
104         StringBuffer strXml = new StringBuffer(  );
105         Date date = new Date(  );
106         HashMap attrList = new HashMap(  );
107         // get only active users to export to wsso
108         AdminUserFilter auFilter = new AdminUserFilter();
109 		auFilter.init();
110 		auFilter.setStatus(AdminUser.ACTIVE_CODE);
111 		Collection<AdminUser> userList = AdminUserHome.findUserByFilter(auFilter);
112 		//AdminAuthentication auth = new AdminWssoAuthentication(  );
113         //Collection<AdminWssoUser> userList = auth.getUserList( "", "", "" );
114 
115         //Open AutorisationWsso
116         XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
117 
118         //Open ApplicationWsso
119         attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APPLICATION_WSSO_APP_ID ),
120             AppPropertiesService.getProperty( PROPERTY_APP_ID ) );
121         XmlUtil.beginElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ),
122             attrList );
123         attrList.clear(  );
124 
125         //Add transmissionDate
126         DateFormat dateFormat = new SimpleDateFormat( DATE_FORMAT );
127         attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_TRANSMISSION_DATE_DATE ),
128             dateFormat.format( date ) );
129         XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_TRANSMISSION_DATE ),
130             attrList );
131         attrList.clear(  );
132 
133         //Add responsable
134         attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_APP_RESPONSABLE_MAIL ),
135             AppPropertiesService.getProperty( PROPERTY_APP_RESPONSABLE ) );
136         XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APP_RESPONSABLE ),
137             attrList );
138         attrList.clear(  );
139 
140         //Add list of allowedUser
141         Pattern pattern = Pattern.compile( REGEX_WSSO_ID );
142         for ( AdminUser user : userList )
143         {
144             Matcher matcher = pattern.matcher( user.getAccessCode( ) );
145             if ( matcher.matches( ) )
146             {
147                 attrList.put( AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ATTR_ALLOWED_USER_WSSO_GUID ),
148                 user.getAccessCode(  ) );
149                 XmlUtil.addEmptyElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_ALLOWED_USER ),
150                 attrList );
151                 attrList.clear(  );
152             }
153         }
154 
155         //Close applicationWsso
156         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_APPLICATION_WSSO ) );
157 
158         //Close autorisationWsso
159         XmlUtil.endElement( strXml, AppPropertiesService.getProperty( PROPERTY_XMLFILEFORMAT_AUTORISATION_WSSO ) );
160 
161         return strXml.toString(  );
162     }
163 
164     /**
165      * Create or update the XML file with the getXml content
166      *
167      * @param plugin the plugin
168      */           
169     public static String createXmlFile( Plugin plugin )
170     {
171         FileWriter fileXmlWriter = null;
172 
173         //String buffer for building the response page
174         StringBuffer sbLogs = new StringBuffer(  );
175         String strFileName = AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
176         String strFolderPath = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" );
177 
178         try
179         {
180             // Test if the pushXml directory exist and create it if it doesn't exist
181             if ( !new File( strFolderPath ).exists(  ) )
182             {
183                 File fileFolder = new File( strFolderPath );
184                 fileFolder.mkdir(  );
185             }
186 
187             // Creates a temporary XML file
188             File fileXml = new File( strFolderPath + strFileName );
189             File fileXmlDirectory = new File( strFolderPath );
190             File fileXmlTemp = File.createTempFile( "tmp", null, fileXmlDirectory );
191             fileXmlWriter = new FileWriter( fileXmlTemp );
192             fileXmlWriter.write( getXml( plugin ) );
193             fileXmlWriter.flush(  );
194             fileXmlWriter.close(  );
195 
196             // Deletes the file if the file exists and renames the temporary file into the file
197             removeXmlFile(  );
198             fileXmlTemp.renameTo( fileXml );
199             sbLogs.append( LOG_MESSAGE_OK + strFileName );
200         }
201         catch ( IOException e )
202         {
203             AppLogService.error( e.getMessage(  ), e );
204             sbLogs.append( LOG_MESSAGE_NOK + strFileName );
205         }
206         catch ( NullPointerException e )
207         {
208             AppLogService.error( e.getMessage(  ), e );
209             sbLogs.append( LOG_MESSAGE_NOK + strFileName );
210         }
211 
212         return sbLogs.toString(  );
213     }
214 
215     /**
216      * Delete the XML file on the file system
217      *
218      */
219     public static void removeXmlFile(  )
220     {
221         String strFileXml = AppPathService.getPath( PROPERTY_XML_STORAGE_FOLDER_PATH, "" ) +
222             AppPropertiesService.getProperty( PROPERTY_XML_FILE_NAME );
223         File file = new File( strFileXml );
224 
225         // Deletes the file if the file exists
226         if ( file.exists(  ) )
227         {
228             file.delete(  );
229         }
230     }
231 }