1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.internal

File PathUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
15
4
1
100
45
8
0.53
3.75
4
2

Classes

Class Line # Actions
PathUtils 38 15 0% 8 6
0.7391304473.9%
 

Contributing tests

This file is covered by 74 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.extension.internal;
21   
22    import java.io.IOException;
23    import java.io.UnsupportedEncodingException;
24    import java.net.JarURLConnection;
25    import java.net.MalformedURLException;
26    import java.net.URL;
27    import java.net.URLConnection;
28    import java.net.URLEncoder;
29   
30    import org.apache.commons.lang3.StringUtils;
31   
32    /**
33    * Various path utilities.
34    *
35    * @version $Id: 0ce143468d3afd84d6f269c58f3423190a45dbe6 $
36    * @since 6.4M1
37    */
 
38    public final class PathUtils
39    {
 
40  0 toggle private PathUtils()
41    {
42    // Utility class
43    }
44   
45    /**
46    * Protect passed String to work with as much filesystems as possible.
47    *
48    * @param str the file or directory name to encode
49    * @return the encoded name
50    * @since 7.1RC1
51    */
 
52  3562 toggle public static String encode(String str)
53    {
54  3562 String encoded;
55  3562 try {
56  3562 encoded = URLEncoder.encode(str, "UTF-8").replace(".", "%2E").replace("*", "%2A");
57    } catch (UnsupportedEncodingException e) {
58    // Should never happen
59   
60  0 encoded = str;
61    }
62   
63  3562 return encoded;
64    }
65   
66    // Workaround insane WebSphere bug (see http://jira.xwiki.org/browse/XWIKI-13898)
 
67  36017 toggle private static URL fixURL(URL jarURL)
68    {
69  36017 String jarURLString = jarURL.toExternalForm();
70  36017 if (StringUtils.contains(jarURLString, ' ')) {
71  0 jarURLString = jarURLString.replace(" ", "%20");
72   
73  0 try {
74  0 return new URL(jarURLString);
75    } catch (MalformedURLException e) {
76    // TODO: Log something ?
77    }
78    }
79   
80  36017 return jarURL;
81    }
82   
83    /**
84    * @param descriptorURL the URL to the core extension descriptor
85    * @return the URL to the core extension file
86    * @throws IOException when failing to access passed URL
87    * @since 8.4RC1
88    * @since 7.4.6
89    */
 
90  36103 toggle public static URL getExtensionURL(URL descriptorURL) throws IOException
91    {
92  36103 URLConnection connection = descriptorURL.openConnection();
93   
94  36103 if (connection instanceof JarURLConnection) {
95  36017 return fixURL(((JarURLConnection) connection).getJarFileURL());
96    }
97   
98  86 return descriptorURL;
99    }
100    }