Skip to content
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
30 changes: 12 additions & 18 deletions src/main/java/org/fanout/pubcontrol/PubControlClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.*;
import java.io.UnsupportedEncodingException;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;
import java.io.*;

import java.security.Key;
Expand Down Expand Up @@ -249,16 +248,13 @@ private void pubCall(String uri, String authHeader,
*/
private void makeHttpRequest(URL url, String authHeader,
String jsonContent) throws PublishFailedException {
URLConnection connection = null;
HttpURLConnection connection = null;
int responseCode = 0;
StringBuilder response = new StringBuilder();
byte[] body = jsonContent.getBytes(StandardCharsets.UTF_8);
try {
connection = url.openConnection();
if (connection instanceof HttpURLConnection)
((HttpURLConnection)connection).setRequestMethod("POST");
else
((HttpsURLConnection)connection).setRequestMethod("POST");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
if (authHeader != null)
connection.setRequestProperty("Authorization", authHeader);
connection.setRequestProperty("Content-Type",
Expand All @@ -273,7 +269,14 @@ private void makeHttpRequest(URL url, String authHeader,
dataOutputStream.write(body);
dataOutputStream.close();

InputStream inputStream = connection.getInputStream();
responseCode = connection.getResponseCode();

InputStream inputStream;
if (responseCode >= 200 && responseCode < 300)
inputStream = connection.getInputStream();
else
inputStream = connection.getErrorStream();

BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
Expand All @@ -282,21 +285,12 @@ private void makeHttpRequest(URL url, String authHeader,
response.append('\r');
}
bufferedReader.close();
if (connection instanceof HttpURLConnection)
responseCode = ((HttpURLConnection)connection).getResponseCode();
else
responseCode = ((HttpsURLConnection)connection).getResponseCode();
} catch (Exception exception) {
throw new PublishFailedException("failed to publish: " +
exception.getMessage());
} finally {
if (connection != null)
{
if (connection instanceof HttpURLConnection)
((HttpURLConnection)connection).disconnect();
else
((HttpsURLConnection)connection).disconnect();
}
connection.disconnect();
}
if (responseCode < 200 || responseCode >= 300)
throw new PublishFailedException("failed to publish: " +
Expand Down