All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whitesource.utils.HttpApiQuery Maven / Gradle / Ivy

package org.whitesource.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.whitesource.agent.client.ClientConstants;
import org.whitesource.utils.logger.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * @author eRez Huberman
 **/
public class HttpApiQuery {
    /* --- Private Members --- */
    private static final Logger logger = LoggerFactory.getLogger(HttpApiQuery.class);
    private static final String CONTENT_TYPE = "content-type";
    private static final String TOKEN = "token";

    public static JsonElement getHttpResponse(String apiUrl, Map params, HttpApiQueryType type){
        logger.debug("getting http response from {}", apiUrl);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            HttpClient client = HttpClientBuilder.create().build();
            HttpUriRequest request;
            if (type.equals(HttpApiQueryType.POST)) {
                request = new HttpPost(apiUrl);
                String requestBody = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(params);
                StringEntity entity = new StringEntity(requestBody, ContentType.APPLICATION_FORM_URLENCODED);
                request.addHeader(CONTENT_TYPE, ClientConstants.APPLICATION_JSON);
                    ((HttpPost) request).setEntity(entity);
            } else {
                request = new HttpGet(apiUrl);
                if (params.get(TOKEN) != null) {
                    request.addHeader("Authorization", "JWT " + params.get(TOKEN));
                }
            }
            HttpResponse response = client.execute(request);
            if (response != null) {
                String jsonString = EntityUtils.toString(response.getEntity());
                JsonElement json = new JsonParser().parse(jsonString);
                return json;
            } else {
                logger.warn("Failed to get response from {}", apiUrl);
            }
        } catch (JsonProcessingException e) {
            logger.warn("getHttpResponse JsonProcessingException: {}", e.getMessage());
            logger.debug("getHttpResponse JsonProcessingException: {}", e.getStackTrace());
        } catch (ClientProtocolException e) {
            logger.warn("getHttpResponse ClientProtocolException: {}", e.getMessage());
            logger.debug("getHttpResponse ClientProtocolException: {}", e.getStackTrace());
        } catch (IOException e) {
            logger.warn("getHttpResponse IOException: {}", e.getMessage());
            logger.debug("getHttpResponse IOException: {}", e.getStackTrace());
        } catch (Exception e){
            logger.warn("getHttpResponse Exception: {}", e.getMessage());
            logger.debug("getHttpResponse Exception: {}", e.getStackTrace());
        }
        return null;
    }
}

enum  QueryType {
    POST,
    GET
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy