package jalview.ext.pymol;
-import jalview.bin.Cache;
-import jalview.gui.Preferences;
-import jalview.structure.StructureCommand;
-import jalview.structure.StructureCommandI;
-
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
+import java.net.HttpURLConnection;
+import java.net.SocketException;
import java.net.URL;
-import java.net.URLConnection;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
+import jalview.bin.Cache;
+import jalview.gui.Preferences;
+import jalview.structure.StructureCommand;
+import jalview.structure.StructureCommandI;
+
public class PymolManager
{
private static final int RPC_REPLY_TIMEOUT_MS = 15000;
String rpcUrl = "http://127.0.0.1:" + this.pymolXmlRpcPort;
PrintWriter out = null;
BufferedReader in = null;
- List<String> result = new ArrayList<>();
+ List<String> result = getReply ? new ArrayList<>() : null;
+
try
{
URL realUrl = new URL(rpcUrl);
- URLConnection conn = realUrl.openConnection();
+ HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("content-type", "text/xml");
conn.setDoOutput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(postBody);
out.flush();
- in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = in.readLine()) != null)
+ int rc = conn.getResponseCode();
+ if (rc != HttpURLConnection.HTTP_OK)
{
- result.add(line);
+ Cache.log.error(
+ String.format("Error status from %s: %d", rpcUrl, rc));
+ return result;
}
+
+ InputStream inputStream = conn.getInputStream();
+ if (getReply)
+ {
+ in = new BufferedReader(new InputStreamReader(inputStream));
+ String line;
+ while ((line = in.readLine()) != null)
+ {
+ result.add(line);
+ }
+ }
+ } catch (SocketException e)
+ {
+ // thrown when 'quit' command is sent to PyMol
+ Cache.log.warn(String.format("Request to %s returned %s", rpcUrl,
+ e.toString()));
} catch (Exception e)
{
e.printStackTrace();
} finally
{
- try
- {
- if (out != null)
- {
- out.close();
- }
- if (in != null)
- {
- in.close();
- }
- } catch (IOException ex)
+ if (out != null)
{
- ex.printStackTrace();
- }
+ out.close();
+ }
}
return result;
}