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

org.springframework.social.partnercenter.http.client.RestClient Maven / Gradle / Ivy

Go to download

A provider extension for Spring Social to enable connectivity with Microsoft and an API binding for Microsoft's Partner Center API.

There is a newer version: 10.11.0
Show newest version
package org.springframework.social.partnercenter.http.client;

import static java.util.Collections.singletonList;

import java.net.URI;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.social.partnercenter.api.ApiFault;
import org.springframework.social.partnercenter.api.ApiFaultException;
import org.springframework.social.partnercenter.serialization.Json;
import org.springframework.social.partnercenter.serialization.JsonSerializationException;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestOperations;
import org.springframework.web.util.UriComponentsBuilder;

public class RestClient implements RestResource {
	private RestOperations restTemplate;
	private URI resourceBaseUri;

	public RestClient(RestOperations restTemplate, URI resourceBaseUri) {
		this.restTemplate = restTemplate;
		this.resourceBaseUri = resourceBaseUri;
	}

	public HttpRequestBuilder request(){
		return new HttpRequestBuilder(this, UriComponentsBuilder.fromUri(resourceBaseUri));
	}
	public HttpRequestBuilder request(String msRequestId, String msCorrelationId){
		return new HttpRequestBuilder(this, UriComponentsBuilder.fromUri(resourceBaseUri), msRequestId, msCorrelationId);
	}

	public HttpRequestBuilder request(MediaType mediaType){
		return new HttpRequestBuilder(this, UriComponentsBuilder.fromUri(resourceBaseUri)).header(HttpHeaders.CONTENT_TYPE, singletonList(mediaType.toString()));
	}

	public ResponseEntity delete(URI uri, HttpHeaders headers){
		try {
			return restTemplate.exchange(uri, HttpMethod.DELETE, new HttpEntity<>(headers), String.class);
		} catch (HttpClientErrorException e) {
			throw buildApiFault(e);
		}
	}

	public  ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, Class responseType) {
		try{
			return restTemplate.exchange(uri, httpMethod, entity, responseType);
		} catch (HttpClientErrorException e){
			throw buildApiFault(e);
		}
	}

	public  ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, ParameterizedTypeReference responseType) {
		try{
			return restTemplate.exchange(uri, httpMethod, entity, responseType);
		} catch (HttpClientErrorException e){
			throw buildApiFault(e);
		}
	}

	protected ApiFaultException buildApiFault(HttpClientErrorException e) {
		String responseBody = e.getResponseBodyAsString();
		try {
			ApiFault apiFault = Json.fromJson(responseBody, ApiFault.class);
			return new ApiFaultException(apiFault.getErrorMessage(), e, e.getStatusCode(), apiFault);
		} catch (JsonSerializationException serializationException) {
			ApiFault apiFault = new ApiFault();
			apiFault.setErrorMessage(responseBody);
			return new ApiFaultException(responseBody, e, e.getStatusCode(), apiFault);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy