Skip to content

Commit 8f82b64

Browse files
Merge pull request #17 from bluestealth/master
Assign server response outside try block
2 parents de04d12 + 9e0ab57 commit 8f82b64

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

build.gradle

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313

1414
apply plugin: 'java'
15-
apply plugin: 'fatjar'
1615
apply plugin: 'maven'
1716
apply plugin: 'signing'
17+
apply plugin: 'com.github.johnrengelman.shadow'
1818

1919
group = 'com.sendgrid'
2020
version = '4.0.0'
@@ -37,10 +37,10 @@ task wrapper(type: Wrapper) {
3737

3838
buildscript {
3939
dependencies {
40-
classpath 'eu.appsatori:gradle-fatjar-plugin:0.1.2' // adds fatJar task
40+
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.4'
4141
}
4242
repositories {
43-
mavenCentral()
43+
jcenter()
4444
}
4545
}
4646

@@ -64,33 +64,36 @@ allprojects {
6464
}
6565
}
6666

67-
// adds 'with-dependencies' to the fatJar name
68-
fatJar {
67+
// adds 'with-dependencies' to the shadowJar name
68+
shadowJar {
6969
classifier 'jar'
7070
baseName "sendgrid-java-http-client"
71+
version version
72+
}
73+
jar {
7174
manifest {
7275
attributes("Implementation-Title": "http", "Implementation-Version": version)
7376
}
7477
}
7578

76-
// copy fatJar to base project directory so they will be in git (and on github for download)
79+
// copy shadowJar to base project directory so they will be in git (and on github for download)
7780
build << {
7881
copy {
79-
println "Copying ${fatJar.archiveName} to $projectDir/repo/com/sendgrid/$version"
80-
from("$buildDir/libs/${fatJar.archiveName}")
82+
println "Copying ${shadowJar.archiveName} to $projectDir/repo/com/sendgrid/$version"
83+
from("$buildDir/libs/${shadowJar.archiveName}")
8184
into("$projectDir/repo/com/sendgrid/$version")
8285
}
8386
copy {
84-
println "Copying ${fatJar.archiveName} to $projectDir/repo/com/sendgrid"
85-
from("$buildDir/libs/${fatJar.archiveName}")
87+
println "Copying ${shadowJar.archiveName} to $projectDir/repo/com/sendgrid"
88+
from("$buildDir/libs/${shadowJar.archiveName}")
8689
into("$projectDir/repo/com/sendgrid")
8790
}
8891
tasks.renameSendGridVersionJarToSendGridJar.execute()
8992
}
9093

9194
task renameSendGridVersionJarToSendGridJar {
9295
doLast {
93-
file("$projectDir/repo/com/sendgrid/${fatJar.archiveName}").renameTo(file("$projectDir/repo/com/sendgrid/http-jar.jar"))
96+
file("$projectDir/repo/com/sendgrid/${shadowJar.archiveName}").renameTo(file("$projectDir/repo/com/sendgrid/http-jar.jar"))
9497
}
9598
}
9699

@@ -149,8 +152,8 @@ uploadArchives {
149152
}
150153

151154
artifacts {
152-
archives fatJar
155+
archives shadowJar
153156
archives jar
154157
archives javadocJar
155158
archives sourcesJar
156-
}
159+
}

src/main/java/com/sendgrid/Client.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.http.Header;
1313
import org.apache.http.StatusLine;
1414
import org.apache.http.annotation.NotThreadSafe;
15+
import org.apache.http.client.ClientProtocolException;
1516
import org.apache.http.client.ResponseHandler;
1617
import org.apache.http.client.methods.CloseableHttpResponse;
1718
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
@@ -277,23 +278,21 @@ public Response delete(Request request) throws URISyntaxException, IOException {
277278
}
278279

279280
private Response executeApiCall(HttpRequestBase httpPost) throws IOException {
280-
CloseableHttpResponse serverResponse = null;
281-
Response response = new Response();
282281
try {
283-
serverResponse = httpClient.execute(httpPost);
284-
response = getResponse(serverResponse);
285-
final StatusLine statusLine = serverResponse.getStatusLine();
286-
if(statusLine.getStatusCode()>=300){
287-
//throwing IOException here to not break API behavior.
288-
throw new IOException("Request returned status Code "+statusLine.getStatusCode()+"Body:"+(response!=null?response.getBody():null));
289-
}
290-
291-
} finally {
292-
if (serverResponse != null) {
282+
CloseableHttpResponse serverResponse = httpClient.execute(httpPost);
283+
try {
284+
Response response = getResponse(serverResponse);
285+
if(response.getStatusCode() >= 300) {
286+
//throwing IOException here to not break API behavior.
287+
throw new IOException("Request returned status Code "+response.getStatusCode()+"Body:"+response.getBody());
288+
}
289+
return response;
290+
} finally {
293291
serverResponse.close();
294292
}
293+
} catch(ClientProtocolException e) {
294+
throw new IOException(e.getMessage());
295295
}
296-
return response;
297296
}
298297

299298
/**
@@ -326,4 +325,15 @@ public Response api(Request request) throws IOException {
326325
throw new IOException(errors.toString());
327326
}
328327
}
329-
}
328+
329+
@Override
330+
public void finalize() throws Throwable {
331+
try {
332+
this.httpClient.close();
333+
} catch(IOException e) {
334+
throw new Throwable(e.getMessage());
335+
} finally {
336+
super.finalize();
337+
}
338+
}
339+
}

0 commit comments

Comments
 (0)