Skip to content

Commit 574379a

Browse files
Centralize logic to check for invalid URLs
1 parent d8e288a commit 574379a

4 files changed

Lines changed: 15 additions & 15 deletions

File tree

src/main/java/com/codedx/bambooplugin/CodeDxScanTask.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import java.io.File;
1818
import java.io.IOException;
19-
import java.net.MalformedURLException;
20-
import java.net.URL;
2119
import java.nio.file.Path;
2220
import java.nio.file.Paths;
2321
import java.util.ArrayList;
@@ -148,9 +146,7 @@ private static Boolean loadUserPreferences(ScanTaskState state) {
148146
logError(state, "Missing Code Dx URL. Please configure it on the task configuration page.");
149147
return false;
150148
} else {
151-
try {
152-
new URL(state.apiUrl);
153-
} catch (MalformedURLException e) {
149+
if (!ServerConfigManager.isURLValid(state.apiUrl)) {
154150
logError(state, "Malformed Code Dx URL. Please correct it on the task configuration page.");
155151
return false;
156152
}

src/main/java/com/codedx/bambooplugin/CodeDxScanTaskConfigurator.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.apache.commons.lang.StringUtils;
1212

1313
import javax.ws.rs.ProcessingException;
14-
import java.net.MalformedURLException;
15-
import java.net.URL;
1614
import java.util.ArrayList;
1715
import java.util.List;
1816
import java.util.Map;
@@ -114,9 +112,7 @@ public void validate(final ActionParametersMap params, final ErrorCollection err
114112
final String url = params.getString("url");
115113
if (url != null && !url.isEmpty()) {
116114
// Make sure the URL is valid
117-
try {
118-
new URL(url);
119-
} catch (MalformedURLException e) {
115+
if (!ServerConfigManager.isURLValid(url)) {
120116
errorCollection.addError("url", "Malformed URL");
121117
}
122118
} else {

src/main/java/com/codedx/bambooplugin/ProjectRefresherServlet.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import javax.ws.rs.ProcessingException;
1515
import java.io.IOException;
1616
import java.net.ConnectException;
17-
import java.net.MalformedURLException;
18-
import java.net.URL;
1917
import java.net.UnknownHostException;
2018

2119
public class ProjectRefresherServlet extends HttpServlet {
@@ -61,9 +59,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
6159
CodeDxCredentials credentials = mapper.readValue(req.getInputStream(), CodeDxCredentials.class);
6260

6361
// Make sure the URL is valid
64-
try {
65-
new URL(credentials.getUrl());
66-
} catch (MalformedURLException e) {
62+
if (!ServerConfigManager.isURLValid(credentials.getUrl())) {
6763
resp.setStatus(404);
6864
resp.getOutputStream().print("Malformed Code Dx URL");
6965
return;

src/main/java/com/codedx/bambooplugin/ServerConfigManager.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import javax.ws.rs.client.Client;
1111
import javax.ws.rs.client.ClientBuilder;
1212
import java.io.Serializable;
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
1315

1416
public class ServerConfigManager implements Serializable {
1517

@@ -98,6 +100,16 @@ public static boolean getDefaultsSet() {
98100
return false;
99101
}
100102

103+
// Helper method to tell if a url is formatted correctly
104+
public static boolean isURLValid(String url) {
105+
try {
106+
new URL(url);
107+
} catch (MalformedURLException e) {
108+
return false;
109+
}
110+
return true;
111+
}
112+
101113
// Fields we want to save
102114
private static class GlobalData implements Serializable {
103115
public String url;

0 commit comments

Comments
 (0)