Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Sometimes the InputStream is null, like with a 404 exception #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions src/main/java/com/exacttarget/fuelsdk/ETRestConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,6 @@ private HttpURLConnection sendRequest(String path, Method method, String payload
private HttpURLConnection sendRequest(URL url, Method method, String payload)
throws ETSdkException
{
Gson gson = client.getGson();

logger.debug(method + " " + url);
String[] token = url.getPath().split("/");
String object = "";
Expand Down Expand Up @@ -274,14 +272,8 @@ private HttpURLConnection sendRequest(URL url, Method method, String payload)
}

if (payload != null) {
if (logger.isDebugEnabled()) {
JsonParser jsonParser = new JsonParser();
String payloadPrettyPrinted =
gson.toJson(jsonParser.parse(payload));
for (String line : payloadPrettyPrinted.split("\\n")) {
logger.debug(line);
}
}
debugResponse(payload);

OutputStream os = null;
try {
os = connection.getOutputStream();
Expand Down Expand Up @@ -312,7 +304,6 @@ private HttpURLConnection sendRequest(URL url, Method method, String payload)
private String receiveResponse(HttpURLConnection connection)
throws ETSdkException
{
Gson gson = client.getGson();

InputStream is = null;
try {
Expand All @@ -326,33 +317,52 @@ private String receiveResponse(HttpURLConnection connection)
}

StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);

if (is != null)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try
{
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line);
}
}
} catch (IOException ex) {
throw new ETSdkException("error reading " + connection.getURL(), ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
throw new ETSdkException("error closing " + connection.getURL(), ex);
catch (IOException ex)
{
throw new ETSdkException("error reading " + connection.getURL(), ex);
}
finally
{
try
{
reader.close();
}
catch (IOException ex)
{
throw new ETSdkException("error closing " + connection.getURL(), ex);
}
}
}

String response = stringBuilder.toString();

debugResponse(response);

return response;
}

private void debugResponse(String response)
{
if (logger.isDebugEnabled()) {
Gson gson = client.getGson();
JsonParser jsonParser = new JsonParser();
String responsePrettyPrinted = gson.toJson(jsonParser.parse(response));
for (String line : responsePrettyPrinted.split("\\n")) {
logger.debug(line);
}
}

return response;
}

/**
Expand Down