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

io.mosip.kernel.packetmanager.util.RestUtil Maven / Gradle / Ivy

package io.mosip.kernel.packetmanager.util;

import com.google.gson.Gson;
import io.mosip.kernel.core.util.DateUtils;
import io.mosip.kernel.core.util.StringUtils;
import io.mosip.kernel.core.util.TokenHandlerUtil;
import io.mosip.kernel.packetmanager.dto.Metadata;
import io.mosip.kernel.packetmanager.dto.PasswordRequest;
import io.mosip.kernel.packetmanager.dto.SecretKeyRequest;
import io.mosip.kernel.packetmanager.dto.TokenRequestDTO;
import io.mosip.kernel.packetmanager.exception.ApiNotAccessibleException;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Iterator;

public class RestUtil {

    @Autowired
    private Environment environment;

    private static final String AUTHORIZATION = "Authorization=";

    public  T postApi(String uri, MediaType mediaType, Object requestType, Class responseClass) throws ApiNotAccessibleException {

        RestTemplate restTemplate;
        T result = null;
        try {
            restTemplate = getRestTemplate();
            result = (T) restTemplate.postForObject(uri, setRequestHeader(requestType, mediaType), responseClass);

        } catch (Exception e) {
            throw new ApiNotAccessibleException(e);
        }
        return result;
    }

    public  T getApi(URI uri, Class responseType) throws ApiNotAccessibleException {
        RestTemplate restTemplate;
        T result = null;
        try {
            restTemplate = getRestTemplate();
            result = (T) restTemplate.exchange(uri, HttpMethod.GET, setRequestHeader(null, null), responseType)
                    .getBody();
        } catch (Exception e) {
            throw new ApiNotAccessibleException(e);
        }
        return result;
    }

    public RestTemplate getRestTemplate() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
                .loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);

        return new RestTemplate(requestFactory);

    }

    private HttpEntity setRequestHeader(Object requestType, MediaType mediaType) throws IOException {
        MultiValueMap headers = new LinkedMultiValueMap();
        headers.add("Cookie", getToken());
        if (mediaType != null) {
            headers.add("Content-Type", mediaType.toString());
        }
        if (requestType != null) {
            try {
                HttpEntity httpEntity = (HttpEntity) requestType;
                HttpHeaders httpHeader = httpEntity.getHeaders();
                Iterator iterator = httpHeader.keySet().iterator();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    if (!(headers.containsKey("Content-Type") && key == "Content-Type"))
                        headers.add(key, httpHeader.get(key).get(0));
                }
                return new HttpEntity(httpEntity.getBody(), headers);
            } catch (ClassCastException e) {
                return new HttpEntity(requestType, headers);
            }
        } else
            return new HttpEntity(headers);
    }

    public String getToken() throws IOException {
        String token = System.getProperty("token");
        boolean isValid = false;

        if (StringUtils.isNotEmpty(token)) {

            isValid = TokenHandlerUtil.isValidBearerToken(token, environment.getProperty("token.request.issuerUrl"),
                    environment.getProperty("token.request.clientId"));


        }
        if (!isValid) {
            TokenRequestDTO tokenRequestDTO = new TokenRequestDTO();
            tokenRequestDTO.setId(environment.getProperty("token.request.id"));
            tokenRequestDTO.setMetadata(new Metadata());

            tokenRequestDTO.setRequesttime(DateUtils.getUTCCurrentDateTimeString());
            // tokenRequestDTO.setRequest(setPasswordRequestDTO());
            tokenRequestDTO.setRequest(setSecretKeyRequestDTO());
            tokenRequestDTO.setVersion(environment.getProperty("token.request.version"));

            Gson gson = new Gson();
            HttpClient httpClient = HttpClientBuilder.create().build();
            // HttpPost post = new
            // HttpPost(environment.getProperty("PASSWORDBASEDTOKENAPI"));
            HttpPost post = new HttpPost(environment.getProperty("KEYBASEDTOKENAPI"));
            try {
                StringEntity postingString = new StringEntity(gson.toJson(tokenRequestDTO));
                post.setEntity(postingString);
                post.setHeader("Content-type", "application/json");
                HttpResponse response = httpClient.execute(post);
                org.apache.http.HttpEntity entity = response.getEntity();
                String responseBody = EntityUtils.toString(entity, "UTF-8");
                Header[] cookie = response.getHeaders("Set-Cookie");
                if (cookie.length == 0)
                    throw new IOException("cookie is empty. Could not generate new token.");
                token = response.getHeaders("Set-Cookie")[0].getValue();
                System.setProperty("token", token.substring(14, token.indexOf(';')));
                return token.substring(0, token.indexOf(';'));
            } catch (IOException e) {
                throw e;
            }
        }
        return AUTHORIZATION + token;
    }

    private SecretKeyRequest setSecretKeyRequestDTO() {
        SecretKeyRequest request = new SecretKeyRequest();
        request.setAppId(environment.getProperty("token.request.appid"));
        request.setClientId(environment.getProperty("token.request.clientId"));
        request.setSecretKey(environment.getProperty("token.request.secretKey"));
        return request;
    }

    private PasswordRequest setPasswordRequestDTO() {
        PasswordRequest request = new PasswordRequest();
        request.setAppId(environment.getProperty("token.request.appid"));
        request.setPassword(environment.getProperty("token.request.password"));
        request.setUserName(environment.getProperty("token.request.username"));
        return request;
    }
}