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

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

Introduction

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

Prototype

public static String trimToEmpty(final String str) 

Source Link

Document

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

Usage

From source file:org.labkey.test.pages.trialshare.CubeObjectEditPage.java

public String getFormValue(String field) {
    elementCache().cancelButton.isDisplayed();
    Locator fieldLocator = Locator.name(field);
    return StringUtils.trimToEmpty(getFormElement(fieldLocator));
}

From source file:org.labkey.test.pages.trialshare.CubeObjectEditPage.java

public Map<String, String> compareFormValues(Map<String, Object> expectedValues) {
    elementCache().cancelButton.isDisplayed(); // Make sure
    Map<String, String> formValues = getFormValues();
    Map<String, String> unexpectedValues = new HashMap<>();
    for (String fieldName : expectedValues.keySet()) {
        String expectedValue;/*from ww  w . ja  va2  s.c  o  m*/
        if (expectedValues.get(fieldName) instanceof String) {
            expectedValue = (String) expectedValues.get(fieldName);
        } else // should be an array of Strings
        {
            expectedValue = StringUtils.join((String[]) expectedValues.get(fieldName), "; ");
        }
        String formValue = StringUtils.trimToEmpty(formValues.get(fieldName));
        log("Comparing field values for " + fieldName + " expecting " + expectedValue + " actual " + formValue);
        if (expectedValue.equals(NOT_EMPTY_VALUE)) {
            if (formValue.isEmpty())
                unexpectedValues.put(fieldName, "expected: " + expectedValue + " actual: " + formValue);
        } else if (!expectedValue.equals(formValue)) {
            unexpectedValues.put(fieldName,
                    " expected: \"" + expectedValue + "\" but was: \"" + formValue + "\"");
        }
    }
    return unexpectedValues;
}

From source file:org.libreplan.web.common.JobSchedulerController.java

/**
 * Concatenating the cronExpression values.
 *
 * @return cronExpression string/*from ww w. ja va2s. c  o  m*/
 */
private String getCronExpressionString() {
    String cronExpression = "";
    cronExpression += StringUtils.trimToEmpty(cronExpressionSeconds.getValue()) + " ";
    cronExpression += StringUtils.trimToEmpty(cronExpressionMinutes.getValue()) + " ";
    cronExpression += StringUtils.trimToEmpty(cronExpressionHours.getValue()) + " ";
    cronExpression += StringUtils.trimToEmpty(cronExpressionDayOfMonth.getValue()) + " ";
    cronExpression += StringUtils.trimToEmpty(cronExpressionMonth.getValue()) + " ";
    cronExpression += StringUtils.trimToEmpty(cronExpressionDayOfWeek.getValue());

    String year = StringUtils.trimToEmpty(cronExpressionYear.getValue());

    return !year.isEmpty() ? cronExpression + " " + year : cronExpression;
}

From source file:org.ligoj.app.http.security.CaptchaFilter.java

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    final HttpSession session = httpServletRequest.getSession(false);
    final Captcha captcha = session == null ? null : (Captcha) session.getAttribute(Captcha.NAME);
    if (captcha == null) {
        // No session -> no CAPTCHA to match
        log.info("No configured for this session");
        fail(response, "session", "null");
    } else {//from   w w  w . jav  a 2 s. c  o m
        session.removeAttribute(Captcha.NAME);
        if (captcha.isCorrect(StringUtils.trimToEmpty(httpServletRequest.getHeader(CAPTCHA_HEADER)))) {
            chain.doFilter(request, response);
        } else {
            // CAPTCHA does not match -> the CAPTCHA must be regenerated.
            log.info("Invalid captcha received from {} '{}' instead of {}", httpServletRequest.getRemoteHost(),
                    request.getParameter(CAPTCHA_HEADER), captcha.getAnswer());
            fail(response, CAPTCHA_HEADER, "invalid");
        }
    }
}

From source file:org.ligoj.app.plugin.build.jenkins.JenkinsPluginResource.java

/**
 * Search the Jenkin's jobs matching to the given criteria. Name, display name and description are considered.
 *
 * @param node/*from  w  w  w .j  av  a  2 s  .c o  m*/
 *            the node to be tested with given parameters.
 * @param criteria
 *            the search criteria.
 * @param view
 *            The optional view URL.
 * @return job names matching the criteria.
 */
private List<Job> findAllByName(final String node, final String criteria, final String view)
        throws SAXException, IOException, ParserConfigurationException {

    // Prepare the context, an ordered set of jobs
    final Format format = new NormalizeFormat();
    final String formatCriteria = format.format(criteria);
    final Map<String, String> parameters = pvResource.getNodeParameters(node);

    // Get the jobs and parse them
    final String url = StringUtils.trimToEmpty(view) + "api/xml?tree=jobs[name,displayName,description,color]";
    final String jobsAsXml = StringUtils.defaultString(getResource(parameters, url), "<a/>");
    final InputStream jobsAsInput = IOUtils.toInputStream(jobsAsXml, StandardCharsets.UTF_8);
    final Element hudson = (Element) xml.parse(jobsAsInput).getFirstChild();
    final Map<String, Job> result = new TreeMap<>();
    for (final Element jobNode : DomUtils.getChildElementsByTagName(hudson, "job")) {

        // Extract string data from this job
        final String name = StringUtils.trimToEmpty(DomUtils.getChildElementValueByTagName(jobNode, "name"));
        final String displayName = StringUtils
                .trimToEmpty(DomUtils.getChildElementValueByTagName(jobNode, "displayName"));
        final String description = StringUtils
                .trimToEmpty(DomUtils.getChildElementValueByTagName(jobNode, "description"));

        // Check the values of this job
        if (format.format(name).contains(formatCriteria) || format.format(displayName).contains(formatCriteria)
                || format.format(description).contains(formatCriteria)) {

            // Retrieve description and display name
            final Job job = new Job();
            job.setName(StringUtils.trimToNull(displayName));
            job.setDescription(StringUtils.trimToNull(description));
            job.setId(name);
            job.setStatus(toStatus(DomUtils.getChildElementValueByTagName(jobNode, "color")));
            result.put(format.format(ObjectUtils.defaultIfNull(job.getName(), job.getId())), job);
        }
    }
    return new ArrayList<>(result.values());
}

From source file:org.ligoj.app.plugin.id.ldap.resource.LdapPluginResource.java

/**
 * Build a user LDAP repository from the given node.
 *
 * @param node/*from  w  w w.ja va2  s . c o m*/
 *            The node, also used as cache key.
 * @return The {@link UserLdapRepository} instance. Cache is involved.
 */
private UserLdapRepository getUserLdapRepository(final String node) {
    log.info("Build ldap template for node {}", node);
    final Map<String, String> parameters = pvResource.getNodeParameters(node);
    final LdapContextSource contextSource = new LdapContextSource();
    contextSource.setReferral(parameters.get(PARAMETER_REFERRAL));
    contextSource.setPassword(parameters.get(PARAMETER_PASSWORD));
    contextSource.setUrl(parameters.get(PARAMETER_URL));
    contextSource.setUserDn(parameters.get(PARAMETER_USER));
    contextSource.setBase(parameters.get(PARAMETER_BASE_BN));
    contextSource.afterPropertiesSet();
    final LdapTemplate template = new LdapTemplate();
    template.setContextSource(contextSource);
    template.setIgnorePartialResultException(true);

    // A new repository instance
    final UserLdapRepository repository = new UserLdapRepository();
    repository.setTemplate(template);
    repository.setPeopleBaseDn(StringUtils.trimToEmpty(parameters.get(PARAMETER_PEOPLE_DN)));
    repository.setPeopleInternalBaseDn(parameters.get(PARAMETER_PEOPLE_INTERNAL_DN));
    repository.setQuarantineBaseDn(StringUtils.trimToEmpty(parameters.get(PARAMETER_QUARANTINE_DN)));
    repository.setDepartmentAttribute(parameters.get(PARAMETER_DEPARTMENT_ATTRIBUTE));
    repository.setLocalIdAttribute(parameters.get(PARAMETER_LOCAL_ID_ATTRIBUTE));
    repository.setUidAttribute(parameters.get(PARAMETER_UID_ATTRIBUTE));
    repository.setLockedAttribute(parameters.get(PARAMETER_LOCKED_ATTRIBUTE));
    repository.setLockedValue(parameters.get(PARAMETER_LOCKED_VALUE));
    repository.setPeopleClass(parameters.get(PARAMETER_PEOPLE_CLASS));
    repository.setCompanyPattern(StringUtils.trimToEmpty(parameters.get(PARAMETER_COMPANY_PATTERN)));
    repository.setClearPassword(Boolean.parseBoolean(parameters.get(PARAMETER_CLEAR_PASSWORD)));

    // Complete the bean
    SpringUtils.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(repository);

    return repository;
}

From source file:org.ligoj.app.plugin.id.ldap.resource.LdapPluginResource.java

/**
 * Build a group LDAP repository from the given node.
 *
 * @param node//  w w  w.java  2  s  .  c  om
 *            The node, also used as cache key.
 * @param template
 *            The {@link LdapTemplate} used to query the repository.
 * @return The {@link UserLdapRepository} instance. Cache is involved.
 */
public GroupLdapRepository newGroupLdapRepository(final String node, final LdapTemplate template) {
    final Map<String, String> parameters = pvResource.getNodeParameters(node);

    // A new repository instance
    final GroupLdapRepository repository = new GroupLdapRepository();
    repository.setTemplate(template);
    repository.setGroupsBaseDn(StringUtils.trimToEmpty(parameters.get(PARAMETER_GROUPS_DN)));

    // Complete the bean
    SpringUtils.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(repository);
    return repository;
}

From source file:org.ligoj.app.plugin.id.ldap.resource.LdapPluginResource.java

private String normalize(final String string) {
    return StringUtils.trimToEmpty(Normalizer.normalize(string).replace("[^\\w\\d]", " ").replace("  ", " "));
}

From source file:org.ligoj.app.plugin.id.resource.batch.AbstractBatchTask.java

/**
 * Split and normalize a string to a collection, ignoring empty items.
 * //from ww w. jav a2s .  c  o m
 * @param rawValue
 *            The raw string to split.
 * @return A collection from the raw string.
 */
protected List<String> toList(final String rawValue) {
    return Pattern.compile(",").splitAsStream(StringUtils.trimToEmpty(rawValue)).map(Normalizer::normalize)
            .filter(StringUtils::isNotBlank).collect(Collectors.toList());
}