
org.apache.qpid.jms.util.PropertyUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qpid-jms-client Show documentation
Show all versions of qpid-jms-client Show documentation
The core JMS Client implementation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.qpid.jms.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import javax.net.ssl.SSLContext;
/**
* Utilities for properties
*/
public class PropertyUtil {
/**
* Creates a URI from the original URI and the given parameters.
*
* @param originalURI
* The URI whose current parameters are removed and replaced with the given remainder value.
* @param params
* The URI params that should be used to replace the current ones in the target.
*
* @return a new URI that matches the original one but has its query options replaced with
* the given ones.
*
* @throws URISyntaxException if the given URI is invalid.
*/
public static URI replaceQuery(URI originalURI, Map params) throws URISyntaxException {
String s = createQueryString(params);
if (s.length() == 0) {
s = null;
}
return replaceQuery(originalURI, s);
}
/**
* Creates a URI with the given query, removing an previous query value from the given URI.
*
* @param uri
* The source URI whose existing query is replaced with the newly supplied one.
* @param query
* The new URI query string that should be appended to the given URI.
*
* @return a new URI that is a combination of the original URI and the given query string.
*
* @throws URISyntaxException if the given URI is invalid.
*/
public static URI replaceQuery(URI uri, String query) throws URISyntaxException {
String schemeSpecificPart = uri.getRawSchemeSpecificPart();
// strip existing query if any
int questionMark = schemeSpecificPart.lastIndexOf("?");
// make sure question mark is not within parentheses
if (questionMark < schemeSpecificPart.lastIndexOf(")")) {
questionMark = -1;
}
if (questionMark > 0) {
schemeSpecificPart = schemeSpecificPart.substring(0, questionMark);
}
if (query != null && query.length() > 0) {
schemeSpecificPart += "?" + query;
}
return new URI(uri.getScheme(), schemeSpecificPart, uri.getFragment());
}
/**
* Creates a URI with the given query, removing an previous query value from the given URI.
*
* @param uri
* The source URI whose existing query is replaced with the newly supplied one.
*
* @return a new URI that is a combination of the original URI and the given query string.
*
* @throws URISyntaxException if the given URI is invalid.
*/
public static URI eraseQuery(URI uri) throws URISyntaxException {
return replaceQuery(uri, (String) null);
}
/**
* Given a key / value mapping, create and return a URI formatted query string that is valid
* and can be appended to a URI.
*
* @param options
* The Mapping that will create the new Query string.
*
* @return a URI formatted query string.
*
* @throws URISyntaxException if the given URI is invalid.
*/
public static String createQueryString(Map options) throws URISyntaxException {
try {
if (options.size() > 0) {
StringBuffer rc = new StringBuffer();
boolean first = true;
for (Entry entry : options.entrySet()) {
if (first) {
first = false;
} else {
rc.append("&");
}
rc.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
rc.append("=");
rc.append(URLEncoder.encode((String) entry.getValue(), "UTF-8"));
}
return rc.toString();
} else {
return "";
}
} catch (UnsupportedEncodingException e) {
throw (URISyntaxException) new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
}
}
/**
* Get properties from a URI and return them in a new {@code Map} instance.
*
* If the URI is null or the query string of the URI is null an empty Map is returned.
*
* @param uri
* the URI whose parameters are to be parsed.
*
* @return Map
of properties
*
* @throws Exception if an error occurs while parsing the query options.
*/
public static Map parseParameters(URI uri) throws Exception {
if (uri == null || uri.getQuery() == null) {
return Collections.emptyMap();
}
return parseQuery(stripPrefix(uri.getQuery(), "?"));
}
/**
* Parse properties from a named resource -eg. a URI or a simple name e.g.
* {@literal foo?name="fred"&size=2}
*
* @param uri
* the URI whose parameters are to be parsed.
*
* @return Map
of properties
*
* @throws Exception if an error occurs while parsing the query options.
*/
public static Map parseParameters(String uri) throws Exception {
if (uri == null) {
return Collections.emptyMap();
}
return parseQuery(stripUpto(uri, '?'));
}
/**
* Get properties from a URI query string.
*
* @param queryString
* the string value returned from a call to the URI class getQuery method.
*
* @return Map
of properties from the parsed string.
*
* @throws Exception if an error occurs while parsing the query options.
*/
public static Map parseQuery(String queryString) throws Exception {
if (queryString != null && !queryString.isEmpty()) {
Map rc = new HashMap();
String[] parameters = queryString.split("&");
for (int i = 0; i < parameters.length; i++) {
int p = parameters[i].indexOf("=");
if (p >= 0) {
String name = URLDecoder.decode(parameters[i].substring(0, p), "UTF-8");
String value = URLDecoder.decode(parameters[i].substring(p + 1), "UTF-8");
rc.put(name, value);
} else {
rc.put(parameters[i], null);
}
}
return rc;
}
return Collections.emptyMap();
}
/**
* Given a map of properties, filter out only those prefixed with the given value, the
* values filtered are returned in a new Map instance.
*
* @param properties
* The map of properties to filter.
* @param optionPrefix
* The prefix value to use when filtering.
*
* @return a filter map with only values that match the given prefix.
*/
public static Map filterProperties(Map properties, String optionPrefix) {
if (properties == null) {
throw new IllegalArgumentException("The given properties object was null.");
}
HashMap rc = new HashMap(properties.size());
for (Iterator> iter = properties.entrySet().iterator(); iter.hasNext();) {
Entry entry = iter.next();
if (entry.getKey().startsWith(optionPrefix)) {
String name = entry.getKey().substring(optionPrefix.length());
rc.put(name, entry.getValue());
iter.remove();
}
}
return rc;
}
/**
* Enumerate the properties of the target object and add them as additional entries
* to the query string of the given string URI.
*
* @param uri
* The string URI value to append the object properties to.
* @param bean
* The Object whose properties will be added to the target URI.
*
* @return a new String value that is the original URI with the added bean properties.
*
* @throws Exception if an error occurs while enumerating the bean properties.
*/
public static String addPropertiesToURIFromBean(String uri, Object bean) throws Exception {
Map properties = PropertyUtil.getProperties(bean);
return PropertyUtil.addPropertiesToURI(uri, properties);
}
/**
* Enumerate the properties of the target object and add them as additional entries
* to the query string of the given URI.
*
* @param uri
* The URI value to append the object properties to.
* @param properties
* The Object whose properties will be added to the target URI.
*
* @return a new String value that is the original URI with the added bean properties.
*
* @throws Exception if an error occurs while enumerating the bean properties.
*/
public static String addPropertiesToURI(URI uri, Map properties) throws Exception {
return addPropertiesToURI(uri.toString(), properties);
}
/**
* Append the given properties to the query portion of the given URI.
*
* @param uri
* The string URI value to append the object properties to.
* @param properties
* The properties that will be added to the target URI.
*
* @return a new String value that is the original URI with the added properties.
*
* @throws Exception if an error occurs while building the new URI string.
*/
public static String addPropertiesToURI(String uri, Map properties) throws Exception {
String result = uri;
if (uri != null && properties != null) {
StringBuilder base = new StringBuilder(stripBefore(uri, '?'));
Map map = parseParameters(uri);
if (!map.isEmpty()) {
map.putAll(properties);
} else {
map = properties;
}
if (!map.isEmpty()) {
base.append('?');
boolean first = true;
for (Map.Entry entry : map.entrySet()) {
if (!first) {
base.append('&');
}
first = false;
base.append(entry.getKey()).append("=").append(entry.getValue());
}
result = base.toString();
}
}
return result;
}
/**
* Set properties on an object using the provided map. The return value
* indicates if all properties from the given map were set on the target object.
*
* @param target
* the object whose properties are to be set from the map options.
* @param properties
* the properties that should be applied to the given object.
*
* @return true if all values in the properties map were applied to the target object.
*/
public static Map setProperties(Object target, Map properties) {
if (target == null) {
throw new IllegalArgumentException("target object cannot be null");
}
if (properties == null) {
throw new IllegalArgumentException("Given Properties object cannot be null");
}
Map unmatched = new HashMap();
for (Map.Entry entry : properties.entrySet()) {
if (!setProperty(target, entry.getKey(), entry.getValue())) {
unmatched.put(entry.getKey(), entry.getValue());
}
}
return Collections.unmodifiableMap(unmatched);
}
//TODO: common impl for above and below methods.
/**
* Set properties on an object using the provided Properties object. The return value
* indicates if all properties from the given map were set on the target object.
*
* @param target
* the object whose properties are to be set from the map options.
* @param properties
* the properties that should be applied to the given object.
*
* @return an unmodifiable map with any values that could not be applied to the target.
*/
public static Map setProperties(Object target, Properties properties) {
if (target == null) {
throw new IllegalArgumentException("target object cannot be null");
}
if (properties == null) {
throw new IllegalArgumentException("Given Properties object cannot be null");
}
Map unmatched = new HashMap();
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy