Search in sources :

Example 1 with GString

use of groovy.lang.GString in project spring-integration by spring-projects.

the class GroovyCommandMessageProcessor method executeScript.

@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
    Assert.notNull(scriptSource, "scriptSource must not be null");
    VariableBindingGroovyObjectCustomizerDecorator customizerDecorator = this.binding != null ? new BindingOverwriteGroovyObjectCustomizerDecorator(this.binding) : new VariableBindingGroovyObjectCustomizerDecorator();
    if (this.customizer != null) {
        customizerDecorator.setCustomizer(this.customizer);
    }
    if (!CollectionUtils.isEmpty(variables)) {
        customizerDecorator.setVariables(variables);
    }
    GroovyScriptFactory factory = new GroovyScriptFactory(this.getClass().getSimpleName(), customizerDecorator);
    if (this.beanClassLoader != null) {
        factory.setBeanClassLoader(this.beanClassLoader);
    }
    if (this.beanFactory != null) {
        factory.setBeanFactory(this.beanFactory);
    }
    Object result = factory.getScriptedObject(scriptSource);
    return (result instanceof GString) ? result.toString() : result;
}
Also used : GroovyScriptFactory(org.springframework.scripting.groovy.GroovyScriptFactory) GString(groovy.lang.GString)

Example 2 with GString

use of groovy.lang.GString in project spring-integration by spring-projects.

the class GroovyScriptExecutingMessageProcessor method executeScript.

@Override
protected Object executeScript(ScriptSource scriptSource, Map<String, Object> variables) throws Exception {
    Assert.notNull(scriptSource, "scriptSource must not be null");
    this.parseScriptIfNecessary(scriptSource);
    Object result = this.execute(variables);
    return (result instanceof GString) ? result.toString() : result;
}
Also used : GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString)

Example 3 with GString

use of groovy.lang.GString in project rest-assured by rest-assured.

the class EncoderRegistry method encodeJSON.

/**
 * <p>Accepts a Collection or a JavaBean object which is converted to JSON.
 * A Map or Collection will be converted to a {@link JsonBuilder}..  A
 * String or GString will be interpreted as valid JSON and passed directly
 * as the request body (with charset conversion if necessary.)</p>
 * <p/>
 * <p>If a Closure is passed as the model, it will be executed as if it were
 * a JSON object definition passed to a {@link JsonBuilder}.  In order
 * for the closure to be interpreted correctly, there must be a 'root'
 * element immediately inside the closure.  For example:</p>
 * <p/>
 * <pre>builder.post( JSON ) {
 *   body = {
 *     root {
 *       first {
 *         one = 1
 *         two = '2'
 *       }
 *       second = 'some string'
 *     }
 *   }
 * }</pre>
 * <p> will return the following JSON string:<pre>
 * {"root":{"first":{"one":1,"two":"2"},"second":"some string"}}</pre></p>
 *
 * @param model data to be converted to JSON, as specified above.
 * @return an {@link HttpEntity} encapsulating this request data
 * @throws UnsupportedEncodingException
 */
@SuppressWarnings("unchecked")
public HttpEntity encodeJSON(Object contentType, Object model) throws UnsupportedEncodingException {
    String contentTypeAsString = contentTypeToString(contentType);
    Object json;
    if (model instanceof Map || model instanceof Collection) {
        json = new JsonBuilder(model);
    } else if (model instanceof Closure) {
        Closure closure = (Closure) model;
        closure.setDelegate(new JsonBuilder());
        json = closure.call();
    } else if (model instanceof String || model instanceof GString || model instanceof byte[]) {
        // assume valid JSON already.
        json = model;
    } else if (model instanceof File) {
        json = toString((File) model, contentTypeAsString);
    } else {
        throw new UnsupportedOperationException("Internal error: Can't encode " + model + " to JSON.");
    }
    return createEntity(contentTypeAsString, json);
}
Also used : JsonBuilder(groovy.json.JsonBuilder) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) FileReader.readToString(io.restassured.internal.support.FileReader.readToString) GString(groovy.lang.GString) GString(groovy.lang.GString)

Example 4 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class ArrayCachedClass method coerceArgument.

public Object coerceArgument(Object argument) {
    Class argumentClass = argument.getClass();
    if (argumentClass.getName().charAt(0) != '[')
        return argument;
    Class argumentComponent = argumentClass.getComponentType();
    Class paramComponent = getTheClass().getComponentType();
    if (paramComponent.isPrimitive()) {
        argument = DefaultTypeTransformation.convertToPrimitiveArray(argument, paramComponent);
    } else if (paramComponent == String.class && argument instanceof GString[]) {
        GString[] strings = (GString[]) argument;
        String[] ret = new String[strings.length];
        for (int i = 0; i < strings.length; i++) {
            ret[i] = strings[i].toString();
        }
        argument = ret;
    } else if (paramComponent == Object.class && argumentComponent.isPrimitive()) {
        argument = DefaultTypeTransformation.primitiveArrayBox(argument);
    }
    return argument;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) GString(groovy.lang.GString)

Example 5 with GString

use of groovy.lang.GString in project groovy-core by groovy.

the class ObjectArrayPutAtMetaMethod method adjustNewValue.

private static Object adjustNewValue(Object[] objects, Object newValue) {
    Class arrayComponentClass = objects.getClass().getComponentType();
    Object adjustedNewVal = newValue;
    if (newValue instanceof Number) {
        if (!arrayComponentClass.equals(newValue.getClass())) {
            adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
        }
    } else if (Character.class.isAssignableFrom(arrayComponentClass)) {
        adjustedNewVal = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
    } else if (Number.class.isAssignableFrom(arrayComponentClass)) {
        if (newValue instanceof Character || newValue instanceof String || newValue instanceof GString) {
            Character ch = DefaultTypeTransformation.getCharFromSizeOneString(newValue);
            adjustedNewVal = DefaultTypeTransformation.castToType(ch, arrayComponentClass);
        }
    } else if (arrayComponentClass.isArray()) {
        adjustedNewVal = DefaultTypeTransformation.castToType(newValue, arrayComponentClass);
    }
    return adjustedNewVal;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) GString(groovy.lang.GString) GString(groovy.lang.GString)

Aggregations

GString (groovy.lang.GString)59 FromString (groovy.transform.stc.FromString)16 Closure (groovy.lang.Closure)5 GroovyObject (groovy.lang.GroovyObject)5 Map (java.util.Map)5 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Binding (groovy.lang.Binding)2 Iterator (java.util.Iterator)2 List (java.util.List)2 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)2 ExpressionEvaluationException (com.evolveum.midpoint.util.exception.ExpressionEvaluationException)1 JsonBuilder (groovy.json.JsonBuilder)1 GroovyShell (groovy.lang.GroovyShell)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 Script (groovy.lang.Script)1 FileReader.readToString (io.restassured.internal.support.FileReader.readToString)1 BufferedWriter (java.io.BufferedWriter)1