-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make multiple attempts to contact Code Dx server
When running a build, previously a single failed attempted to contact the server would throw an exception and cause the build to fail. This will allow the plugin to try multiple times at different intervals before failing the build.
- Loading branch information
1 parent
1910d71
commit d934f2e
Showing
3 changed files
with
92 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/com/secdec/codedx/api/client/CodeDxRepeatingClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.secdec.codedx.api.client; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.ClientProtocolException; | ||
import org.apache.http.client.methods.HttpGet; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.lang.reflect.Type; | ||
import java.net.Socket; | ||
import java.net.SocketException; | ||
|
||
/** | ||
* Created by samuelj on 3/27/15. | ||
*/ | ||
public class CodeDxRepeatingClient extends CodeDxClient { | ||
private PrintStream logger; | ||
|
||
public CodeDxRepeatingClient(String url, String key, PrintStream logger) { | ||
super(url, key); | ||
this.logger = logger; | ||
} | ||
|
||
/** | ||
* A generic get that will marshal JSON data into some type. | ||
* @param path Append this to the URL | ||
* @param typeOfT | ||
* @param experimental If this request is part of the experimental API | ||
* @return Something of type T | ||
* @throws ClientProtocolException | ||
* @throws IOException | ||
* @throws CodeDxClientException | ||
*/ | ||
protected <T> T doGet(String path, Type typeOfT, boolean experimental) throws IOException, CodeDxClientException { | ||
try { | ||
int fails = 0; | ||
while (fails < 3) { | ||
try { | ||
return super.doGet(path, typeOfT, experimental); | ||
} catch (CodeDxClientException clientException) { | ||
fails++; | ||
logger.println("Attempt " + fails + " " + clientException.getMessage() + " response code: " + clientException.getHttpCode()); | ||
switch (fails) { | ||
case 1: | ||
logger.println("Trying again after 1 second"); | ||
Thread.sleep(1000); | ||
break; | ||
case 2: | ||
logger.println("Trying again after 5 seconds"); | ||
Thread.sleep(5000); | ||
break; | ||
case 3: | ||
logger.println("Trying again after 30 seconds"); | ||
Thread.sleep(30000); | ||
break; | ||
default: | ||
throw clientException; | ||
} | ||
} catch (SocketException socketException) { | ||
fails++; | ||
logger.println("Attempt " + fails + " " + socketException.getMessage()); | ||
switch (fails) { | ||
case 1: | ||
logger.println("Trying again after 1 second"); | ||
Thread.sleep(1000); | ||
break; | ||
case 2: | ||
logger.println("Trying again after 5 seconds"); | ||
Thread.sleep(5000); | ||
break; | ||
case 3: | ||
logger.println("Trying again after 30 seconds"); | ||
Thread.sleep(30000); | ||
break; | ||
default: | ||
throw socketException; | ||
} | ||
} | ||
|
||
} | ||
} catch (InterruptedException i) { | ||
logger.println("Thread was interrupted while waiting to re-attempt GET"); | ||
throw new CodeDxClientException("Thread was interrupted. Unabled to finish GET", -1); | ||
} | ||
//This shouldn't happen, but we all know how that assumption turns out. | ||
throw new CodeDxClientException("GET was unsuccessful for " + path, -1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters