View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.webappcontainer.util;
35  
36  import java.io.IOException;
37  import java.net.MalformedURLException;
38  import java.net.URL;
39  
40  import sun.misc.BASE64Decoder;
41  import sun.misc.BASE64Encoder;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  
44  /**
45   * This class provide tools for Urls
46   * @author ELY
47   * 
48   */
49  public final class UrlUtils
50  {
51  	/**
52  	 * Private constructor - this class does not need to be instantiated
53  	 */
54  	private UrlUtils()
55  	{
56  	}
57  
58  	/**
59  	 * Check if the host of the two url are the same
60  	 * @param strFirstUrl The first url
61  	 * @param strSecondUrl The second url
62  	 * @return true if the host are equals, false else or if an url is malformed (MalformedURLException).
63  	 */
64  	public static boolean hostsEquals( String strFirstUrl, String strSecondUrl )
65  	{
66  		URL urlFirstUrl = null;
67  		URL urlSecondUrl = null;
68  
69  		try
70  		{
71  			urlFirstUrl = new URL( strFirstUrl );
72  			urlSecondUrl = new URL( strSecondUrl );
73  		}
74  		catch ( MalformedURLException e )
75  		{
76  			return false;
77  		}
78  
79  		return urlFirstUrl.getHost().equalsIgnoreCase( urlSecondUrl.getHost() );
80  	}
81  
82  	/**
83  	 * Convert the specified url
84  	 * @param strUrl The url to convert
85  	 * @param strBaseUrlSite The webapp url
86  	 * @return The converted url
87  	 */
88  	public static String convertRelativeToAbsoluteUrl( String strUrl, String strBaseUrlSite )
89  	{
90  		String strConvertedUrl = null;
91  		URL baseUrlSite = null;
92  		AppLogService.debug( "[ConvertUrl] Webapp url = " + strBaseUrlSite );
93  
94  		try
95  		{
96  			baseUrlSite = new URL( strBaseUrlSite );
97  
98  			URL convertedUrl = new URL( baseUrlSite, strUrl );
99  
100 			strConvertedUrl = convertedUrl.toExternalForm();
101 		}
102 		catch ( MalformedURLException e )
103 		{
104 			AppLogService.error( "[convertUrl] Malformed Url Exception with url = " + strUrl, e );
105 
106 			return null;
107 		}
108 
109 		AppLogService.debug( "[ConvertUrl] " + strUrl + "   ->   " + strConvertedUrl );
110 
111 		return strConvertedUrl;
112 	}
113 
114 	/**
115 	 * Encode (Base64) the specified Url
116 	 * @param strUrl The url to encode
117 	 * @return The encoded url
118 	 */
119 	public static String encodeUrl( String strUrl )
120 	{
121 		BASE64Encoder encoder = new BASE64Encoder();
122 
123 		return encoder.encode( strUrl.getBytes() );
124 	}
125 
126 	/**
127 	 * Decode (Base64) the specified url
128 	 * 
129 	 * @param strUrl the url to decode
130 	 * @return The decoded url or null if error (IOException)
131 	 */
132 	public static String decodeUrl( String strUrl )
133 	{
134 		BASE64Decoder decoder = new BASE64Decoder();
135 		String strReturnUrl = null;
136 
137 		try
138 		{
139 			if ( strUrl != null )
140 			{
141 				strReturnUrl = new String( decoder.decodeBuffer( strUrl ) );
142 			}
143 		}
144 		catch ( IOException e )
145 		{
146 			return null;
147 		}
148 
149 		return strReturnUrl;
150 	}
151 }