Looking For A Reason Why My Java Code Could Not Create Pathway Image Using Reactome Restful Api
1
1
Entering edit mode
11.7 years ago
samsara ▴ 630

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 • 3.8k views
ADD COMMENT
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

The image written is blank.

ADD REPLY
2
Entering edit mode
11.7 years ago

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 COMMENT

Login before adding your answer.

Traffic: 2434 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6