Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / URIEncoder.java
blobbaf75275f91419bcce68e19f54c64a1e0a95c599
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2012 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to:
18 * dkocher@cyberduck.ch
21 import org.apache.commons.lang3.StringUtils;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URLEncoder;
25 import java.util.StringTokenizer;
27 /**
28 * @version $Id$
30 public final class URIEncoder {
32 private URIEncoder() {
36 /**
37 * URL encode a path
39 * @param p Path
40 * @return URI encoded
41 * @see java.net.URLEncoder#encode(String, String)
43 public static String encode(final String p) {
44 try {
45 final StringBuilder b = new StringBuilder();
46 final StringTokenizer t = new StringTokenizer(p, "/");
47 if(!t.hasMoreTokens()) {
48 return p;
50 if(StringUtils.startsWith(p, String.valueOf(Path.DELIMITER))) {
51 b.append(Path.DELIMITER);
53 while(t.hasMoreTokens()) {
54 b.append(URLEncoder.encode(t.nextToken(), "UTF-8"));
55 if(t.hasMoreTokens()) {
56 b.append(Path.DELIMITER);
59 if(StringUtils.endsWith(p, String.valueOf(Path.DELIMITER))) {
60 b.append(Path.DELIMITER);
62 // Becuase URLEncoder uses <code>application/x-www-form-urlencoded</code> we have to replace these
63 // for proper URI percented encoding.
64 return b.toString().replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
66 catch(UnsupportedEncodingException e) {
67 return p;