Skip to content

Commit

Permalink
On Level 4
Browse files Browse the repository at this point in the history
  • Loading branch information
SNanda8895 committed Jul 30, 2024
1 parent 7da5f65 commit bd1c6cf
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/main/java/io/jenkins/plugins/sample/SampleConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

/**
* Example of Jenkins global configuration.
*/
@Extension
public class SampleConfiguration extends GlobalConfiguration {

/** @return the singleton instance */
/**
* @return the singleton instance
*/
public static SampleConfiguration get() {
return ExtensionList.lookupSingleton(SampleConfiguration.class);
}
Expand Down Expand Up @@ -48,7 +56,6 @@ public void setPassword(Secret password) {
}



public String getUserName() {
return userName;
}
Expand All @@ -60,7 +67,6 @@ public void setUserName(String userName) {
}



public String getUrl() {
return url;
}
Expand All @@ -75,7 +81,7 @@ public String getDescription() {
return description;
}

@DataBoundSetter
@DataBoundSetter
public void setDescription(String description) {
this.description = description;
save();
Expand All @@ -86,13 +92,16 @@ public SampleConfiguration() {
load();
}

/** @return the currently configured label, if any */
/**
* @return the currently configured label, if any
*/
public String getLabel() {
return label;
}

/**
* Together with {@link #getLabel}, binds to entry in {@code config.jelly}.
*
* @param label the new value of this field
*/
@DataBoundSetter
Expand All @@ -104,24 +113,53 @@ public void setLabel(String label) {
public FormValidation doCheckLabel(@QueryParameter String value) {
if (StringUtils.isEmpty(value)) {
return FormValidation.warning("Please specify a label.");
}if (!value.matches("[a-zA-Z ]+")) {
}
if (!value.matches("[a-zA-Z ]+")) {
return FormValidation.warning("Name can only contain letters and spaces.");
}
return FormValidation.ok();
}

public FormValidation doCheckDescription(@QueryParameter String value) {
if (StringUtils.isEmpty(value)) {
return FormValidation.warning("Please specify a description.");
}
return FormValidation.ok();
}

public FormValidation doCheckUserName(@QueryParameter String value) {
if(StringUtils.isEmpty(value)) {
if (StringUtils.isEmpty(value)) {
return FormValidation.warning("Please specify username");
} if (!value.matches("[a-zA-Z]+")) {
}
if (!value.matches("[a-zA-Z]+")) {
return FormValidation.warning("UserName can only contain letters.");
}
return FormValidation.ok();
}

}
public FormValidation doTestConnection(@QueryParameter("url") String url,
@QueryParameter("username") String username,
@QueryParameter("password") Secret password) throws MalformedURLException {

try {
URL obj = new URL(url);
HttpURLConnection connection =(HttpURLConnection) obj.openConnection();
connection.setRequestMethod("Get");
String auth = username + ":" + password.getPlainText();
String encodedAuth = java.util.Base64.getEncoder().encodeToString(auth.getBytes());
connection.setRequestProperty("Auth", "Basic" +encodedAuth);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
return FormValidation.ok("Successful");
}
else {
return FormValidation.error("Failed to connect " +responseCode);
}

} catch (IOException e) {
throw new RuntimeException("Not able to connect" + e.getMessage());
}


}
}

0 comments on commit bd1c6cf

Please sign in to comment.