import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConnectToGnpyServer {
private static final Logger LOG = LoggerFactory.getLogger(ConnectToGnpyServer.class);
- static final String URL_GNPY = "http://127.0.0.1:5000/gnpy/api/v1.0/files";
+ static final String URL_GNPY = "http://127.0.0.1:8008/gnpy/api/v1.0/files";
- public String gnpyCnx(String jsonTxt) {
+ public String gnpyCnx(String jsonTxt) throws GnpyException {
String jsonRespTxt = null;
+
try {
URL url = new URL(URL_GNPY);
String userCredentials = "gnpy:gnpy";
os.write(jsonTxt.getBytes());
os.flush();
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
- throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
+ throw new GnpyException(String.format(
+ "In connectToGnpyServer: could not connect to GNPy - response code: %s",conn.getResponseCode()));
}
InputStreamReader response = new InputStreamReader((conn.getInputStream()));
if (response != null) {
- jsonRespTxt = null;
- try {
- jsonRespTxt = CharStreams.toString(response);
- } catch (IOException e1) {
- LOG.warn("Could not read characters of GNPy response: {}", e1.getMessage());
- }
+ jsonRespTxt = CharStreams.toString(response);
}
conn.disconnect();
- } catch (MalformedURLException e) {
- LOG.warn("Exception : Malformed GNPy URL");
} catch (IOException e) {
- LOG.warn("IOException when connecting to GNPy server: {}", e.getMessage());
+ throw new GnpyException("In connectToGnpyServer: excpetion",e);
}
return jsonRespTxt;
}
conn.setRequestProperty("Content-Type", "application/json");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
+ LOG.info("In connectToGnpyServer: Gnpy instance is connected to T-PCE");
exist = true;
}
conn.disconnect();
}
catch (IOException e) {
- LOG.warn("Could not connect to GNPy server");
+ LOG.warn("In connectToGnpyserver: could not connect to GNPy server {}",e.getMessage());
+ return exist;
}
return exist;
}
- public String readResponse(InputStreamReader response) {
+ public String readResponse(InputStreamReader response) throws GnpyException {
String output = null;
BufferedReader br = new BufferedReader(response);
String line;
}
output = sb.toString();
} catch (IOException e) {
- LOG.warn("IOException when reading GNPy response: {}", e.getMessage());
+ throw new GnpyException("In connectToGnpyserver: could not read response",e);
}
return output;
}