mirror of https://github.com/apache/jclouds.git
added json escape utility
This commit is contained in:
parent
41a27c6073
commit
55e7b82ba8
|
@ -0,0 +1,34 @@
|
|||
/**
|
||||
*
|
||||
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
|
||||
*
|
||||
* ====================================================================
|
||||
* Licensed 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 com.google.gson;
|
||||
|
||||
/**
|
||||
* The gson project use package to control access to their objects. However,
|
||||
* this prevents us from doing valid work, like controling the json emitted on a
|
||||
* per-object basis. This is here to afford us to do this.
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class ExposedEscaper extends Escaper {
|
||||
|
||||
public ExposedEscaper(boolean escapeHtmlCharacters) {
|
||||
super(escapeHtmlCharacters);
|
||||
}
|
||||
|
||||
}
|
|
@ -70,6 +70,7 @@ import com.google.common.collect.ImmutableList;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.OutputSupplier;
|
||||
import com.google.gson.ExposedEscaper;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.ProvisionException;
|
||||
import com.google.inject.spi.Message;
|
||||
|
@ -80,6 +81,11 @@ import com.google.inject.spi.Message;
|
|||
* @author Adrian Cole
|
||||
*/
|
||||
public class Utils {
|
||||
private static final ExposedEscaper escaper = new ExposedEscaper(false);
|
||||
|
||||
public static String escapeJsonString(CharSequence plainText) {
|
||||
return escaper.escapeJsonString(plainText);
|
||||
}
|
||||
|
||||
public static <K, V> Supplier<Map<K, V>> composeMapSupplier(Iterable<Supplier<Map<K, V>>> suppliers) {
|
||||
return new ListMapSupplier<K, V>(suppliers);
|
||||
|
@ -210,7 +216,7 @@ public class Utils {
|
|||
}
|
||||
|
||||
public static boolean eventuallyTrue(Supplier<Boolean> assertion, long inconsistencyMillis)
|
||||
throws InterruptedException {
|
||||
throws InterruptedException {
|
||||
|
||||
for (int i = 0; i < 30; i++) {
|
||||
if (!assertion.get()) {
|
||||
|
@ -245,14 +251,15 @@ public class Utils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Encode the given string with the given encoding, if possible. If the encoding fails with
|
||||
* {@link UnsupportedEncodingException}, log a warning and fall back to the system's default
|
||||
* encoding.
|
||||
* Encode the given string with the given encoding, if possible. If the
|
||||
* encoding fails with {@link UnsupportedEncodingException}, log a warning
|
||||
* and fall back to the system's default encoding.
|
||||
*
|
||||
* @param str
|
||||
* what to encode
|
||||
* @param charsetName
|
||||
* the name of a supported {@link java.nio.charset.Charset </code>charset<code>}
|
||||
* the name of a supported {@link java.nio.charset.Charset
|
||||
* </code>charset<code>}
|
||||
* @return properly encoded String.
|
||||
*/
|
||||
public static byte[] encodeString(String str, String charsetName) {
|
||||
|
@ -260,15 +267,16 @@ public class Utils {
|
|||
return str.getBytes(charsetName);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
logger.warn(e, "Failed to encode string to bytes with encoding " + charsetName
|
||||
+ ". Falling back to system's default encoding");
|
||||
+ ". Falling back to system's default encoding");
|
||||
return str.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the given string with the UTF-8 encoding, the sane default. In the very unlikely event
|
||||
* the encoding fails with {@link UnsupportedEncodingException}, log a warning and fall back to
|
||||
* the system's default encoding.
|
||||
* Encode the given string with the UTF-8 encoding, the sane default. In the
|
||||
* very unlikely event the encoding fails with
|
||||
* {@link UnsupportedEncodingException}, log a warning and fall back to the
|
||||
* system's default encoding.
|
||||
*
|
||||
* @param str
|
||||
* what to encode
|
||||
|
@ -319,7 +327,8 @@ public class Utils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Will throw an exception if the argument is null or empty. Accepts a custom error message.
|
||||
* Will throw an exception if the argument is null or empty. Accepts a custom
|
||||
* error message.
|
||||
*
|
||||
* @param nullableString
|
||||
* string to verify. Can be null or empty.
|
||||
|
@ -331,8 +340,8 @@ public class Utils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
|
||||
* rest.properties to populate the set.
|
||||
* Gets a set of supported providers. Idea stolen from pallets
|
||||
* (supported-clouds). Uses rest.properties to populate the set.
|
||||
*
|
||||
*/
|
||||
public static Iterable<String> getSupportedProviders() {
|
||||
|
@ -340,8 +349,8 @@ public class Utils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets a set of supported providers. Idea stolen from pallets (supported-clouds). Uses
|
||||
* rest.properties to populate the set.
|
||||
* Gets a set of supported providers. Idea stolen from pallets
|
||||
* (supported-clouds). Uses rest.properties to populate the set.
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -357,7 +366,7 @@ public class Utils {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Iterable<String> getSupportedProvidersOfTypeInProperties(
|
||||
final Class<? extends RestContextBuilder> type, final Properties properties) {
|
||||
final Class<? extends RestContextBuilder> type, final Properties properties) {
|
||||
return filter(transform(filter(properties.entrySet(), new Predicate<Map.Entry<Object, Object>>() {
|
||||
|
||||
@Override
|
||||
|
@ -388,8 +397,8 @@ public class Utils {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <S, A> Class<RestContextBuilder<S, A>> resolveContextBuilderClass(String provider,
|
||||
Properties properties) throws ClassNotFoundException, IllegalArgumentException, SecurityException,
|
||||
InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
Properties properties) throws ClassNotFoundException, IllegalArgumentException, SecurityException,
|
||||
InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
String contextBuilderClassName = properties.getProperty(provider + ".contextbuilder");
|
||||
String syncClassName = properties.getProperty(provider + ".sync");
|
||||
String asyncClassName = properties.getProperty(provider + ".async");
|
||||
|
@ -398,7 +407,7 @@ public class Utils {
|
|||
Class.forName(syncClassName);
|
||||
Class.forName(asyncClassName);
|
||||
return (Class<RestContextBuilder<S, A>>) (contextBuilderClassName != null ? Class
|
||||
.forName(contextBuilderClassName) : RestContextBuilder.class);
|
||||
.forName(contextBuilderClassName) : RestContextBuilder.class);
|
||||
} else {
|
||||
checkArgument(contextBuilderClassName != null, "please configure contextbuilder class for " + provider);
|
||||
return (Class<RestContextBuilder<S, A>>) Class.forName(contextBuilderClassName);
|
||||
|
@ -406,9 +415,9 @@ public class Utils {
|
|||
}
|
||||
|
||||
public static <S, A> RestContextBuilder<S, A> initContextBuilder(
|
||||
Class<RestContextBuilder<S, A>> contextBuilderClass, @Nullable Class<S> sync, @Nullable Class<A> async,
|
||||
Properties properties) throws ClassNotFoundException, IllegalArgumentException, SecurityException,
|
||||
InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
Class<RestContextBuilder<S, A>> contextBuilderClass, @Nullable Class<S> sync, @Nullable Class<A> async,
|
||||
Properties properties) throws ClassNotFoundException, IllegalArgumentException, SecurityException,
|
||||
InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
checkArgument(properties != null, "please configure properties for " + contextBuilderClass);
|
||||
try {
|
||||
return (RestContextBuilder<S, A>) contextBuilderClass.getConstructor(Properties.class).newInstance(properties);
|
||||
|
@ -416,14 +425,14 @@ public class Utils {
|
|||
checkArgument(sync != null, "please configure sync class for " + contextBuilderClass);
|
||||
checkArgument(async != null, "please configure async class for " + contextBuilderClass);
|
||||
return (RestContextBuilder<S, A>) contextBuilderClass.getConstructor(sync.getClass(), async.getClass(),
|
||||
Properties.class).newInstance(sync, async, properties);
|
||||
Properties.class).newInstance(sync, async, properties);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Class<PropertiesBuilder> resolvePropertiesBuilderClass(String providerName, Properties props)
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException,
|
||||
NoSuchMethodException {
|
||||
throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException,
|
||||
NoSuchMethodException {
|
||||
String propertiesBuilderClassName = props.getProperty(providerName + ".propertiesbuilder", null);
|
||||
if (propertiesBuilderClassName != null) {
|
||||
return (Class<PropertiesBuilder>) Class.forName(propertiesBuilderClassName);
|
||||
|
@ -434,7 +443,7 @@ public class Utils {
|
|||
|
||||
public static Iterable<Module> modulesForProviderInProperties(String providerName, Properties props) {
|
||||
return concat(modulesFromProperty(props, "jclouds.modules"),
|
||||
modulesFromProperty(props, providerName + ".modules"));
|
||||
modulesFromProperty(props, providerName + ".modules"));
|
||||
}
|
||||
|
||||
public static Iterable<Module> modulesFromProperty(Properties props, String property) {
|
||||
|
|
|
@ -27,12 +27,12 @@ import java.util.concurrent.TimeoutException;
|
|||
import org.jclouds.rest.AuthorizationException;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.inject.ProvisionException;
|
||||
import com.google.inject.internal.ImmutableList;
|
||||
import com.google.inject.spi.Message;
|
||||
|
||||
/**
|
||||
|
@ -68,8 +68,7 @@ public class UtilsTest {
|
|||
}
|
||||
|
||||
public void testReplaceTokens() throws UnsupportedEncodingException {
|
||||
assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")),
|
||||
"hello world");
|
||||
assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world");
|
||||
}
|
||||
|
||||
public void testMultiMax() {
|
||||
|
|
Loading…
Reference in New Issue