|
26 | 26 | import java.net.URISyntaxException; |
27 | 27 | import java.net.URLEncoder; |
28 | 28 | import java.net.UnknownHostException; |
| 29 | +import java.util.ArrayList; |
29 | 30 | import java.util.List; |
| 31 | +import java.util.ListIterator; |
| 32 | +import java.util.StringTokenizer; |
30 | 33 |
|
31 | 34 | import javax.net.ssl.HttpsURLConnection; |
32 | 35 |
|
|
37 | 40 | import org.apache.commons.httpclient.UsernamePasswordCredentials; |
38 | 41 | import org.apache.commons.httpclient.auth.AuthScope; |
39 | 42 | import org.apache.commons.httpclient.methods.GetMethod; |
| 43 | +import org.apache.commons.httpclient.util.URIUtil; |
40 | 44 | import org.apache.http.NameValuePair; |
| 45 | +import org.apache.http.message.BasicNameValuePair; |
| 46 | +import org.apache.http.client.utils.URIBuilder; |
41 | 47 | import org.apache.http.client.utils.URLEncodedUtils; |
42 | 48 | import org.apache.log4j.Logger; |
43 | 49 |
|
| 50 | +import com.cloud.utils.crypt.DBEncryptionUtil; |
44 | 51 | import com.cloud.utils.exception.CloudRuntimeException; |
45 | 52 |
|
46 | 53 | public class UriUtils { |
@@ -138,6 +145,59 @@ public static boolean cifsCredentialsPresent(URI uri) { |
138 | 145 | return (foundUser && foundPswd); |
139 | 146 | } |
140 | 147 |
|
| 148 | + public static String getUpdateUri(String url, boolean encrypt) { |
| 149 | + String updatedPath = null; |
| 150 | + try { |
| 151 | + String query = URIUtil.getQuery(url); |
| 152 | + URIBuilder builder = new URIBuilder(url); |
| 153 | + builder.removeQuery(); |
| 154 | + |
| 155 | + String updatedQuery = new String(); |
| 156 | + List<NameValuePair> queryParams = getUserDetails(query); |
| 157 | + ListIterator<NameValuePair> iterator = queryParams.listIterator(); |
| 158 | + while (iterator.hasNext()) { |
| 159 | + NameValuePair param = iterator.next(); |
| 160 | + String value = null; |
| 161 | + if ("password".equalsIgnoreCase(param.getName()) && |
| 162 | + param.getValue() != null) { |
| 163 | + value = encrypt ? DBEncryptionUtil.encrypt(param.getValue()) : DBEncryptionUtil.decrypt(param.getValue()); |
| 164 | + } else { |
| 165 | + value = param.getValue(); |
| 166 | + } |
| 167 | + |
| 168 | + if (updatedQuery.isEmpty()) { |
| 169 | + updatedQuery += (param.getName() + "=" + value); |
| 170 | + } else { |
| 171 | + updatedQuery += ("&" + param.getName() + "=" + value); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + String schemeAndHost = new String(); |
| 176 | + URI newUri = builder.build(); |
| 177 | + if (newUri.getScheme() != null) { |
| 178 | + schemeAndHost = newUri.getScheme() + "://" + newUri.getHost(); |
| 179 | + } |
| 180 | + |
| 181 | + updatedPath = schemeAndHost + newUri.getPath() + "?" + updatedQuery; |
| 182 | + } catch (URISyntaxException e) { |
| 183 | + throw new CloudRuntimeException("Couldn't generate an updated uri. " + e.getMessage()); |
| 184 | + } |
| 185 | + |
| 186 | + return updatedPath; |
| 187 | + } |
| 188 | + |
| 189 | + private static List<NameValuePair> getUserDetails(String query) { |
| 190 | + List<NameValuePair> details = new ArrayList<NameValuePair>(); |
| 191 | + StringTokenizer allParams = new StringTokenizer(query, "&"); |
| 192 | + while (allParams.hasMoreTokens()) { |
| 193 | + String param = allParams.nextToken(); |
| 194 | + details.add(new BasicNameValuePair(param.substring(0, param.indexOf("=")), |
| 195 | + param.substring(param.indexOf("=") + 1))); |
| 196 | + } |
| 197 | + |
| 198 | + return details; |
| 199 | + } |
| 200 | + |
141 | 201 | // Get the size of a file from URL response header. |
142 | 202 | public static Long getRemoteSize(String url) { |
143 | 203 | Long remoteSize = (long)0; |
|
0 commit comments