Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.kirgor.enklib.rest;
  
 
 import java.util.List;
 import java.util.Map;

Base class for RESTful clients, which should be extended in order to support necessary content type. Uses Apache HttpClient.
 
 public abstract class RESTClient {
     private String baseUrl;
     private String contentType;
     private Map<StringStringcookies = new HashMap<StringString>();
     private HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

    
Creates RESTClient instance.

Parameters:
baseUrl Base URL, which all request paths will be appended to.
contentType Content-Type value, which will be passed as request header to all requests.
 
     public RESTClient(String baseUrlString contentType) {
         this. = baseUrl;
         this. = contentType;
     }

    
Performs GET request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
params Map of URL query params.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
 
     public <T> EntityResponse<T> get(Class<T> entityClassString pathMap<StringStringparamsMap<StringStringheadersthrows IOExceptionRESTException {
         HttpGet httpGet = buildHttpGet(pathparams);
         return parseEntityResponse(entityClassgetHttpResponse(httpGetheaders));
     }

    
Performs GET request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
params Map of URL query params.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
 
     public <T> EntityResponse<T> get(Class<T> entityClassString pathMap<StringStringparamsthrows IOExceptionRESTException {
         return get(entityClasspathparamsnull);
     }

    
Performs GET request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
 
     public <T> EntityResponse<T> get(Class<T> entityClassString paththrows IOExceptionRESTException {
         return get(entityClasspathnullnull);
     }

    
Performs GET request.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
 
     public <T> EntityResponse<T> get(Class<T> entityClassthrows IOExceptionRESTException {
         return get(entityClass""nullnull);
     }

    
Performs GET request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
params Map of URL query params.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> getWithListResult(Class<T> entityClassString pathMap<StringStringparamsMap<StringStringheadersthrows IOExceptionRESTException {
        HttpGet httpGet = buildHttpGet(pathparams);
        return parseListEntityResponse(entityClassgetHttpResponse(httpGetheaders));
    }

    
Performs GET request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
params Map of URL query params.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> getWithListResult(Class<T> entityClassString pathMap<StringStringparamsthrows IOExceptionRESTException {
        return getWithListResult(entityClasspathparamsnull);
    }

    
Performs GET request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> getWithListResult(Class<T> entityClassString paththrows IOExceptionRESTException {
        return getWithListResult(entityClasspathnullnull);
    }

    
Performs GET request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> getWithListResult(Class<T> entityClassthrows IOExceptionRESTException {
        return getWithListResult(entityClass""nullnull);
    }

    
Performs GET request.

Parameters:
path Request path.
params Map of URL query params.
headers Map of HTTP request headers.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response get(String pathMap<StringStringparamsMap<StringStringheadersthrows IOExceptionRESTException {
        HttpGet httpGet = buildHttpGet(pathparams);
        return parseResponse(getHttpResponse(httpGetheaders));
    }

    
Performs GET request.

Parameters:
path Request path.
params Map of URL query params.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response get(String pathMap<StringStringparamsthrows IOExceptionRESTException {
        return get(pathparamsnull);
    }

    
Performs GET request.

Parameters:
path Request path.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response get(String paththrows IOExceptionRESTException {
        return get(pathnullnull);
    }

    
Performs GET request.

Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response get() throws IOExceptionRESTException {
        return get(""nullnull);
    }

    
Performs POST request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> post(Class<T> entityClassString pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postInternal(entityClasspayloadnew HttpPost( + path), headers);
    }

    
Performs POST request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> post(Class<T> entityClassString pathObject payloadthrows IOExceptionRESTException {
        return post(entityClasspathpayloadnull);
    }

    
Performs POST request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> post(Class<T> entityClassString paththrows IOExceptionRESTException {
        return post(entityClasspathnullnull);
    }

    
Performs POST request.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> post(Class<T> entityClassthrows IOExceptionRESTException {
        return post(entityClassnullnullnull);
    }

    
Performs POST request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> postWithListResult(Class<T> entityClassString pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postWithListResultInternal(entityClasspayloadnew HttpPost( + path), headers);
    }

    
Performs POST request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> postWithListResult(Class<T> entityClassString pathObject payloadthrows IOExceptionRESTException {
        return postWithListResult(entityClasspathpayloadnull);
    }

    
Performs POST request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> postWithListResult(Class<T> entityClassString paththrows IOExceptionRESTException {
        return postWithListResult(entityClasspathnullnull);
    }

    
Performs POST request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> postWithListResult(Class<T> entityClassthrows IOExceptionRESTException {
        return postWithListResult(entityClassnullnullnull);
    }

    
Performs POST request.

Parameters:
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response post(String pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postInternal(payloadnew HttpPost( + path), headers);
    }

    
Performs POST request.

Parameters:
path Request path.
payload Entity, which will be used as request payload.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response post(String pathObject payloadthrows IOExceptionRESTException {
        return post(pathpayloadnull);
    }

    
Performs POST request.

Parameters:
path Request path.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response post(String paththrows IOExceptionRESTException {
        return post(pathnullnull);
    }

    
Performs POST request.

Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response post() throws IOExceptionRESTException {
        return post(""nullnull);
    }

    
Performs PUT request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> put(Class<T> entityClassString pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postInternal(entityClasspayloadnew HttpPut( + path), headers);
    }

    
Performs PUT request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> put(Class<T> entityClassString pathObject payloadthrows IOExceptionRESTException {
        return put(entityClasspathpayloadnull);
    }

    
Performs PUT request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> put(Class<T> entityClassString paththrows IOExceptionRESTException {
        return put(entityClasspathnullnull);
    }

    
Performs PUT request.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> put(Class<T> entityClassthrows IOExceptionRESTException {
        return put(entityClassnullnullnull);
    }

    
Performs PUT request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> putWithListResult(Class<T> entityClassString pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postWithListResultInternal(entityClasspayloadnew HttpPut( + path), headers);
    }

    
Performs PUT request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
payload Entity, which will be used as request payload.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> putWithListResult(Class<T> entityClassString pathObject payloadthrows IOExceptionRESTException {
        return putWithListResult(entityClasspathpayloadnull);
    }

    
Performs PUT request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> putWithListResult(Class<T> entityClassString paththrows IOExceptionRESTException {
        return putWithListResult(entityClasspathnullnull);
    }

    
Performs PUT request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> putWithListResult(Class<T> entityClassthrows IOExceptionRESTException {
        return putWithListResult(entityClassnullnullnull);
    }

    
Performs PUT request.

Parameters:
path Request path.
payload Entity, which will be used as request payload.
headers Map of HTTP request headers.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response put(String pathObject payloadMap<StringStringheadersthrows IOExceptionRESTException {
        return postInternal(payloadnew HttpPut( + path), headers);
    }

    
Performs PUT request.

Parameters:
path Request path.
payload Entity, which will be used as request payload.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response put(String pathObject payloadthrows IOExceptionRESTException {
        return put(pathpayloadnull);
    }

    
Performs PUT request.

Parameters:
path Request path.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response put(String paththrows IOExceptionRESTException {
        return put(pathnullnull);
    }

    
Performs PUT request.

Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response put() throws IOExceptionRESTException {
        return put(""nullnull);
    }

    
Performs DELETE request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> delete(Class<T> entityClassString pathMap<StringStringheadersthrows IOExceptionRESTException {
        return parseEntityResponse(entityClassgetHttpResponse(new HttpDelete( + path), headers));
    }

    
Performs DELETE request.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> delete(Class<T> entityClassString paththrows IOExceptionRESTException {
        return delete(entityClasspathnull);
    }

    
Performs DELETE request.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<T> delete(Class<T> entityClassthrows IOExceptionRESTException {
        return delete(entityClass""null);
    }

    
Performs DELETE request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
headers Map of HTTP request headers.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> deleteWithListResult(Class<T> entityClassString pathMap<StringStringheadersthrows IOExceptionRESTException {
        return parseListEntityResponse(entityClassgetHttpResponse(new HttpDelete( + path), headers));
    }

    
Performs DELETE request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
path Request path.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> deleteWithListResult(Class<T> entityClassString paththrows IOExceptionRESTException {
        return deleteWithListResult(entityClasspathnull);
    }

    
Performs DELETE request, while expected response entity is a list of specified type.

Parameters:
entityClass Class, which contains expected response entity fields.
<T> Type of class, which contains expected response entity fields.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public <T> EntityResponse<List<T>> deleteWithListResult(Class<T> entityClassthrows IOExceptionRESTException {
        return deleteWithListResult(entityClass""null);
    }

    
Performs DELETE request.

Parameters:
path Request path.
headers Map of HTTP request headers.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response delete(String pathMap<StringStringheadersthrows IOExceptionRESTException {
        return parseResponse(getHttpResponse(new HttpDelete( + path), headers));
    }

    
Performs DELETE request.

Parameters:
path Request path.
Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response delete(String paththrows IOExceptionRESTException {
        return delete(pathnull);
    }

    
Performs DELETE request.

Throws:
java.io.IOException If error during HTTP connection or entity parsing occurs.
com.kirgor.enklib.rest.exception.RESTException If HTTP response code is non OK.
    public Response delete() throws IOExceptionRESTException {
        return delete(""null);
    }

    
Gets map of cookies, which are currently stored in the client.
    public Map<StringStringgetCookies() {
        return ;
    }

    
Manually add cookie to the client.

Parameters:
name Cookie name.
value Cookie value.
    public void addCookie(String nameString value) {
        .put(namevalue);
    }

    
Removes cookie from the client.

Parameters:
name Cookie name.
    public void removeCookie(String name) {
        .remove(name);
    }

    
Gets cookie value by name or null.

Parameters:
name Cookie name.
    public String getCookieValue(String name) {
        return .get(name);
    }

    
Parse entity of specified class from org.apache.http.HttpResponse instance.

Parameters:
entityClass Entity class.
httpResponse org.apache.http.HttpResponse instance, which is ready to read from.
<T> Entity type.
Throws:
java.io.IOException
    protected abstract <T> T parseEntity(Class<T> entityClassHttpResponse httpResponsethrows IOException;

    
Parse entity of specified class from org.apache.http.HttpResponse instance, while expected response entity is a list of specified type.

Parameters:
entityClass Entity class.
httpResponse org.apache.http.HttpResponse instance, which is ready to read from.
<T> Entity type.
Throws:
java.io.IOException
    protected abstract <T> List<T> parseListEntity(Class<T> entityClassHttpResponse httpResponsethrows IOException;

    
Converts payload to string according to content type.

Parameters:
entity Entity to convert.
    protected abstract String payloadToString(Object entity);
    private <T> EntityResponse<T> postInternal(Class<T> entityClassObject payloadHttpEntityEnclosingRequestBase requestMap<StringStringheadersthrows IOExceptionRESTException {
        if (payload != null) {
            request.setEntity(new StringEntity(payloadToString(payload)));
        }
        return parseEntityResponse(entityClassgetHttpResponse(requestheaders));
    }
    private <T> EntityResponse<List<T>> postWithListResultInternal(Class<T> entityClassObject payloadHttpEntityEnclosingRequestBase requestMap<StringStringheadersthrows IOExceptionRESTException {
        if (payload != null) {
            request.setEntity(new StringEntity(payloadToString(payload)));
        }
        return parseListEntityResponse(entityClassgetHttpResponse(requestheaders));
    }
    private Response postInternal(Object payloadHttpEntityEnclosingRequestBase requestMap<StringStringheadersthrows IOExceptionRESTException {
        if (payload != null) {
            request.setEntity(new StringEntity(payloadToString(payload)));
        }
        return parseResponse(getHttpResponse(requestheaders));
    }
    private CloseableHttpResponse getHttpResponse(HttpUriRequest httpUriRequestMap<StringStringheadersthrows IOException {
        httpUriRequest.addHeader("Content-Type");
        if (.size() > 0) {
            StringBuilder cookiesStringBuilder = new StringBuilder();
            for (Map.Entry<StringStringentry : .entrySet()) {
                cookiesStringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append(";");
            }
            cookiesStringBuilder.setLength(cookiesStringBuilder.length() - 1);
            httpUriRequest.addHeader("Cookie"cookiesStringBuilder.toString());
        }
        if (headers != null) {
            for (Map.Entry<StringStringentry : headers.entrySet()) {
                httpUriRequest.addHeader(entry.getKey(), entry.getValue());
            }
        }
        CloseableHttpClient httpClient = .build();
        return httpClient.execute(httpUriRequest);
    }
    private int getHttpResponseStatusCode(HttpResponse httpResponse) {
        return httpResponse.getStatusLine().getStatusCode();
    }
    private Map<StringStringgetHttpResponseHeaders(HttpResponse httpResponse) {
        Map<StringStringheaders = new HashMap<StringString>();
        for (Header header : httpResponse.getAllHeaders()) {
            headers.put(header.getName(), header.getValue());
            if (header.getName().toLowerCase().equals("set-cookie")) {
                int equalsIndex = header.getValue().indexOf('=');
                int semicolonIndex = header.getValue().indexOf(';');
                String cookieName = header.getValue().substring(0, equalsIndex);
                String cookieValue = header.getValue().substring(equalsIndex + 1, semicolonIndex);
                .put(cookieNamecookieValue);
            }
        }
        return headers;
    }
    private Response parseResponse(CloseableHttpResponse httpResponsethrows IOExceptionRESTException {
        int code = getHttpResponseStatusCode(httpResponse);
        Map<StringStringheaders = getHttpResponseHeaders(httpResponse);
        httpResponse.close();
        if (code < 400) {
            return new Response(codeheaders);
        } else {
            throw new RESTException(codeheadersnull);
        }
    }
    private <T> EntityResponse<T> parseEntityResponse(Class<T> entityClassCloseableHttpResponse httpResponsethrows IOExceptionRESTException {
        int code = getHttpResponseStatusCode(httpResponse);
        Map<StringStringheaders = getHttpResponseHeaders(httpResponse);
        T entity = parseEntity(entityClasshttpResponse);
        httpResponse.close();
        if (code < 400) {
            return new EntityResponse<T>(codeheadersentity);
        } else {
            throw new RESTException(codeheadersentity);
        }
    }
    private <T> EntityResponse<List<T>> parseListEntityResponse(Class<T> entityClassCloseableHttpResponse httpResponsethrows IOExceptionRESTException {
        int code = getHttpResponseStatusCode(httpResponse);
        Map<StringStringheaders = getHttpResponseHeaders(httpResponse);
        List<T> entity = parseListEntity(entityClasshttpResponse);
        httpResponse.close();
        if (code < 400) {
            return new EntityResponse<List<T>>(codeheadersentity);
        } else {
            throw new RESTException(codeheadersentity);
        }
    }
    private HttpGet buildHttpGet(String pathMap<StringStringparamsthrows UnsupportedEncodingException {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append().append(path);
        if (params != null && params.size() > 0) {
            stringBuilder.append("?");
            for (Map.Entry<StringStringentry : params.entrySet()) {
                stringBuilder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8")).append("&");
            }
            stringBuilder.setLength(stringBuilder.length() - 1);
        }
        return new HttpGet(stringBuilder.toString());
    }
New to GrepCode? Check out our FAQ X