|
1 | 1 | package org.openherbarium.module.srv.imageservice.rest.endpoint;
|
2 | 2 |
|
| 3 | +import javax.annotation.PostConstruct; |
| 4 | +import javax.ws.rs.client.Client; |
| 5 | +import javax.ws.rs.client.ClientBuilder; |
| 6 | +import javax.ws.rs.core.Response; |
| 7 | +import java.util.Objects; |
| 8 | + |
3 | 9 | public class ImageServiceRestClient {
|
4 | 10 |
|
5 |
| - // TODO |
6 |
| - // getImage(String id) |
| 11 | + public static final String PROPERTY_IP = "imageservice.ip"; |
| 12 | + public static final String PROPERTY_PORT = "imageservice.port"; |
| 13 | + public static final String DEFAULT_PORT = "8080"; |
| 14 | + public static final String DEFAULT_IP = "127.0.0.1"; |
| 15 | + |
| 16 | + private String serverIp; |
| 17 | + private String serverPort; |
| 18 | + |
| 19 | + @PostConstruct |
| 20 | + public void init() { |
| 21 | + this.serverIp = System.getProperty(PROPERTY_IP, DEFAULT_IP); |
| 22 | + this.serverPort = System.getProperty(PROPERTY_PORT, DEFAULT_PORT); |
| 23 | + if (Objects.isNull(serverIp)) throw new NullPointerException("serverIP is null"); |
| 24 | + if (Objects.isNull(serverPort)) throw new NullPointerException("serverPort is null"); |
| 25 | + } |
| 26 | + |
| 27 | + public String getImageProperties(String imageid) { |
| 28 | + final Client client = ClientBuilder.newClient(); |
| 29 | + |
| 30 | + final Response response = client.target(buildBaseUrl() + imageid + "/" + "properties") |
| 31 | + .request() |
| 32 | + .get(); |
| 33 | + |
| 34 | + return response.readEntity(String.class); |
| 35 | + } |
| 36 | + |
| 37 | + public byte[] getImage(String imageid, String tilegroup, String image) { |
| 38 | + final Client client = ClientBuilder.newClient(); |
| 39 | + |
| 40 | + final Response response = client.target(buildBaseUrl() + imageid + "/" + tilegroup + "/" + image) |
| 41 | + .request() |
| 42 | + .get(); |
| 43 | + |
| 44 | + return response.readEntity(byte[].class); |
| 45 | + } |
| 46 | + |
7 | 47 |
|
| 48 | + private String buildBaseUrl() { |
| 49 | + return String.format("http://%s:%s/rest/imageservice/", serverIp, serverPort); |
| 50 | + } |
8 | 51 | }
|
0 commit comments