下面列出了怎么用org.apache.commons.httpclient.util.URIUtil的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public CloudBlobContainerWrapper getContainerReference(String name)
throws URISyntaxException, StorageException {
String fullUri;
try {
fullUri = baseUriString + "/" + URIUtil.encodePath(name);
} catch (URIException e) {
throw new RuntimeException("problem encoding fullUri", e);
}
MockCloudBlobContainerWrapper container = new MockCloudBlobContainerWrapper(
fullUri, name);
// Check if we have a pre-existing container with that name, and prime
// the wrapper with that knowledge if it's found.
for (PreExistingContainer existing : preExistingContainers) {
if (fullUri.equalsIgnoreCase(existing.containerUri)) {
// We have a pre-existing container. Mark the wrapper as created and
// make sure we use the metadata for it.
container.created = true;
backingStore.setContainerMetadata(existing.containerMetadata);
break;
}
}
return container;
}
private String fullUriString(String relativePath, boolean withTrailingSlash) {
String fullUri;
String baseUri = this.baseUri;
if (!baseUri.endsWith("/")) {
baseUri += "/";
}
if (withTrailingSlash && !relativePath.equals("")
&& !relativePath.endsWith("/")) {
relativePath += "/";
}
try {
fullUri = baseUri + URIUtil.encodePath(relativePath);
} catch (URIException e) {
throw new RuntimeException("problem encoding fullUri", e);
}
return fullUri;
}
@Override
public CloudBlobContainerWrapper getContainerReference(String name)
throws URISyntaxException, StorageException {
String fullUri;
try {
fullUri = baseUriString + "/" + URIUtil.encodePath(name);
} catch (URIException e) {
throw new RuntimeException("problem encoding fullUri", e);
}
MockCloudBlobContainerWrapper container = new MockCloudBlobContainerWrapper(
fullUri, name);
// Check if we have a pre-existing container with that name, and prime
// the wrapper with that knowledge if it's found.
for (PreExistingContainer existing : preExistingContainers) {
if (fullUri.equalsIgnoreCase(existing.containerUri)) {
// We have a pre-existing container. Mark the wrapper as created and
// make sure we use the metadata for it.
container.created = true;
backingStore.setContainerMetadata(existing.containerMetadata);
break;
}
}
return container;
}
private String fullUriString(String relativePath, boolean withTrailingSlash) {
String fullUri;
String baseUri = this.baseUri;
if (!baseUri.endsWith("/")) {
baseUri += "/";
}
if (withTrailingSlash && !relativePath.equals("")
&& !relativePath.endsWith("/")) {
relativePath += "/";
}
try {
fullUri = baseUri + URIUtil.encodePath(relativePath);
} catch (URIException e) {
throw new RuntimeException("problem encoding fullUri", e);
}
return fullUri;
}
/**
* Convert D2 URL template into a string used for throttling limiter
*
* Valid:
* d2://host/${resource-id}
*
* Invalid:
* d2://host${resource-id}, because we cannot differentiate the host
*/
public static String createR2ClientLimiterKey(Config config) {
String urlTemplate = config.getString(HttpConstants.URL_TEMPLATE);
try {
String escaped = URIUtil.encodeQuery(urlTemplate);
URI uri = new URI(escaped);
if (uri.getHost() == null)
throw new RuntimeException("Cannot get host part from uri" + urlTemplate);
String key = uri.getScheme() + "/" + uri.getHost();
if (uri.getPort() > 0) {
key = key + "/" + uri.getPort();
}
log.info("Get limiter key [" + key + "]");
return key;
} catch (Exception e) {
throw new RuntimeException("Cannot create R2 limiter key", e);
}
}
/**
* Gets the path encoded suitable for url like file system e.g. (http, webdav).
*
* @param charset the charset used for the path encoding
* @return The encoded path.
* @throws URIException If an error occurs encoding the URI.
* @throws FileSystemException If some other error occurs.
*/
public String getPathQueryEncoded(final String charset) throws URIException, FileSystemException {
if (getQueryString() == null) {
if (charset != null) {
return URIUtil.encodePath(getPathDecoded(), charset);
}
return URIUtil.encodePath(getPathDecoded());
}
final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
if (charset != null) {
sb.append(URIUtil.encodePath(getPathDecoded(), charset));
} else {
sb.append(URIUtil.encodePath(getPathDecoded()));
}
sb.append("?");
sb.append(getQueryString());
return sb.toString();
}
private String buildQueryString() {
final StringBuilder urlBuilder = new StringBuilder(RESOURCE_URL);
try {
urlBuilder.append("?code=");
urlBuilder.append(this.pinCode);
urlBuilder.append("&client_id=");
urlBuilder.append(this.clientId);
urlBuilder.append("&client_secret=");
urlBuilder.append(this.clientSecret);
urlBuilder.append("&grant_type=authorization_code");
return URIUtil.encodeQuery(urlBuilder.toString());
} catch (final Exception e) {
throw new NestException(e);
}
}
@SuppressWarnings("unchecked")
public static List<NameValuePair> getAddressPairs(String address) {
try {
String query = URIUtil.getQuery(address);
List<NameValuePair> params = new ParameterParser().parse(query, '&');
List<NameValuePair> res = new ArrayList<NameValuePair>();
for (NameValuePair nvp : params) {
res.add(new NameValuePair(URIUtil.decode(nvp.getName(), DEFAULT_CHARSET), URIUtil.decode(nvp.getValue(), DEFAULT_CHARSET)));
}
return res;
} catch (URIException e) {
throw new SpagoBIRuntimeException(e);
}
}
/**
* Utility function used to convert a given URI to a decoded string
* representation sent to the backing store. URIs coming as input
* to this class will be encoded by the URI class, and we want
* the underlying storage to store keys in their original UTF-8 form.
*/
private static String convertUriToDecodedString(URI uri) {
try {
String result = URIUtil.decode(uri.toString());
return result;
} catch (URIException e) {
throw new AssertionError("Failed to decode URI: " + uri.toString());
}
}
/**
* Escape and encode a string regarded as within the query component of an URI.
* @param value the value to encode
* @return encoded query, null if the default charset is not supported
*/
public static String encodeQueryValue(final String value) {
try {
return URIUtil.encodeWithinQuery(value, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Escape and encode a string regarded as the path component of an URI.
* @param path the path component to encode
* @return encoded path, null if UTF-8 is not supported
*/
public static String encodePath(final String path) {
try {
return URIUtil.encodePath(path, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Parse and decode the path component from the given request.
* @param request Http request to parse
* @param servletName the name of servlet that precedes the path
* @return decoded path component, null if UTF-8 is not supported
*/
public static String getDecodedPath(final HttpServletRequest request, String servletName) {
try {
return URIUtil.decode(getRawPath(request, servletName), "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Utility function used to convert a given URI to a decoded string
* representation sent to the backing store. URIs coming as input
* to this class will be encoded by the URI class, and we want
* the underlying storage to store keys in their original UTF-8 form.
*/
private static String convertUriToDecodedString(URI uri) {
try {
String result = URIUtil.decode(uri.toString());
return result;
} catch (URIException e) {
throw new AssertionError("Failed to decode URI: " + uri.toString());
}
}
/**
* Escape and encode a string regarded as within the query component of an URI.
* @param value the value to encode
* @return encoded query, null if the default charset is not supported
*/
public static String encodeQueryValue(final String value) {
try {
return URIUtil.encodeWithinQuery(value, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Escape and encode a string regarded as the path component of an URI.
* @param path the path component to encode
* @return encoded path, null if UTF-8 is not supported
*/
public static String encodePath(final String path) {
try {
return URIUtil.encodePath(path, "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Parse and decode the path component from the given request.
* @param request Http request to parse
* @param servletName the name of servlet that precedes the path
* @return decoded path component, null if UTF-8 is not supported
*/
public static String getDecodedPath(final HttpServletRequest request, String servletName) {
try {
return URIUtil.decode(getRawPath(request, servletName), "UTF-8");
} catch (URIException e) {
throw new AssertionError("JVM does not support UTF-8"); // should never happen!
}
}
/**
* Construct a KafkaDestination by parsing URI to extract the relevant fields
* @param uri Kafka destination URI
* @return KafkaDestination created by parsing the URI string
*/
public static KafkaDestination parse(String uri) {
Validate.isTrue(uri.startsWith(SCHEME_KAFKA) || uri.startsWith(SCHEME_SECURE_KAFKA),
"Invalid scheme in URI: " + uri);
try {
// Decode URI in case it's escaped
uri = URIUtil.decode(uri);
} catch (Exception e) {
throw new DatastreamRuntimeException("Failed to decode Kafka destination URI: " + uri, e);
}
URI u = URI.create(uri);
String scheme = u.getScheme();
String zkAddress = u.getAuthority();
String path = u.getPath();
String topicName;
int lastSlash = path.lastIndexOf("/");
if (lastSlash > 0) {
// intermediate paths are part of ZK address
zkAddress += path.substring(0, lastSlash);
topicName = path.substring(lastSlash + 1);
} else {
topicName = path.substring(1);
}
for (String hostInfo : zkAddress.split(",")) {
long portNum = URI.create(SCHEME_KAFKA + "://" + hostInfo).getPort();
Validate.isTrue(portNum != -1, "Missing port number in URI: " + uri);
}
Validate.notBlank(zkAddress, "Missing zkAddress in URI: " + uri);
Validate.notBlank(topicName, "Missing topic name in URI: " + uri);
boolean isSecure = scheme.equals(SCHEME_SECURE_KAFKA);
return new KafkaDestination(zkAddress, topicName, isSecure);
}
@Test
public void testEscapedUri() throws Exception {
String uri = "kafka://localhost%3A12913%2Fkafka-datastream/testtopic_test";
KafkaDestination destination = KafkaDestination.parse(uri);
Assert.assertEquals(destination.getZkAddress(), "localhost:12913/kafka-datastream");
Assert.assertEquals(destination.getTopicName(), "testtopic_test");
uri = URIUtil.encodeWithinAuthority("kafka://localhost:12913/kafka-datastream/testtopic_test");
destination = KafkaDestination.parse(uri);
Assert.assertEquals(destination.getZkAddress(), "localhost:12913/kafka-datastream");
Assert.assertEquals(destination.getTopicName(), "testtopic_test");
}
/**
* Construct a HTTPS URL from given components.
*
* Note: The <code>userinfo</code> format is normally
* <code><username>:<password></code> where
* username and password must both be URL escaped.
*
* @param userinfo the userinfo string whose parts are URL escaped
* @param host the host string
* @param port the port number
* @param path the path string
* @param query the query string
* @param fragment the fragment string
* @throws URIException If {@link #checkValid()} fails
* @see #getDefaultProtocolCharset
*/
public HttpsURL(String userinfo, String host, int port, String path,
String query, String fragment) throws URIException {
// validate and contruct the URI character sequence
StringBuffer buff = new StringBuffer();
if (userinfo != null || host != null || port != -1) {
_scheme = DEFAULT_SCHEME; // in order to verify the own protocol
buff.append(_default_scheme);
buff.append("://");
if (userinfo != null) {
buff.append(userinfo);
buff.append('@');
}
if (host != null) {
buff.append(URIUtil.encode(host, URI.allowed_host));
if (port != -1 || port != DEFAULT_PORT) {
buff.append(':');
buff.append(port);
}
}
}
if (path != null) { // accept empty path
if (scheme != null && !path.startsWith("/")) {
throw new URIException(URIException.PARSING,
"abs_path requested");
}
buff.append(URIUtil.encode(path, URI.allowed_abs_path));
}
if (query != null) {
buff.append('?');
buff.append(URIUtil.encode(query, URI.allowed_query));
}
if (fragment != null) {
buff.append('#');
buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
}
parseUriReference(buff.toString(), true);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* Note: The <code>userinfo</code> format is normally
* <code><username>:<password></code> where
* username and password must both be URL escaped.
*
* @param userinfo the userinfo string whose parts are URL escaped
* @param host the host string
* @param port the port number
* @param path the path string
* @param query the query string
* @param fragment the fragment string
* @throws URIException If {@link #checkValid()} fails
* @see #getDefaultProtocolCharset
*/
public HttpURL(String userinfo, String host, int port, String path,
String query, String fragment) throws URIException {
// validate and contruct the URI character sequence
StringBuffer buff = new StringBuffer();
if (userinfo != null || host != null || port != -1) {
_scheme = DEFAULT_SCHEME; // in order to verify the own protocol
buff.append(_default_scheme);
buff.append("://");
if (userinfo != null) {
buff.append(userinfo);
buff.append('@');
}
if (host != null) {
buff.append(URIUtil.encode(host, URI.allowed_host));
if (port != -1 || port != DEFAULT_PORT) {
buff.append(':');
buff.append(port);
}
}
}
if (path != null) { // accept empty path
if (scheme != null && !path.startsWith("/")) {
throw new URIException(URIException.PARSING,
"abs_path requested");
}
buff.append(URIUtil.encode(path, URI.allowed_abs_path));
}
if (query != null) {
buff.append('?');
buff.append(URIUtil.encode(query, URI.allowed_query));
}
if (fragment != null) {
buff.append('#');
buff.append(URIUtil.encode(fragment, URI.allowed_fragment));
}
parseUriReference(buff.toString(), true);
checkValid();
}
protected static String toUserinfo(String user, String password) throws URIException {
if (user == null) return null;
StringBuffer usrinfo = new StringBuffer(20); //sufficient for real world
usrinfo.append(URIUtil.encode(user, URI.allowed_within_userinfo));
if (password == null) return usrinfo.toString();
usrinfo.append(':');
usrinfo.append(URIUtil.encode(password, URI.allowed_within_userinfo));
return usrinfo.toString();
}
private String getEncodedUrl(String url) {
try {
return URIUtil.encodeQuery(URIUtil.decode(url));
} catch (URIException ue) {
LOGGER.warn("URIException on " + url);
return url;
}
}
@Override
public String execute(Licence licence) {
String trackingUrl = licence.getProduct().getUrl();
if (StringUtils.isBlank(trackingUrl)) {
trackingUrl = defaultUrl();
}
if (StringUtils.contains(trackingUrl, "?")) {
trackingUrl = trackingUrl + "&";
} else {
trackingUrl = trackingUrl + "?";
}
trackingUrl = trackingUrl + additionalUrlMetadata(licence);
try {
trackingUrl = URIUtil.encodeQuery(trackingUrl);
} catch (URIException e1) {
}
String callToAction = licence.getProduct().getUrlCallToAction();
if (StringUtils.isBlank(callToAction)) {
callToAction = defaultCallToAction();
}
try {
Url url = as(username, apiKey).call(shorten(trackingUrl));
return callToAction + url.getShortUrl();
} catch (Exception e) {
logger.error("Unable to bit.ly tracking url for licence " + licence.getId() , e);
return defaultTrackingUrl();
}
}
@Override
public String execute(Product product) {
String trackingUrl = product.getUrl();
if (StringUtils.isBlank(trackingUrl)) {
trackingUrl = defaultUrl();
}
if (StringUtils.contains(trackingUrl, "?")) {
trackingUrl = trackingUrl + "&";
} else {
trackingUrl = trackingUrl + "?";
}
trackingUrl = trackingUrl + additionalUrlMetadata(product);
try {
trackingUrl = URIUtil.encodeQuery(trackingUrl);
} catch (URIException e1) {
}
String callToAction = product.getUrlCallToAction();
if (StringUtils.isBlank(callToAction)) {
callToAction = defaultCallToAction();
}
try {
Url url = as(username, apiKey).call(shorten(trackingUrl));
return callToAction + url.getShortUrl();
} catch (Exception e) {
logger.error("Unable to bit.ly tracking url for product " + product.getId() , e);
return defaultTrackingUrl();
}
}
/**
* Splits the given string on the first '?' then encodes the first half as a path (ignoring slashes and colons)
* and the second half as a query segment (ignoring questionmarks, equals signs, etc...).
*
* @deprecated Prefer to use {@link HttpEncodingTools#encode(String)} instead for encoding specific
* pieces of the URI. This method does not escape certain reserved characters, like '/', ':', '=', and '?'.
* As such, this is not safe to use on URIs that may contain these reserved characters in the wrong places.
*/
@Deprecated
public static String encodeUri(String uri) {
try {
return URIUtil.encodePathQuery(uri);
} catch (URIException ex) {
throw new EsHadoopIllegalArgumentException("Cannot escape uri [" + uri + "]", ex);
}
}
/**
* Encodes characters in the string except for those allowed in an absolute path.
*
* @deprecated Prefer to use {@link HttpEncodingTools#encode(String)} instead for encoding specific
* pieces of the URI. This method does not escape certain reserved characters, like '/' and ':'.
* As such, this is not safe to use on paths that may contain these reserved characters in the wrong places.
*/
@Deprecated
public static String encodePath(String path) {
try {
return URIUtil.encodePath(path, "UTF-8");
} catch (URIException ex) {
throw new EsHadoopIllegalArgumentException("Cannot encode path segment [" + path + "]", ex);
}
}
public String getHarDetailsURL(final String harName) {
try {
return URIUtil.encodeQuery(targetURL + "/details?label=" + harName);
} catch (URIException e) {
return null;
}
}
private String buildQueryString() {
final StringBuilder urlBuilder = new StringBuilder(RESOURCE_URL);
try {
urlBuilder.append("?auth=");
urlBuilder.append(this.accessToken);
return URIUtil.encodeQuery(urlBuilder.toString());
} catch (final Exception e) {
throw new NestException(e);
}
}
private String buildQueryString() {
final StringBuilder urlBuilder = new StringBuilder(RESOURCE_URL);
try {
urlBuilder.append("?auth=");
urlBuilder.append(this.accessToken);
return URIUtil.encodeQuery(urlBuilder.toString());
} catch (final Exception e) {
throw new NestException(e);
}
}
private String buildQueryString() {
final StringBuilder urlBuilder = new StringBuilder(RESOURCE_URL);
try {
urlBuilder.append("?grant_type=ecobeePin");
urlBuilder.append("&code=");
urlBuilder.append(authToken);
urlBuilder.append("&client_id=");
urlBuilder.append(appKey);
return URIUtil.encodeQuery(urlBuilder.toString());
} catch (final Exception e) {
throw new EcobeeException(e);
}
}