Skip to content

Instantly share code, notes, and snippets.

@bohnman
Created November 14, 2012 19:48
Show Gist options
  • Save bohnman/4074310 to your computer and use it in GitHub Desktop.
Save bohnman/4074310 to your computer and use it in GitHub Desktop.
Wraps Spring's UriUtils with the assumption that we're using UTF-8
import org.springframework.web.util.UriUtils;
import java.io.UnsupportedEncodingException;
public class UriUtil {
private static final String ENCODING = "UTF-8";
private UriUtil() {
}
public static String encodeScheme(String scheme) {
try {
return UriUtils.encodeScheme(scheme, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeAuthority(String authority) {
try {
return UriUtils.encodeAuthority(authority, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeUserInfo(String userInfo) {
try {
return UriUtils.encodeUserInfo(userInfo, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeHost(String host) {
try {
return UriUtils.encodeHost(host, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodePort(String port) {
try {
return UriUtils.encodePort(port, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodePath(String path) {
try {
return UriUtils.encodePath(path, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodePathSegment(String segment) {
try {
return UriUtils.encodePathSegment(segment, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeQuery(String query) {
try {
return UriUtils.encodeQuery(query, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeQueryParam(String queryParam) {
try {
return UriUtils.encodeQueryParam(queryParam, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String encodeFragment(String fragment) {
try {
return UriUtils.encodeFragment(fragment, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String decode(String source) {
try {
return UriUtils.decode(source, ENCODING);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment