public Object invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body
        , Map<String, String> headerParams, String contentType) throws ApiException {
        Client client = getClient(host);

        StringBuilder b = new StringBuilder();

        for(String key : queryParams.keySet()) {
            String value = queryParams.get(key);
            if (value != null){
                if(b.toString().length() == 0)
                    b.append("?");
                else
                    b.append("&");
                b.append(escapeString(key)).append("=").append(escapeString(value));
            }
        }
        String querystring = b.toString();


        try {
            querystring = URIUtil.encodeQuery(querystring);
        }
        catch (URIException e) {
            throw new ApiException(0, e.getStackTrace().toString());
        }
        
        Builder builder = client.resource(host + path + querystring).accept(new String[]{"application/json", "image/png"});
        for(String key : headerParams.keySet()) {
            builder.header(key, headerParams.get(key));
        }

        for(String key : defaultHeaderMap.keySet()) {
            if(!headerParams.containsKey(key)) {
                builder.header(key, defaultHeaderMap.get(key));
            }
        }
        ClientResponse response = null;

        if("GET".equals(method)) {
            response = (ClientResponse) builder.get(ClientResponse.class);
        }
        else if ("POST".equals(method)) {
            if(body == null)
                response = builder.post(ClientResponse.class, serialize(body));
            else
                response = builder.type("application/json").post(ClientResponse.class, serialize(body));
        }
        else if ("PUT".equals(method)) {
            if(body == null)
                response = builder.put(ClientResponse.class, serialize(body));
            else
                response = builder.type("application/json").put(ClientResponse.class, serialize(body));
        }
        else if ("DELETE".equals(method)) {
            if(body == null)
                response = builder.delete(ClientResponse.class, serialize(body));
            else
                response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
        }
        else {
            throw new ApiException(500, "unknown method type " + method);
        }
        if(response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
            return null;
        }
        else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
                // Handle the casting of the response based on the type.
                if (!response.getHeaders().get("Content-Type").get(0).equals(MediaType.APPLICATION_JSON)) {
                            return response.getEntityInputStream();
                    }
                    return (String) response.getEntity(String.class);
        }
        else {
            throw new ApiException(
                                response.getClientResponseStatus().getStatusCode(),
                                response.getEntity(String.class));
        }
    }
public Object invokeAPI ( String host , String path , String method , Map < String , String > queryParams , Object body , Map < String , String > headerParams , String contentType ) throws ApiException { Client client = getClient ( host ) ; StringBuilder b = new StringBuilder ( ) ; for ( String key : queryParams . keySet ( ) ) { String value = queryParams . get ( key ) ; if ( value != null ) { if ( b . toString ( ) . length ( ) == 0 ) b . append ( "?" ) ; else b . append ( "&" ) ; b . append ( escapeString ( key ) ) . append ( "=" ) . append ( escapeString ( value ) ) ; } } String querystring = b . toString ( ) ; try { querystring = URIUtil . encodeQuery ( querystring ) ; } catch ( URIException e ) { throw new ApiException ( 0 , e . getStackTrace ( ) . toString ( ) ) ; } Builder builder = client . resource ( host + path + querystring ) . accept ( new String [ ] { "application/json" , "image/png" } ) ; for ( String key : headerParams . keySet ( ) ) { builder . header ( key , headerParams . get ( key ) ) ; } for ( String key : defaultHeaderMap . keySet ( ) ) { if ( ! headerParams . containsKey ( key ) ) { builder . header ( key , defaultHeaderMap . get ( key ) ) ; } } ClientResponse response = null ; if ( "GET" . equals ( method ) ) { response = ( ClientResponse ) builder . get ( ClientResponse . class ) ; } else if ( "POST" . equals ( method ) ) { if ( body == null ) response = builder . post ( ClientResponse . class , serialize ( body ) ) ; else response = builder . type ( "application/json" ) . post ( ClientResponse . class , serialize ( body ) ) ; } else if ( "PUT" . equals ( method ) ) { if ( body == null ) response = builder . put ( ClientResponse . class , serialize ( body ) ) ; else response = builder . type ( "application/json" ) . put ( ClientResponse . class , serialize ( body ) ) ; } else if ( "DELETE" . equals ( method ) ) { if ( body == null ) response = builder . delete ( ClientResponse . class , serialize ( body ) ) ; else response = builder . type ( "application/json" ) . delete ( ClientResponse . class , serialize ( body ) ) ; } else { throw new ApiException ( 500 , "unknown method type " + method ) ; } if ( response . getClientResponseStatus ( ) == ClientResponse . Status . NO_CONTENT ) { return null ; } else if ( response . getClientResponseStatus ( ) . getFamily ( ) == Family . SUCCESSFUL ) { if ( ! response . getHeaders ( ) . get ( "Content-Type" ) . get ( 0 ) . equals ( MediaType . APPLICATION_JSON ) ) { return response . getEntityInputStream ( ) ; } return ( String ) response . getEntity ( String . class ) ; } else { throw new ApiException ( response . getClientResponseStatus ( ) . getStatusCode ( ) , response . getEntity ( String . class ) ) ; } }
Serialize an Object.
@param host the targeted host
@param path the targeted rest endpoint
@param method the HTTP method
@param queryParams the query parameters
@param body the obligatory body of a post
@param headerParams the HTTP header parameters
@param contentType the content type
@throws APIException if an exception occurs during querying of the API.