Question: Looking For A Reason Why My Java Code Could Not Create Pathway Image Using Reactome Restful Api
1
gravatar for samsara
6.5 years ago by
samsara600
The Earth
samsara600 wrote:

I have been struggling with Reactome RESTful API to generate pathway diagram for a while. When performed HTTP GET requesto to http://reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI/RESTfulWS/pathwayDiagram/109581/png, API returns base64 encoded image. Here, 109581 is Pathway database identifier. For some reason I am unable to write it to PNG image in java.

I am using Apache Commons Codec for base64 decoding and jersey for REST client.

Could you please find out the reason why i am unable to write to image after decoding base64 string ?

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author bishwa
 */
public class ReactomeRESTJava {

    public static void writeImage(String encodedString) throws IOException
    {
        // Decode InputcodedString
        byte[] decodedByteArray = Base64.decodeBase64(encodedString);
        // convert byte into string
        String decodedString = new String(decodedByteArray);
        //System.out.println(decodedString);

        BufferedWriter out = new BufferedWriter(new FileWriter("test.png"));
        out.write(decodedString);
        out.close();

    }

    public static void main(String[] args) {
        // TODO code application logic here
        try
        {
            // Parameters ------------------------------------------------------
            String baseURL="http://reactomews.oicr.on.ca:8080/ReactomeRESTfulAPI"
                    + "/RESTfulWS/";
            //String method="POST";
            String method="GET";
            String url = "pathwayDiagram/109581/png";
            String input= "ID=170075,176374,68557";

            Client client1 = Client.create();
            WebResource webRS = client1.resource(baseURL+url);

            ClientResponse response = null ;

            // perform GET of POST request
            System.out.println("Fetching ..");
            switch (method) 
            {
                case "POST":
                    response = webRS.accept("application/json")
                            .post(ClientResponse.class, input);
                    break;
                case "GET":
                    response = webRS.accept("text/plain")
                            .get(ClientResponse.class);
                    break;
            }

            // throw response error codes
            if(response.getStatus() != 200)
            {
                throw new RuntimeException("Failed: HTTP error code: "
                        + response.getStatus());
            }

            String output = response.getEntity(String.class);
            //System.out.println("Output from Server ..\n");
            //System.out.println(output);

            // write base64 coded string to png image
            System.out.println("Writing image to a file...");
            writeImage(output);

        } catch (RuntimeException | IOException e){}
    }
}
pathway api java • 2.8k views
ADD COMMENTlink modified 6.5 years ago by Pierre Lindenbaum123k • written 6.5 years ago by samsara600

what do you mean "I'm unable to write..." ? What is the exact error ?

ADD REPLYlink written 6.5 years ago by Pierre Lindenbaum123k

The image written is blank.

ADD REPLYlink modified 6.5 years ago • written 6.5 years ago by samsara600
2
gravatar for Pierre Lindenbaum
6.5 years ago by
France/Nantes/Institut du Thorax - INSERM UMR1087
Pierre Lindenbaum123k wrote:

you should never write an array of bytes using a *Writer : UTF-8 or any 'Locale' characters will be interpreted. seehttp://stackoverflow.com/questions/5223760/writer-or-outputstream Use a *OutputStream instead.

You'd better use:

FileOutputStream out=new FileOutputStream(new File("image.png"));
out.write(decodedByteArray);
out.close();
ADD COMMENTlink modified 6.5 years ago • written 6.5 years ago by Pierre Lindenbaum123k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 758 users visited in the last hour