Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/main/java/com/checkmarx/intellij/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
public final class Utils {

public static final String EMPTY = "";
private static final Logger LOGGER = getLogger(Utils.class);
private static final SimpleDateFormat input = new SimpleDateFormat(Constants.INPUT_DATE_FORMAT);
private static final SimpleDateFormat output = new SimpleDateFormat(Constants.OUTPUT_DATE_FORMAT);
Expand Down Expand Up @@ -114,24 +115,16 @@ public static String dateParser(String unformattedDate) {
}

public static void notify(Project project, String content, NotificationType type) {
new Notification(Constants.NOTIFICATION_GROUP_ID,
null,
null,
null,
content,
type,
NotificationListener.URL_OPENING_LISTENER)
NotificationGroupManager.getInstance()
.getNotificationGroup(Constants.NOTIFICATION_GROUP_ID)
.createNotification(content, type)
.notify(project);
}

public static void notifyScan(String title, String message, Project project, Runnable func, NotificationType notificationType, String actionText) {
Notification notification = new Notification(Constants.NOTIFICATION_GROUP_ID,
null,
title,
null,
message,
notificationType,
null);
Notification notification = NotificationGroupManager.getInstance()
.getNotificationGroup(Constants.NOTIFICATION_GROUP_ID)
.createNotification(title, message, notificationType);

if (func != null) {
notification.addAction(NotificationAction.createSimple(actionText, func));
Expand Down Expand Up @@ -342,4 +335,30 @@ public static void notifySessionExpired() {
public static boolean isFilterEnabled(Set<String> enabledFilterValues, String filterValue) {
return enabledFilterValues != null && !enabledFilterValues.isEmpty() && enabledFilterValues.contains(filterValue);
}

public static boolean isNotBlank(String str) {
return !isBlank(str);
}

public static int length(CharSequence cs) {
if (cs == null)
return 0;
else
return cs.length();
}

public static boolean isBlank(CharSequence cs) {
int strLen = length(cs);
if (strLen == 0) {
return true;
} else {
for(int i = 0; i < strLen; ++i) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.checkmarx.intellij.commands.results.obj.ResultGetState;
import com.checkmarx.intellij.settings.global.CxWrapperFactory;
import com.intellij.openapi.diagnostic.Logger;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -42,7 +41,7 @@ public class Results {
@NotNull
public static CompletableFuture<ResultGetState> getResults(String scanIdFieldValue) {
return CompletableFuture.supplyAsync(() -> {
boolean getLatest = StringUtils.isBlank(scanIdFieldValue);
boolean getLatest = Utils.isBlank(scanIdFieldValue);
ResultGetState newState = new ResultGetState();
String scanId;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -92,9 +91,9 @@ private void store() {
* @return error message if secret not present in secure storage, otherwise null
*/
private String validate(@NotNull GlobalSettingsState settingsState, @NotNull GlobalSettingsSensitiveState sensitiveState) {
if (settingsState.isApiKeyEnabled() && StringUtils.isBlank(sensitiveState.getApiKey())) {
if (settingsState.isApiKeyEnabled() && Utils.isBlank(sensitiveState.getApiKey())) {
return Bundle.missingFieldMessage(Resource.API_KEY);
} else if (!settingsState.isApiKeyEnabled() && (StringUtils.isBlank(sensitiveState.getRefreshToken())
} else if (!settingsState.isApiKeyEnabled() && (Utils.isBlank(sensitiveState.getRefreshToken())
|| isTokenExpired(settingsState.getRefreshTokenExpiry()))) {
return Bundle.missingFieldMessage(Resource.REFRESH_TOKEN);
}
Expand All @@ -107,7 +106,7 @@ private String validate(@NotNull GlobalSettingsState settingsState, @NotNull Glo
* @return true, if the refresh token is expired otherwise false
*/
public boolean isTokenExpired(String tokenExpiryString){
if (!StringUtils.isBlank(tokenExpiryString)){
if (!Utils.isBlank(tokenExpiryString)){
boolean isExpired = LocalDateTime.parse(tokenExpiryString).isBefore(LocalDateTime.now());
LOGGER.warn("Refresh Token Expired: "+isExpired);
return isExpired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.intellij.util.ui.JBUI;
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
Expand Down Expand Up @@ -310,7 +309,7 @@ private void triggerDrawResultsTree(String scanIdValue, boolean overrideSelectio

currentState = new ResultGetState();

if (!StringUtils.isBlank(scanIdValue) && !uuidPattern.matcher(scanIdValue).matches()) {
if (!Utils.isBlank(scanIdValue) && !uuidPattern.matcher(scanIdValue).matches()) {
currentState.setMessage(Bundle.message(Resource.INVALID_SCAN_ID));
updateDisplay();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;
Expand Down Expand Up @@ -44,7 +43,7 @@ public void run(@NotNull ProgressIndicator progressIndicator) {
LOGGER.info(Bundle.message(Resource.SCAN_CANCELING_INFO, scanId));
Scan.scanCancel(scanId);
LOGGER.info(Bundle.message(Resource.SCAN_CANCELED, scanId));
propertiesComponent.setValue(Constants.RUNNING_SCAN_ID_PROPERTY, StringUtils.EMPTY);
propertiesComponent.setValue(Constants.RUNNING_SCAN_ID_PROPERTY, Utils.EMPTY);
ActivityTracker.getInstance().inc();
Utils.notifyScan(null, Bundle.message(Resource.SCAN_CANCELED_SUCCESSFULLY), e.getProject(), null, NotificationType.INFORMATION, null);
}
Expand All @@ -59,7 +58,7 @@ public void update(@NotNull AnActionEvent e) {
e.getPresentation().setVisible(StartScanAction.getUserHasPermissionsToScan());

PropertiesComponent propertiesComponent = PropertiesComponent.getInstance(Objects.requireNonNull(e.getProject()));
boolean isScanRunning = StringUtils.isNotBlank(propertiesComponent.getValue(Constants.RUNNING_SCAN_ID_PROPERTY));
boolean isScanRunning = Utils.isNotBlank(propertiesComponent.getValue(Constants.RUNNING_SCAN_ID_PROPERTY));
e.getPresentation().setEnabled(isScanRunning);
}
catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import lombok.Setter;
import lombok.SneakyThrows;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -89,7 +88,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
boolean matchProject = isAstProjectMatchesWorkspaceProject();
// Case it is a git repo check for project and branch match
if (repository != null) {
String storedBranch = Optional.ofNullable(propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY)).orElse(StringUtils.EMPTY);
String storedBranch = Optional.ofNullable(propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY)).orElse(Utils.EMPTY);
if(storedBranch.equals(Constants.USE_LOCAL_BRANCH)) {
storedBranch = getActiveBranch(workspaceProject);
}
Expand Down Expand Up @@ -125,7 +124,7 @@ private boolean isAstProjectMatchesWorkspaceProject() {
String workspaceProjectName = getRepositoryProjectName();

// Return true if the selected project matches the expected project name
return StringUtils.isNotBlank(pluginProjectName) &&
return Utils.isNotBlank(pluginProjectName) &&
workspaceProjectName != null &&
pluginProjectName.equals(workspaceProjectName);
}
Expand Down Expand Up @@ -260,7 +259,7 @@ private Runnable pollingScan(String scanId) {
LOGGER.info(msg(Resource.SCAN_RUNNING, scanId));
} else {
LOGGER.info(msg(Resource.SCAN_FINISHED, scan.getStatus().toLowerCase()));
propertiesComponent.setValue(Constants.RUNNING_SCAN_ID_PROPERTY, StringUtils.EMPTY);
propertiesComponent.setValue(Constants.RUNNING_SCAN_ID_PROPERTY, Utils.EMPTY);
ActivityTracker.getInstance().inc();
pollScanExecutor.shutdown();

Expand Down Expand Up @@ -297,11 +296,11 @@ public void update(@NotNull AnActionEvent e) {
cxToolWindowPanel = getCxToolWindowPanel(e);
workspaceProject = e.getProject();
propertiesComponent = PropertiesComponent.getInstance(Objects.requireNonNull(workspaceProject));
boolean isScanRunning = StringUtils.isNotBlank(propertiesComponent.getValue(Constants.RUNNING_SCAN_ID_PROPERTY));
boolean isScanRunning = Utils.isNotBlank(propertiesComponent.getValue(Constants.RUNNING_SCAN_ID_PROPERTY));
String storedProject = propertiesComponent.getValue(Constants.SELECTED_PROJECT_PROPERTY);
String storedBranch = propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY);

boolean projectAndBranchSelected = StringUtils.isNotBlank(storedProject) && StringUtils.isNotBlank(storedBranch);
boolean projectAndBranchSelected = Utils.isNotBlank(storedProject) && Utils.isNotBlank(storedBranch);

// Check if IDE was restarted and there's a scan still running
if (isScanRunning && !isPollingScan && !actionInitialized) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.BranchChangeListener;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -46,7 +45,7 @@ public BranchSelectionGroup(@NotNull Project project,
String storedBranch = propertiesComponent.getValue(Constants.SELECTED_BRANCH_PROPERTY);
return Bundle.message(Resource.BRANCH_SELECT_PREFIX)
+ ": "
+ (StringUtils.isBlank(storedBranch) ? setDefaultBranch() : storedBranch);
+ (Utils.isBlank(storedBranch) ? setDefaultBranch() : storedBranch);
}

private String setDefaultBranch() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -111,7 +110,7 @@ protected String getTitle() {
String storedProject = propertiesComponent.getValue(Constants.SELECTED_PROJECT_PROPERTY);
return Bundle.message(Resource.PROJECT_SELECT_PREFIX)
+ ": "
+ (StringUtils.isBlank(storedProject) ? NONE_SELECTED : storedProject);
+ (Utils.isBlank(storedProject) ? NONE_SELECTED : storedProject);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand All @@ -41,7 +40,7 @@ public class ScanSelectionGroup extends BaseSelectionGroup {
public ScanSelectionGroup(@NotNull Project project) {
super(project);
String storedValue = propertiesComponent.getValue(Constants.SELECTED_SCAN_PROPERTY);
if (StringUtils.isNotBlank(storedValue)) {
if (Utils.isNotBlank(storedValue)) {
ApplicationManager.getApplication().invokeLater(() -> Optional.ofNullable(getCxToolWindowPanel(project))
.ifPresent(cxToolWindowPanel -> cxToolWindowPanel.selectScan(
unFormatScan(storedValue))));
Expand Down Expand Up @@ -70,7 +69,7 @@ public void refresh(String projectId, String branch, Boolean selectLatestScan) {
removeAll();
CompletableFuture.supplyAsync((Supplier<List<com.checkmarx.ast.scan.Scan>>) () -> {
try {
return StringUtils.isBlank(projectId) || StringUtils.isBlank(branch)
return Utils.isBlank(projectId) || Utils.isBlank(branch)
? Collections.emptyList()
: Scan.getList(projectId, branch);
} catch (IOException | URISyntaxException | InterruptedException | CxException e) {
Expand Down Expand Up @@ -102,7 +101,7 @@ protected String getTitle() {
return Bundle.message(Resource.SCAN_SELECT_PREFIX) + ": " + (isEnabled() ? NONE_SELECTED : "...");
}
String storedScan = propertiesComponent.getValue(Constants.SELECTED_SCAN_PROPERTY);
return Bundle.message(Resource.SCAN_SELECT_PREFIX) + ": " + (StringUtils.isBlank(storedScan)
return Bundle.message(Resource.SCAN_SELECT_PREFIX) + ": " + (Utils.isBlank(storedScan)
? NONE_SELECTED
: storedScan);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.intellij.ui.hover.TreeHoverListener;
import com.intellij.ui.tree.ui.DefaultTreeUI;
import com.intellij.ui.treeStructure.Tree;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.tree.DefaultMutableTreeNode;
Expand Down Expand Up @@ -110,7 +109,7 @@ private static void addResultToEngine(Project project,
for (GroupBy groupBy : groupByList) {
NonLeafNode child = null;
String childKey = groupBy.getFunction().apply(result);
if (StringUtils.isBlank(childKey)) {
if (Utils.isBlank(childKey)) {
continue;
}
// search for the child node
Expand Down
Loading
Loading