Skip to content

Commit

Permalink
Fix JENKINS-17961
Browse files Browse the repository at this point in the history
  • Loading branch information
gboissinot committed Aug 3, 2013
1 parent f0dffd4 commit 3b58375
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -32,7 +32,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<xtrigger.lib.version>0.24</xtrigger.lib.version>
<xtrigger.lib.version>0.26</xtrigger.lib.version>
<jersey.client.version>1.9.1</jersey.client.version>
<json.path.version>0.5.5</json.path.version>
<jackson.mapper.as1.version>1.8.3</jackson.mapper.as1.version>
Expand Down
156 changes: 88 additions & 68 deletions src/main/java/org/jenkinsci/plugins/urltrigger/URLTrigger.java
Expand Up @@ -18,7 +18,6 @@
import hudson.util.FormValidation;
import hudson.util.Secret;
import hudson.util.SequentialExecutionQueue;
import hudson.util.StreamTaskListener;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
Expand Down Expand Up @@ -56,7 +55,6 @@
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;


Expand Down Expand Up @@ -174,7 +172,6 @@ public void writeLogTo(XMLOutput out) throws IOException {

}


private String getURLValue(URLTriggerEntry entry, Node node) throws XTriggerException {
String entryURL = entry.getUrl();
if (entryURL != null) {
Expand All @@ -192,6 +189,7 @@ private String getURLValue(URLTriggerEntry entry, Node node) throws XTriggerExce

@Override
protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTriggerException {

if (entries == null || entries.isEmpty()) {
log.info("No URLs to poll.");
return false;
Expand All @@ -208,37 +206,57 @@ protected boolean checkIfModified(Node pollingNode, XTriggerLog log) throws XTri
}

private boolean checkIfModifiedEntry(URLTriggerEntry entry, Node pollingNode, XTriggerLog log) throws XTriggerException {
URLResponse response;
if (entry.isHttp()) {
Client client = getClientObject(entry, log);
String url = getURLValue(entry, pollingNode);
log.info(String.format("Invoking the url: \n %s", url));
ClientResponse clientResponse = client.resource(url).get(ClientResponse.class);

if (isServiceUnavailableAndNotExpected(clientResponse, entry)) {
log.info("URL to poll unavailable.");
log.info("Skipping URLTrigger initialization. Waiting next schedule");
return false;
}
String resolvedURL = getURLValue(entry, pollingNode);
URLTriggerResolvedEntry resolvedEntry = new URLTriggerResolvedEntry(resolvedURL, entry);

response = new HTTPResponse(clientResponse);
} else {
try {
response = getFTPResponse(getFTPClientObject(entry), entry);
if (response == null) {
return false;
}
log.info("FTP poll result: " + response.getEntityTagValue());
} catch (Exception ex) {
log.info("Failed to poll URL: " + ex.toString());
log.info("Skipping URLTrigger initialization. Waiting next schedule");
if (!resolvedEntry.isURLTriggerValidURL())
throw new IllegalArgumentException("Only http(s) and ftp URLs are supported. For non-http/ftp protocols, consider other XTrigger plugins");

if (resolvedEntry.isHttp() || resolvedEntry.isHttps()) {
return checkIfModifiedEntryForHttpOrHttpsURL(resolvedEntry, log);
}

return checkIfModifiedEntryFoFTPURL(resolvedEntry, log);
}

private boolean checkIfModifiedEntryForHttpOrHttpsURL(URLTriggerResolvedEntry resolvedEntry, XTriggerLog log) throws XTriggerException {

Client client = getClientObject(resolvedEntry, log);

String url = resolvedEntry.getResolvedURL();
log.info(String.format("Invoking the url: \n %s", url));
ClientResponse clientResponse = client.resource(url).get(ClientResponse.class);

URLTriggerEntry entry = resolvedEntry.getEntry();

if (isServiceUnavailableAndNotExpected(clientResponse, entry)) {
log.info("URL to poll unavailable.");
log.info("Skipping URLTrigger initialization. Waiting next schedule");
return false;
}

URLResponse response = new HTTPResponse(clientResponse);
URLTriggerService urlTriggerService = URLTriggerService.getInstance();
return urlTriggerService.isSchedulingAndGetRefresh(response, entry, log);

}

private boolean checkIfModifiedEntryFoFTPURL(URLTriggerResolvedEntry resolvedEntry, XTriggerLog log) throws XTriggerException {
URLResponse response;
try {
response = getFTPResponse(resolvedEntry);
if (response == null) {
return false;
}
log.info("FTP poll result: " + response.getEntityTagValue());
} catch (Exception ex) {
log.info("Failed to poll URL: " + ex.toString());
log.info("Skipping URLTrigger initialization. Waiting next schedule");
return false;
}

URLTriggerService urlTriggerService = URLTriggerService.getInstance();
return urlTriggerService.isSchedulingAndGetRefresh(response, resolvedEntry.getEntry(), log);

return urlTriggerService.isSchedulingAndGetRefresh(response, entry, log);
}

private boolean isServiceUnavailableAndNotExpected(ClientResponse clientResponse, URLTriggerEntry entry) {
Expand All @@ -256,9 +274,10 @@ protected String getName() {
return URLTriggerCause.NAME;
}

private Client getClientObject(URLTriggerEntry entry, XTriggerLog log) throws XTriggerException {
private Client getClientObject(URLTriggerResolvedEntry resolvedEntry, XTriggerLog log) throws XTriggerException {

Client client = createClient(isHttps(entry.getUrl()), entry.isProxyActivated());
URLTriggerEntry entry = resolvedEntry.getEntry();
Client client = createClient(resolvedEntry.isHttps(), entry.isProxyActivated());
if (isAuthBasic(entry)) {
addBasicAuth(entry, log, client);
}
Expand All @@ -273,10 +292,6 @@ private Client getClientObject(URLTriggerEntry entry, XTriggerLog log) throws XT
return client;
}

private boolean isHttps(String url) {
return url != null && url.startsWith("https");
}

private Client createClient(boolean isHttps, boolean withProxy) throws XTriggerException {
Client client;
if (withProxy) {
Expand Down Expand Up @@ -395,45 +410,46 @@ protected boolean requiresWorkspaceForPolling() {

@Override
protected void start(Node node, BuildableItem buildableItem, boolean newInstance, XTriggerLog log) {
URLTriggerService service = URLTriggerService.getInstance();
try {
for (URLTriggerEntry entry : entries) {
String url = getURLValue(entry, null);
if (!entry.isHttp() && !entry.isFtp())
throw new IllegalArgumentException("Only http(s) and ftp URLs are supported. For non-http/ftp protocols, consider other XTrigger plugins");

URLResponse response;
if (entry.isHttp()) {
Client client = getClientObject(entry, null);
ClientResponse clientResponse = client.resource(url).get(ClientResponse.class);
if (HttpServletResponse.SC_SERVICE_UNAVAILABLE == clientResponse.getStatus()) {
log.info("URL to poll unavailable.");
log.info("Skipping URLTrigger initialization. Waiting for next schedule.");
return;
}
response = new HTTPResponse(clientResponse);
} else {
response = getFTPResponse(getFTPClientObject(entry), entry);
if (response == null) {
return;
}
log.info("FTP poll result: " + response.getEntityTagValue());
}
service.initContent(response, entry, new XTriggerLog((StreamTaskListener) TaskListener.NULL));
}
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Severe error on trigger startup " + t.getMessage());
t.printStackTrace();
}
// URLTriggerService service = URLTriggerService.getInstance();
// try {
// for (URLTriggerEntry entry : entries) {
// String url = getURLValue(entry, node);
// if (!entry.isHttp() && !entry.isFtp())
// throw new IllegalArgumentException("Only http(s) and ftp URLs are supported. For non-http/ftp protocols, consider other XTrigger plugins");
//
// URLResponse response;
// if (entry.isHttp()) {
// Client client = getClientObject(entry, null);
// ClientResponse clientResponse = client.resource(url).get(ClientResponse.class);
// if (HttpServletResponse.SC_SERVICE_UNAVAILABLE == clientResponse.getStatus()) {
// log.info("URL to poll unavailable.");
// log.info("Skipping URLTrigger initialization. Waiting for next schedule.");
// return;
// }
// response = new HTTPResponse(clientResponse);
// } else {
// response = getFTPResponse(getFTPClientObject(entry), entry);
// if (response == null) {
// return;
// }
// log.info("FTP poll result: " + response.getEntityTagValue());
// }
// service.initContent(response, entry, new XTriggerLog((StreamTaskListener) TaskListener.NULL));
// }
// } catch (Throwable t) {
// LOGGER.log(Level.SEVERE, "Severe error on trigger startup " + t.getMessage());
// t.printStackTrace();
// }
}

@Override
public URLTriggerDescriptor getDescriptor() {
return (URLTriggerDescriptor) Hudson.getInstance().getDescriptorOrDie(getClass());
}

private static FTPClient getFTPClientObject(URLTriggerEntry entry) throws URISyntaxException, IOException {
return getFTPClientObject(entry.getUrl(), entry.getUsername(), entry.getRealPassword());
private static FTPClient getFTPClientObject(URLTriggerResolvedEntry resolvedEntry) throws URISyntaxException, IOException {
URLTriggerEntry entry = resolvedEntry.getEntry();
return getFTPClientObject(resolvedEntry.getResolvedURL(), entry.getUsername(), entry.getRealPassword());
}

private static FTPClient getFTPClientObject(String url, String basicUsername, String basicPassword) throws URISyntaxException, IOException {
Expand Down Expand Up @@ -469,8 +485,11 @@ private static FTPClient getFTPClientObject(String url, String basicUsername, St
return ftpClient;
}

private FTPResponse getFTPResponse(FTPClient ftpClient, URLTriggerEntry entry) throws IOException, ParseException, URISyntaxException {
URI uri = new URI(entry.getUrl());
private FTPResponse getFTPResponse(URLTriggerResolvedEntry resolvedEntry) throws IOException, ParseException, URISyntaxException {

FTPClient ftpClient = getFTPClientObject(resolvedEntry);

URI uri = new URI(resolvedEntry.getResolvedURL());
FTPResponse response = new FTPResponse();
String timeResponse = ftpClient.getModificationTime(uri.getPath());
if (timeResponse == null) {
Expand All @@ -485,6 +504,7 @@ private FTPResponse getFTPResponse(FTPClient ftpClient, URLTriggerEntry entry) t
response.setStatus(Integer.parseInt(dateResponse[0]));
response.setEntityTagValue(timeResponse); // I'm not sure what else could be used

URLTriggerEntry entry = resolvedEntry.getEntry();
if (entry.isInspectingContent()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ftpClient.retrieveFile(uri.getPath(), baos);
Expand Down
Expand Up @@ -149,4 +149,8 @@ public boolean isFtp() {
public boolean isHttp() {
return url.startsWith("http");
}

public boolean isHttps() {
return url.startsWith("https");
}
}
@@ -0,0 +1,49 @@
package org.jenkinsci.plugins.urltrigger;

/**
* @author Gregory Boissinot
*/
public class URLTriggerResolvedEntry {

private String resolvedURL;

private URLTriggerEntry entry;

public URLTriggerResolvedEntry(String resolvedURL, URLTriggerEntry entry) {
if (resolvedURL == null) {
throw new NullPointerException("A resolved URL is required.");
}
this.resolvedURL = resolvedURL;

if (entry == null) {
throw new NullPointerException("An entry object is required.");
}
this.entry = entry;
}

public String getResolvedURL() {
return resolvedURL;
}

public URLTriggerEntry getEntry() {
return entry;
}

public boolean isURLTriggerValidURL() {
return isHttp()
|| isHttps()
|| isFtp();
}

public boolean isHttp() {
return resolvedURL.startsWith("http");
}

public boolean isHttps() {
return resolvedURL.startsWith("https");
}

public boolean isFtp() {
return resolvedURL.startsWith("ftp");
}
}

0 comments on commit 3b58375

Please sign in to comment.