Java example for scripting the RCSB-PDB RESTful Custom Report web services

For more information on the RESTful web services see here.

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

public class GetCustomReport
{
   
   public static final String SERVICELOCATION="http://www.rcsb.org/pdb/rest/customReport";
   
   public static void main(String[] args){

      String qstr = "?pdbids=1stp,2jef,1cdg&customReportColumns=structureId,structureTitle,experimentalTechnique&format=csv&service=wsfile";
      
      //pdbids=* will retrieve a report including all the current PDB IDs
      //String qstr = "?pdbids=*&customReportColumns=structureId,structureTitle,experimentalTechnique&format=csv&service=wsfile";

      GetCustomReport g = new GetCustomReport();
      
      try {
         
         String s = g.getResult(qstr);
         
         System.out.println(s);
         
      }
      catch (Exception e)
      {
      	 System.out.println("Error: " + e.getMessage());
      }
   }
   
   public static String getResult(String qstr) throws IOException
   {
      String urlStr = SERVICELOCATION+qstr;
      
      URL url = new URL(urlStr);
      
      HttpURLConnection conn =
          (HttpURLConnection) url.openConnection();
      
      if (conn.getResponseCode() != 200) {
         throw new IOException(conn.getResponseMessage());
       }

       // Buffer the result into a string
       BufferedReader rd = new BufferedReader(
           new InputStreamReader(conn.getInputStream()));
       StringBuilder sb = new StringBuilder();
       String line;
       while ((line = rd.readLine()) != null) {
         sb.append(line+"\n");
       }
       rd.close();

       conn.disconnect();
       return sb.toString();
     }
}