Example usage for org.apache.commons.lang3 StringUtils trimToNull

List of usage examples for org.apache.commons.lang3 StringUtils trimToNull

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils trimToNull.

Prototype

public static String trimToNull(final String str) 

Source Link

Document

Removes control characters (char <= 32) from both ends of this String returning null if the String is empty ("") after the trim or if it is null .

Usage

From source file:com.monarchapis.driver.service.v1.SingleServiceInfoResolver.java

public void setEnvironmentName(String environmentName) {
    this.environmentName = StringUtils.trimToNull(environmentName);
}

From source file:com.haulmont.cuba.web.gui.components.WebAbstractTextArea.java

@Override
protected V convertToModel(String componentRawValue) throws ConversionException {
    String value = emptyToNull(componentRawValue);

    if (isTrimming()) {
        value = StringUtils.trimToNull(value);
    }//from   w  w w  .j a  v a 2  s . c  o m

    if (datatype != null) {
        try {
            return datatype.parse(value, locale);
        } catch (ParseException e) {
            // vaadin8 localized message
            throw new ConversionException("Unable to convert value", e);
        }
    }

    if (valueBinding != null && valueBinding.getSource() instanceof EntityValueSource) {
        EntityValueSource entityValueSource = (EntityValueSource) valueBinding.getSource();
        Datatype<V> propertyDataType = entityValueSource.getMetaPropertyPath().getRange().asDatatype();
        try {
            return propertyDataType.parse(componentRawValue);
        } catch (ParseException e) {
            // vaadin8 localized message
            throw new ConversionException("Unable to convert value", e);
        }
    }

    return super.convertToModel(componentRawValue);
}

From source file:alfio.controller.api.admin.SpecialPriceApiController.java

@RequestMapping(value = "/events/{eventName}/categories/{categoryId}/link-codes")
public List<SendCodeModification> linkAssigneeToCodes(@PathVariable("eventName") String eventName,
        @PathVariable("categoryId") int categoryId, @RequestBody UploadBase64FileModification file,
        Principal principal) throws IOException {

    Validate.isTrue(StringUtils.isNotEmpty(eventName));
    try (InputStreamReader isr = new InputStreamReader(file.getInputStream());
            CSVReader reader = new CSVReader(isr)) {
        List<SendCodeModification> content = reader.readAll().stream().map(line -> {
            Validate.isTrue(line.length >= 4);
            return new SendCodeModification(StringUtils.trimToNull(line[0]), trim(line[1]), trim(line[2]),
                    trim(line[3]));/*from  w  w  w  . j  a va 2  s .c o  m*/
        }).collect(Collectors.toList());
        return specialPriceManager.linkAssigneeToCode(content, eventName, categoryId, principal.getName());
    }
}

From source file:com.qq.tars.service.PatchService.java

private String md5(String path) {
    String command = "md5sum " + path;
    Pair<Integer, Pair<String, String>> result = SystemUtils.exec(command);
    if (result.getLeft() == -1) {
        return null;
    }// ww  w .  ja  va 2 s  .c o m
    String stdout = result.getRight().getLeft();
    if (stdout.contains(" ")) {
        stdout = stdout.substring(0, stdout.indexOf(" "));
    }

    return StringUtils.trimToNull(stdout);
}

From source file:hoot.services.controllers.job.InputParamsValidator.java

/**
 * Parses and validates a set of input parameters to a Jersey service method
 * //w ww .j a  va  2 s. c  o m
 * @param name
 *            name of the parameter
 * @param type
 *            type of the parameter
 * @param rangeMin
 *            minimum allowable value for numeric parameters
 * @param rangeMax
 *            maximum allowable value for numeric parameters
 * @param optional
 *            if true; the parameter is considered optional and must not be
 *            present
 * @param defaultValue
 *            a default value to assign to the parameter, if it has no value
 * @return a parameter value
 */
public Object validateAndParseInputParam(String name, Object type, Object rangeMin, Object rangeMax,
        boolean optional, Object defaultValue) {

    // special case
    if (name.equals("geospatialBounds")) {
        if ((inputParams.get("geospatialBounds") == null) && (defaultValue != null)) {
            return new BoundingBox((String) defaultValue);
        }
        return new BoundingBox((String) inputParams.get("geospatialBounds"));
    }

    Object param = inputParams.get(name);
    if (((param == null) || (StringUtils.trimToNull(String.valueOf(param).trim()) == null)) && !optional) {
        throw new IllegalArgumentException("Invalid input parameter value.  Required parameter: " + name
                + " not sent to: " + ReflectUtils.getCallingClassName());
    }

    if ((param == null) && (defaultValue != null)) {
        if (!type.getClass().equals(defaultValue.getClass())) {
            throw new IllegalArgumentException(
                    "Invalid input parameter value.  Mismatching input parameter type: " + type
                            + " and default value type: " + defaultValue + "for parameter: " + name
                            + " sent to: " + ReflectUtils.getCallingClassName());
        }
        return defaultValue;
    }

    if (param != null) {
        if (StringUtils.trimToNull(String.valueOf(param).trim()) == null) {
            throw new IllegalArgumentException(
                    "Invalid input parameter: " + name + " sent to: " + ReflectUtils.getCallingClassName());
        }

        return validateAndParseParamValueString(String.valueOf(param).trim(), name, type, rangeMin, rangeMax);
    }

    Object paramValue = null;

    return paramValue;
}

From source file:com.monarchapis.driver.service.v1.SingleServiceInfoResolver.java

public void setServiceName(String serviceName) {
    this.serviceName = StringUtils.trimToNull(serviceName);
}

From source file:hoot.services.validators.review.ReviewInputParamsValidator.java

/**
 * Parses and validates a set of input parameters to a Jersey review service method
 * //from  ww w.  j  a  v a2s. c o  m
 * @param name name of the parameter
 * @param type type of the parameter
 * @param rangeMin minimum allowable value for numeric parameters
 * @param rangeMax maximum allowable value for numeric parameters
 * @param optional if true; the parameter is considered optional and must not be present
 * @param defaultValue a default value to assign to the parameter, if it has no value
 * @return a parameter value
 * @throws Exception
 * @todo add enum validation capabilities
 */
public Object validateAndParseInputParam(final String name, final Object type, final Object rangeMin,
        final Object rangeMax, final boolean optional, final Object defaultValue) throws Exception {
    Object paramValue = null;

    //special case
    if (name.equals("geospatialBounds")) {
        if (inputParams.get("geospatialBounds") == null && defaultValue != null) {
            return new BoundingBox((String) defaultValue);
        } else {
            return new BoundingBox((String) inputParams.get("geospatialBounds"));
        }
    }

    Object param = inputParams.get(name);
    if ((param == null || StringUtils.trimToNull(String.valueOf(param).trim()) == null) && !optional) {
        throw new Exception("Invalid input parameter value.  Required parameter: " + name + " not sent to: "
                + ReflectUtils.getCallingClassName());
    }

    if (param == null && defaultValue != null) {
        if (!type.getClass().equals(defaultValue.getClass())) {
            throw new Exception("Invalid input parameter value.  Mismatching input parameter type: "
                    + type.toString() + " and default value type: " + defaultValue.toString()
                    + "for parameter: " + name + " sent to: " + ReflectUtils.getCallingClassName());
        }
        return defaultValue;
    }

    if (param != null) {
        if (StringUtils.trimToNull(String.valueOf(param).trim()) == null) {
            throw new Exception(
                    "Invalid input parameter: " + name + " sent to: " + ReflectUtils.getCallingClassName());
        }

        return validateAndParseParamValueString(String.valueOf(param).trim(), name, type, rangeMin, rangeMax);
    }

    return paramValue;
}

From source file:com.monarchapis.driver.service.v1.SingleServiceInfoResolver.java

public void setProviderKey(String providerKey) {
    this.providerKey = StringUtils.trimToNull(providerKey);
}

From source file:kenh.xscript.elements.Call.java

@Override
public int invoke() throws UnsupportedScriptException {

    logger.info(getInfo());//from   w w w  . j a  v a 2  s.  co m

    this.getEnvironment().removeVariable(Return.VARIABLE_RETURN); // clear {return}

    // 1) find Method
    Map<String, Element> methods = this.getEnvironment().getMethods();
    String name = getAttribute(ATTRIBUTE_METHOD_NAME);
    if (StringUtils.isBlank(name)) {
        UnsupportedScriptException ex = new UnsupportedScriptException(this, "The method name is empty.");
        throw ex;
    }
    try {
        name = (String) this.getEnvironment().parse(name);
    } catch (Exception e) {
        throw new UnsupportedScriptException(this, e);
    }
    String var = getAttribute(ATTRIBUTE_RETURN_NAME);
    if (StringUtils.isNotBlank(var)) {
        try {
            var = StringUtils.trimToNull((String) this.getEnvironment().parse(var));
        } catch (Exception e) {
            throw new UnsupportedScriptException(this, e);
        }
    }

    Element e = methods.get(name);
    if (e == null || !(e instanceof Method)) {
        UnsupportedScriptException ex = new UnsupportedScriptException(this,
                "Could't find the method to invoke. [" + name + "]");
        throw ex;
    }

    // 2) handle the Method's parameter
    Method m = (Method) e;
    String[][] parameters = m.getParameters();

    Map<String, Object> new_vars = new LinkedHashMap();
    List<String> new_cons = new LinkedList();
    Vector parameterCallback = new Vector();

    for (String[] parameter : parameters) {
        String paraName = StringUtils.trimToEmpty(parameter[0]);
        if (StringUtils.isBlank(paraName))
            continue;

        boolean required = false;
        Object defaultValue = null;

        if (parameter.length > 1) {
            for (int i = 1; i < parameter.length; i++) {
                String modi = StringUtils.trimToEmpty(parameter[i]);
                if (modi.equals(Method.MODI_FINAL)) {
                    new_cons.add(paraName);
                }
                if (modi.equals(Method.MODI_REQUIRED)) {
                    required = true;
                }
                if ((modi.startsWith(Method.MODI_DEFAULT + "(") && modi.endsWith(")"))) {
                    String defaultValue_ = StringUtils.substringAfter(modi, Method.MODI_DEFAULT + "(");
                    defaultValue_ = StringUtils.substringBeforeLast(defaultValue_, ")");
                    try {
                        defaultValue = this.getEnvironment().parse(defaultValue_);
                    } catch (UnsupportedExpressionException e_) {
                        UnsupportedScriptException ex = new UnsupportedScriptException(this, e_);
                        throw ex;
                    }
                }
            }
        }

        String paraValue = this.getAttribute(paraName);
        Object paraObj = null;
        if (paraValue == null) {

            if (required) {
                UnsupportedScriptException ex = new UnsupportedScriptException(this,
                        "Missing parameter. [" + paraName + "]");
                throw ex;
            } else {
                if (defaultValue != null) {
                    new_vars.put(paraName, defaultValue);
                }
            }

        } else {
            try {
                paraObj = this.getEnvironment().parse(paraValue);
                new_vars.put(paraName, paraObj);
                if (paraObj instanceof kenh.expl.Callback) {
                    parameterCallback.add(paraObj);
                }

            } catch (UnsupportedExpressionException ex) {
                throw new UnsupportedScriptException(this, ex);
            }
        }

    }

    if (this.getAttributes().size() > new_vars.size()
            + (this.getAttributes().containsKey(ATTRIBUTE_RETURN_NAME) ? 2 : 1)) {
        java.util.Set<String> keys = this.getAttributes().keySet();
        String additional = "";
        for (String key : keys) {
            if (key.equals(ATTRIBUTE_METHOD_NAME))
                continue;
            if (key.equals(ATTRIBUTE_RETURN_NAME))
                continue;
            if (new_vars.containsKey(key))
                continue;
            additional = additional + key + ", ";
        }
        UnsupportedScriptException ex = new UnsupportedScriptException(this,
                "Unknown parameter. [" + StringUtils.substringBeforeLast(additional, ",") + "]");
        throw ex;
    }

    // 3) remove non-public variable in Environment. save Method's parameter in Environment.
    List<String> publics = this.getEnvironment().getPublics();
    List<String> constant = this.getEnvironment().getContants();

    Map<String, Object> keep_vars = new LinkedHashMap();
    List<String> keep_cons = new LinkedList();
    List<String> keep_pubs = new LinkedList();
    java.util.Set<String> keys = this.getEnvironment().getVariables().keySet();
    for (String key : keys) {
        if (!publics.contains(key)) {
            keep_vars.put(key, this.getEnvironment().getVariable(key));
            if (constant.contains(key))
                keep_cons.add(key);
        }
    }

    keys = keep_vars.keySet();
    for (String key : keys) {
        if (constant.contains(key))
            constant.remove(key);
        this.getEnvironment().removeVariable(key, false);
    }

    // public variable in Environment have the same name with Method's parameter
    for (String[] parameter : parameters) {
        String key = StringUtils.trimToEmpty(parameter[0]);
        if (this.getEnvironment().containsVariable(key)) {
            if (constant.contains(key)) {
                constant.remove(key);
                keep_cons.add(key);
            }
            publics.remove(key);
            keep_pubs.add(key);
            keep_vars.put(key, this.getEnvironment().removeVariable(key, false));
        }
        this.getEnvironment().setVariable(key, new_vars.get(key));
        if (new_cons.contains(key))
            constant.add(key);
    }

    // 4)invoke the Method's child elements.
    int r = m.processChildren();
    if (r != RETURN && r != NONE) {
        if (r == EXCEPTION) {
            Object ex = this.getEnvironment().getVariable(Constant.VARIABLE_EXCEPTION);
            if (ex instanceof Exception) {
                if (ex instanceof UnsupportedScriptException) {
                    throw (UnsupportedScriptException) ex;
                } else {
                    UnsupportedScriptException ex_ = new UnsupportedScriptException(this, (Exception) ex);
                    throw ex_;
                }
            }

        }

        UnsupportedScriptException ex = new UnsupportedScriptException(this,
                "Unsupported value is returned. [" + r + "]");
        throw ex;
    }

    Object returnObj = null;
    if (StringUtils.isNotBlank(var)) {
        if (r == RETURN) {
            returnObj = this.getEnvironment().getVariable(Return.VARIABLE_RETURN);
        } else {
            UnsupportedScriptException ex = new UnsupportedScriptException(this,
                    "The method does not have return value. [" + name + "]");
            throw ex;
        }
    }

    // 5) remove non-public variable from Environment. restore original variables
    List<String> remove_vars = new LinkedList();
    keys = this.getEnvironment().getVariables().keySet();
    for (String key : keys) {
        if (!publics.contains(key)) {
            remove_vars.add(key);
        }
    }

    for (String key : remove_vars) {
        if (constant.contains(key))
            constant.remove(key);
        Object obj = this.getEnvironment().getVariable(key);
        if (parameterCallback.contains(obj)) {
            this.getEnvironment().removeVariable(key, false);
        } else {
            this.getEnvironment().removeVariable(key);
        }
    }

    keys = keep_vars.keySet();
    for (String key : keys) {
        if (!constant.contains(key)) {
            if (!this.getEnvironment().containsVariable(key))
                this.getEnvironment().setVariable(key, keep_vars.get(key));
        }
        if (keep_cons.contains(key))
            constant.add(key);
        if (keep_pubs.contains(key))
            publics.add(key);
    }

    // 6) store {return}
    if (returnObj != null) {
        this.saveVariable(var, returnObj, null);
    }

    return NONE;
}

From source file:com.threewks.thundr.bigmetrics.admin.BigMetricsQueryController.java

public JsonView queryResults(String queryId, Long pageSize, String pageToken) {
    try {/*from  ww w .  j  a  v a2s.  c om*/
        pageToken = StringUtils.trimToNull(pageToken);
        pageSize = clamp(pageSize, 1, 10000, 1000);

        boolean ready = bigMetricsService.isQueryComplete(queryId);
        if (!ready) {
            return new JsonView("Processing").withStatusCode(StatusCode.Accepted);
        }
        QueryResult queryResult = bigMetricsService.queryResult(queryId, pageSize, pageToken);
        return new JsonView(queryResult);
    } catch (RuntimeException e) {
        Logger.warn("Failed to check query results for job id %s: %s", queryId, e.getMessage());
        return new JsonView("Failed " + e.getMessage()).withStatusCode(StatusCode.InternalServerError);
    }
}