consolidation of Platform isJS calls
[jalview.git] / src / jalview / javascript / web / WebResource.java
1 package jalview.javascript.web;
2
3 import jalview.util.Platform;
4
5 import java.net.URI;
6 import java.net.URISyntaxException;
7
8 /*
9  *  A JavaScript-only proxy for com.sun.jersey.api.client.WebResource
10  * 
11  */
12 public class WebResource
13 {
14
15   private String endpoint, params = "";
16
17   public WebResource(String endpoint)
18   {
19     this.endpoint = endpoint;
20   }
21
22   public WebResource queryParam(String key, String value)
23   {
24     params += (params == "" ? "?" : "&") + key + "="
25             + Platform.encodeURI(value);
26     return this;
27   }
28
29   public URI getURI()
30   {
31     try
32     {
33       return new URI(endpoint + params);
34     } catch (URISyntaxException e)
35     {
36       e.printStackTrace();
37       return null;
38     }
39   }
40
41   public Builder accept(String... encoding)
42   {
43     return new Builder(getURI(), encoding);
44   }
45
46   public static class Builder
47   {
48     private URI uri;
49
50     private String[] encoding;
51
52     public Builder(URI uri, String... encoding)
53     {
54       this.uri = uri;
55       this.encoding = encoding; // application/json
56     }
57
58     /**
59      * Get the response
60      * 
61      * @param c
62      *          must be ClientResponse
63      * @return
64      */
65     public ClientResponse get(Class<?> c)
66     {
67       return new ClientResponse(Platform.getFileAsString(uri.toString()),
68               encoding);
69     }
70   }
71
72 }