Class RestClient


  • public class RestClient
    extends Object
    A simple Yamcs Rest client to help with basic requests.
    • Constructor Detail

      • RestClient

        public RestClient​(ServerURL serverURL)
        Creates a rest client that communications using protobuf
    • Method Detail

      • doRequest

        public CompletableFuture<byte[]> doRequest​(String resource,
                                                   io.netty.handler.codec.http.HttpMethod method)
        Performs a request with an empty body. Works using protobuf
        Parameters:
        resource -
        method -
        Returns:
        a the response body
      • doRequest

        public CompletableFuture<String> doRequest​(String resource,
                                                   io.netty.handler.codec.http.HttpMethod method,
                                                   String body)
        Perform asynchronously the request indicated by the HTTP method and return the result as a future providing byte array. Note that the response body will be limited to 10485760 - in case the server sends more than that, the CompletableFuture will completed with an error (the get() method will throw an Exception); the partial response will not be available.
        Parameters:
        resource - the url and query parameters after the "/api" part.
        method - http method to use
        body - the body of the request. Can be used even for the GET requests although strictly not allowed by the HTTP standard.
        Returns:
        the response body
        Throws:
        IllegalArgumentException - when the resource specification is invalid
      • doRequest

        public CompletableFuture<byte[]> doRequest​(String resource,
                                                   io.netty.handler.codec.http.HttpMethod method,
                                                   com.google.protobuf.Message message)
        Perform asynchronously the request indicated by the HTTP method and return the result as a future providing byte array. To be used when performing protobuf requests.
        Parameters:
        resource -
        method -
        message -
        Returns:
        future containing protobuf encoded data
      • doRequest

        public CompletableFuture<byte[]> doRequest​(String resource,
                                                   io.netty.handler.codec.http.HttpMethod method,
                                                   byte[] body)
        Perform asynchronously the request indicated by the HTTP method and return the result as a future providing byte array. To be used when performing protobuf requests.
        Parameters:
        resource -
        method -
        body - protobuf encoded data.
        Returns:
        future containing protobuf encoded data
      • doBaseRequest

        public CompletableFuture<byte[]> doBaseRequest​(String resource,
                                                       io.netty.handler.codec.http.HttpMethod method,
                                                       byte[] body)
        Perform asynchronously the request indicated by the HTTP method and return the result as a future providing byte array. To be used when performing protobuf requests.
        Parameters:
        resource -
        method -
        body - protobuf encoded data.
        Returns:
        future containing protobuf encoded data
      • doBulkRequest

        public CompletableFuture<Void> doBulkRequest​(io.netty.handler.codec.http.HttpMethod method,
                                                     String resource,
                                                     byte[] body,
                                                     BulkRestDataReceiver receiver)
        Performs a bulk request and provides the result piece by piece to the receiver. The potentially large result is split into messages based on the VarInt size preceding each message. The maximum size of each individual message is limited to 10485760
        Parameters:
        method -
        resource -
        receiver -
        Returns:
        future that is completed when the request is finished
        Throws:
        RuntimeException - if the uri + resource does not form a correct URL
      • setSendMediaType

        public void setSendMediaType​(String sendMediaType)
      • setAcceptMediaType

        public void setAcceptMediaType​(String acceptMediaType)
      • setMaxResponseLength

        public void setMaxResponseLength​(int size)
      • close

        public void close()
      • isAutoclose

        public boolean isAutoclose()
      • setAutoclose

        public void setAutoclose​(boolean autoclose)
        if autoclose is set, the httpClient will be automatically closed at the end of the request, so the netty eventgroup is shutdown. Otherwise it has to be done manually - but then the same object can be used to perform multiple requests.
        Parameters:
        autoclose -
      • addCookie

        public void addCookie​(io.netty.handler.codec.http.cookie.Cookie c)
      • getCookies

        public List<io.netty.handler.codec.http.cookie.Cookie> getCookies()
      • isInsecureTls

        public boolean isInsecureTls()
      • setInsecureTls

        public void setInsecureTls​(boolean insecureTls)
        if true and https connections are used, do not verify server certificate
        Parameters:
        insecureTls -
      • setCaCertFile

        public void setCaCertFile​(String caCertFile)
                           throws IOException,
                                  GeneralSecurityException
        In case of https connections, this file contains the CA certificates that are used to verify server certificate. If this is not set, java will use the default mechanism with the trustStore that can be configured via the javax.net.ssl.trustStore system property.
        Parameters:
        caCertFile -
        Throws:
        IOException
        GeneralSecurityException