Coverage Report - fr.paris.lutece.plugins.releaser.util.svn.SvnSiteService
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnSiteService
39 %
15/38
25 %
2/8
2,75
 
 1  
 /*
 2  
  * Copyright (c) 2002-2015, 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  
 
 35  
 
 36  
 package fr.paris.lutece.plugins.releaser.util.svn;
 37  
 
 38  
 import fr.paris.lutece.plugins.releaser.business.ReleaserUser;
 39  
 import fr.paris.lutece.plugins.releaser.util.ReleaserUtils;
 40  
 import fr.paris.lutece.portal.service.util.AppLogService;
 41  
 import fr.paris.lutece.portal.service.util.AppPropertiesService;
 42  
 import fr.paris.lutece.util.httpaccess.HttpAccess;
 43  
 import fr.paris.lutece.util.httpaccess.HttpAccessException;
 44  
 import fr.paris.lutece.util.signrequest.BasicAuthorizationAuthenticator;
 45  
 import fr.paris.lutece.util.signrequest.RequestAuthenticator;
 46  
 import java.util.ArrayList;
 47  
 import java.util.List;
 48  
 import java.util.Locale;
 49  
 
 50  
 import javax.servlet.http.HttpServletRequest;
 51  
 
 52  
 /**
 53  
  * SvnSiteService
 54  
  */
 55  0
 public class SvnSiteService 
 56  
 {
 57  
     private static final String PROPERTY_SITE_REPOSITORY_LOGIN = "releaser.site.repository.login";
 58  
     private static final String PROPERTY_SITE_REPOSITORY_PASSWORD = "releaser.site.repository.password";
 59  
 
 60  
     /**
 61  
      * Fetch the pom.xml content from a repository
 62  
      * 
 63  
      * @param strPomUrl
 64  
      *            The POM URL
 65  
      * @return The POM content
 66  
      */
 67  
     public static String fetchPom( String strPomUrl,HttpServletRequest request,Locale locale )
 68  
     {
 69  
         try
 70  
         {
 71  0
             RequestAuthenticator authenticator = getSiteAuthenticator( request,locale);
 72  0
             HttpAccess httpAccess = new HttpAccess( );
 73  0
             return httpAccess.doGet( strPomUrl, authenticator, null );
 74  
         }
 75  0
         catch( HttpAccessException ex )
 76  
         {
 77  0
             AppLogService.error( "Error fecthing pom.xml content : " + ex.getMessage( ), ex );
 78  
         }
 79  0
         return null;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Gets the last release found in the SVN repository
 84  
      * @param strSiteArtifactId The site artifact id
 85  
      * @param strTrunkUrl The trunk URL
 86  
      * @return The version if found otherwise null
 87  
      */
 88  
     public static String getLastRelease( String strSiteArtifactId , String strTrunkUrl,HttpServletRequest request,Locale locale )
 89  
     {
 90  1
         String strTagsUrl = strTrunkUrl.replace( "trunk", "tags" );
 91  
 
 92  1
         List<String> list = new ArrayList<>( );
 93  
 
 94  
         try
 95  
         {
 96  1
             RequestAuthenticator authenticator = getSiteAuthenticator(request,locale );
 97  1
             HttpAccess httpAccess = new HttpAccess( );
 98  1
             String strHtml = httpAccess.doGet( strTagsUrl , authenticator , null );
 99  0
             list = getAnchorsList( strHtml , strSiteArtifactId );
 100  
         }
 101  1
         catch( HttpAccessException e )
 102  
         {
 103  1
             AppLogService.error( "SvnSiteService : Error retrieving release version : " + e.getMessage( ), e );
 104  0
         }
 105  
 
 106  1
         return ( !list.isEmpty() ) ? list.get( list.size() - 1 ) : null;
 107  
         
 108  
     }
 109  
     
 110  
     /**
 111  
      * Gets anchor list using more optimized method
 112  
      *
 113  
      * @param strHtml
 114  
      *            The HTML code
 115  
      * @return The list
 116  
      */
 117  
     private static List<String> getAnchorsList( String strHtml , String strPrefix )
 118  
     {
 119  0
         List<String> list = new ArrayList<String>( );
 120  0
         String strCurrent = strHtml;
 121  
 
 122  0
         int nPos = strCurrent.indexOf( "<a " );
 123  
 
 124  0
         while ( nPos > 0 )
 125  
         {
 126  0
             strCurrent = strCurrent.substring( nPos );
 127  
 
 128  0
             int nEndTag = strCurrent.indexOf( '>' );
 129  0
             int nTagEnd = strCurrent.indexOf( "</a>" );
 130  0
             String strTag = strCurrent.substring( nEndTag + 1, nTagEnd ).replaceAll( "\\/", "" );
 131  0
             if( strTag.startsWith( strPrefix ))
 132  
             {
 133  0
                 list.add( strTag );
 134  
             }
 135  0
             strCurrent = strCurrent.substring( nTagEnd + 4 );
 136  0
             nPos = strCurrent.indexOf( "<a " );
 137  0
         }
 138  
 
 139  0
         return list;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Build an authenticathor to access the site repository
 144  
      * 
 145  
      * @return The authenticator
 146  
      */
 147  
     private static RequestAuthenticator getSiteAuthenticator( HttpServletRequest request,Locale locale)
 148  
     {
 149  1
         ReleaserUser user=ReleaserUtils.getReleaserUser( request, locale );
 150  1
         String strLogin=null;
 151  1
         String strPassword=null;
 152  
         
 153  1
         if(user!=null)
 154  
         {
 155  1
              strLogin=user.getSvnSiteAccountLogin( );
 156  1
             strPassword=user.getSvnSiteAccountPassword( );
 157  
         }
 158  1
         return new BasicAuthorizationAuthenticator( strLogin, strPassword );
 159  
     }
 160  
     
 161  
 }