Example usage for org.apache.commons.lang StringUtils defaultIfBlank

List of usage examples for org.apache.commons.lang StringUtils defaultIfBlank

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils defaultIfBlank.

Prototype

public static String defaultIfBlank(String str, String defaultStr) 

Source Link

Document

Returns either the passed in String, or if the String is whitespace, empty ("") or null, the value of defaultStr.

Usage

From source file:org.sonar.server.issue.notification.IssueChangesEmailTemplate.java

private String getUserFullName(@Nullable String login) {
    if (login == null) {
        return null;
    }/* w  w  w .j a  va 2  s  . c  o  m*/
    try (DbSession dbSession = dbClient.openSession(false)) {
        UserDto userDto = dbClient.userDao().selectByLogin(dbSession, login);
        if (userDto == null || !userDto.isActive()) {
            // most probably user was deleted
            return login;
        }
        return StringUtils.defaultIfBlank(userDto.getName(), login);
    }
}

From source file:org.sonar.server.notification.email.EmailNotificationChannel.java

private void send(EmailMessage emailMessage) throws EmailException {
    // Trick to correctly initialize javax.mail library
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {// w  w w  .  j av  a  2  s  . c  o  m
        LOG.debug("Sending email: {}", emailMessage);
        String host = null;
        try {
            host = new URL(configuration.getServerBaseURL()).getHost();
        } catch (MalformedURLException e) {
            // ignore
        }

        SimpleEmail email = new SimpleEmail();
        if (StringUtils.isNotBlank(host)) {
            /*
             * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and
             * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook
             */
            if (StringUtils.isNotEmpty(emailMessage.getMessageId())) {
                String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">";
                email.addHeader(IN_REPLY_TO_HEADER, messageId);
                email.addHeader(REFERENCES_HEADER, messageId);
            }
            // Set headers for proper filtering
            email.addHeader(LIST_ID_HEADER, "SonarQube <sonar." + host + ">");
            email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL());
        }
        // Set general information
        email.setCharset("UTF-8");
        String from = StringUtils.isBlank(emailMessage.getFrom()) ? FROM_NAME_DEFAULT
                : (emailMessage.getFrom() + " (SonarQube)");
        email.setFrom(configuration.getFrom(), from);
        email.addTo(emailMessage.getTo(), " ");
        String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ",
                "") + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
        email.setSubject(subject);
        email.setMsg(emailMessage.getMessage());
        // Send
        email.setHostName(configuration.getSmtpHost());
        configureSecureConnection(email);
        if (StringUtils.isNotBlank(configuration.getSmtpUsername())
                || StringUtils.isNotBlank(configuration.getSmtpPassword())) {
            email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword());
        }
        email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
        email.setSocketTimeout(SOCKET_TIMEOUT);
        email.send();

    } finally {
        Thread.currentThread().setContextClassLoader(classloader);
    }
}

From source file:org.sonar.server.qualityprofile.RegisterQualityProfiles.java

private static String nameOfDefaultProfile(List<RulesProfile> profiles) {
    String defaultName = null;//  ww  w . j a va2 s .  c om
    boolean hasSonarWay = false;

    for (RulesProfile profile : profiles) {
        if (profile.getDefaultProfile()) {
            defaultName = profile.getName();
        } else if (DEFAULT_PROFILE_NAME.equals(profile.getName())) {
            hasSonarWay = true;
        }
    }

    if (StringUtils.isBlank(defaultName) && !hasSonarWay && !profiles.isEmpty()) {
        defaultName = profiles.get(0).getName();
    }

    return StringUtils.defaultIfBlank(defaultName, DEFAULT_PROFILE_NAME);
}

From source file:org.sonar.server.rule.DeprecatedRulesDefinition.java

@CheckForNull
private String ruleName(String repositoryKey, org.sonar.api.rules.Rule rule) {
    String name = i18n.getName(repositoryKey, rule.getKey());
    if (StringUtils.isNotBlank(name)) {
        return name;
    }/*from ww w  . j a  v  a2 s .c  o m*/
    return StringUtils.defaultIfBlank(rule.getName(), null);
}

From source file:org.sonar.server.rule.DeprecatedRulesDefinition.java

@CheckForNull
private String ruleDescription(String repositoryKey, org.sonar.api.rules.Rule rule) {
    String description = i18n.getDescription(repositoryKey, rule.getKey());
    if (StringUtils.isNotBlank(description)) {
        return description;
    }//from  w ww  . j  av a  2s .co m
    return StringUtils.defaultIfBlank(rule.getDescription(), null);
}

From source file:org.sonar.server.rule.DeprecatedRulesDefinition.java

@CheckForNull
private String paramDescription(String repositoryKey, String ruleKey, RuleParam param) {
    String desc = StringUtils.defaultIfEmpty(i18n.getParamDescription(repositoryKey, ruleKey, param.getKey()),
            param.getDescription());//from  w w w. j  a  v a 2  s  .co  m
    return StringUtils.defaultIfBlank(desc, null);
}

From source file:org.sonar.server.rule.RuleUpdate.java

/**
 * Set to <code>null</code> or blank to remove existing note.
 *//* w  w w . j a  va2 s.  c  o  m*/
public RuleUpdate setMarkdownNote(@Nullable String s) {
    this.markdownNote = s == null ? null : StringUtils.defaultIfBlank(s, null);
    this.changeMarkdownNote = true;
    return this;
}

From source file:org.sonar.server.search.SearchClient.java

@Override
public synchronized void start() {
    if (nativeClient == null) {
        ESLoggerFactory.setDefaultFactory(new Slf4jESLoggerFactory());
        org.elasticsearch.common.settings.Settings esSettings = ImmutableSettings.settingsBuilder()
                .put("node.name",
                        StringUtils.defaultIfEmpty(settings.getString(ProcessProperties.CLUSTER_NODE_NAME),
                                "sq_local_client"))
                .put("network.bind_host",
                        StringUtils.defaultIfEmpty(settings.getString(ProcessProperties.SEARCH_HOST),
                                "localhost"))
                .put("node.rack_id",
                        StringUtils.defaultIfEmpty(settings.getString(ProcessProperties.CLUSTER_NODE_NAME),
                                "unknown"))
                .put("cluster.name", StringUtils
                        .defaultIfBlank(settings.getString(ProcessProperties.CLUSTER_NAME), "sonarqube"))
                .build();//from www.j a v  a  2s.  c  o m
        nativeClient = new TransportClient(esSettings);
        ((TransportClient) nativeClient).addTransportAddress(new InetSocketTransportAddress(
                StringUtils.defaultIfEmpty(settings.getString(ProcessProperties.SEARCH_HOST),
                        LoopbackAddress.get().getHostAddress()),
                settings.getInt(ProcessProperties.SEARCH_PORT)));
    }
}

From source file:org.sonar.server.startup.RegisterNewProfiles.java

private static String defaultProfileName(List<RulesProfile> profiles) {
    String defaultName = null;//from   w  w  w  . j  a  v a2 s .c  o m
    boolean hasSonarWay = false;

    for (RulesProfile profile : profiles) {
        if (profile.getDefaultProfile()) {
            defaultName = profile.getName();
        } else if (DEFAULT_PROFILE_NAME.equals(profile.getName())) {
            hasSonarWay = true;
        }
    }

    if (StringUtils.isBlank(defaultName) && !hasSonarWay && !profiles.isEmpty()) {
        defaultName = profiles.get(0).getName();
    }

    return StringUtils.defaultIfBlank(defaultName, DEFAULT_PROFILE_NAME);
}

From source file:org.sonarqube.tests.performance.MavenLogs.java

/**
 * Total time: 6.015s/*from   w w w  . j  av a  2  s .  c  om*/
 * Total time: 3:14.025s
 */
public static Long extractTotalTime(String logs) {
    Pattern pattern = Pattern.compile("^.*Total time: (\\d*:)?(\\d+).(\\d+)s.*$", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(logs);
    if (matcher.matches()) {
        String minutes = StringUtils.defaultIfBlank(StringUtils.removeEnd(matcher.group(1), ":"), "0");
        String seconds = StringUtils.defaultIfBlank(matcher.group(2), "0");
        String millis = StringUtils.defaultIfBlank(matcher.group(3), "0");

        return (Long.parseLong(minutes) * 60000) + (Long.parseLong(seconds) * 1000) + Long.parseLong(millis);
    }
    throw new IllegalStateException("Maven logs do not contain \"Total time\"");
}