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

fun.feellmoose.service.impl.RestTemplateSastLinkService Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
package fun.feellmoose.service.impl;


import com.fasterxml.jackson.core.type.TypeReference;
import fun.feellmoose.enums.GrantType;
import fun.feellmoose.enums.SastLinkErrorEnum;
import fun.feellmoose.exception.SastLinkException;
import fun.feellmoose.model.response.SastLinkResponse;
import fun.feellmoose.model.response.data.AccessToken;
import fun.feellmoose.model.response.data.RefreshToken;
import fun.feellmoose.model.response.data.User;
import fun.feellmoose.service.SastLinkService;
import fun.feellmoose.util.JsonUtil;
import org.springframework.http.RequestEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import fun.feellmoose.enums.SastLinkApi;


public final class RestTemplateSastLinkService extends AbstractSastLinkService {
    private final RestTemplate restTemplate;

    private RestTemplateSastLinkService(Builder builder) {
        super(builder);
        this.restTemplate = builder.restTemplate;
    }

    @Override
    public AccessToken accessToken(String code) throws SastLinkException {
        MultiValueMap map = new LinkedMultiValueMap<>();
        map.add(CODE, code);
        map.add(CODE_VERIFIER, code_verifier);
        map.add(GRANT_TYPE, GrantType.AUTHORIZATION_CODE.name);
        map.add(REDIRECT_URI, redirect_uri);
        map.add(CLIENT_ID, client_id);
        map.add(CLIENT_SECRET, client_secret);
        RequestEntity> entity = RequestEntity
                .post(SastLinkApi.ACCESS_TOKEN.getHttp(host_name))
                .header(CONTENT_TYPE, "multipart/form-data")
                .body(map);
        String body = restTemplate.exchange(entity, String.class).getBody();
        if (body == null) throw new SastLinkException(SastLinkErrorEnum.NULL_RESPONSE_BODY);
        if (body.isEmpty()) throw new SastLinkException(SastLinkErrorEnum.EMPTY_RESPONSE_BODY);
        SastLinkResponse response = JsonUtil.fromJson(body, new TypeReference<>() {
        });
        if (!response.isSuccess()) {
            throw new SastLinkException(response);
        }
        return response.getData();
    }


    @Override
    public RefreshToken refreshToken(String refreshToken) throws SastLinkException {
        MultiValueMap map = new LinkedMultiValueMap<>();
        map.add(REFRESH_TOKEN, refreshToken);
        map.add(GRANT_TYPE, GrantType.REFRESH_TOKEN.name);
        RequestEntity> entity = RequestEntity
                .post(SastLinkApi.REFRESH.getHttp(host_name))
                .header(CONTENT_TYPE, "multipart/form-data")
                .body(map);
        String body = restTemplate.exchange(entity, String.class).getBody();
        if (body == null) throw new SastLinkException(SastLinkErrorEnum.NULL_RESPONSE_BODY);
        if (body.isEmpty()) throw new SastLinkException(SastLinkErrorEnum.EMPTY_RESPONSE_BODY);
        SastLinkResponse response = JsonUtil.fromJson(body, new TypeReference<>() {
        });
        if (!response.isSuccess()) {
            throw new SastLinkException(response);
        }
        return response.getData();
    }

    @Override
    public User user(String accessToken) throws SastLinkException {
        RequestEntity entity = RequestEntity
                .get(SastLinkApi.USER_INFO.getHttp(host_name))
                .header(AUTHORIZATION, "Bearer " + accessToken)
                .build();
        String body = restTemplate.exchange(entity, String.class).getBody();
        if (body == null) throw new SastLinkException(SastLinkErrorEnum.NULL_RESPONSE_BODY);
        if (body.isEmpty()) throw new SastLinkException(SastLinkErrorEnum.EMPTY_RESPONSE_BODY);
        SastLinkResponse response = JsonUtil.fromJson(body, new TypeReference<>() {
        });
        if (!response.isSuccess()) {
            throw new SastLinkException(response);
        }
        return response.getData();
    }


    public static class Builder extends AbstractSastLinkService.Builder {
        private RestTemplate restTemplate;

        public Builder setRestTemplate(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
            return this;
        }

        @Override
        protected Builder self() {
            return this;
        }

        @Override
        public SastLinkService build() {
            super.build();
            if (this.restTemplate == null) {
                this.restTemplate = new RestTemplate();
            }
            return new RestTemplateSastLinkService(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy