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

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

package org.springframework.social.partnercenter.http.client;

import static java.util.Collections.singletonList;

import java.net.URI;
import java.util.Map;

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.web.client.RestTemplate;

public class RestResource {
	private RestTemplate restTemplate;
	private String resourceBaseUri;

	public HttpRequestBuilder request(){
		return new HttpRequestBuilder(this, this.resourceBaseUri);
	}

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

	RestTemplate getRestTemplate() {
		return restTemplate;
	}

	public RestResource(RestTemplate restTemplate, String resourceBaseUri) {
		this.restTemplate = restTemplate;
		this.resourceBaseUri = resourceBaseUri;
	}

	 ResponseEntity get(URI url, ParameterizedTypeReference responseType) {
		return execute(url, HttpMethod.GET, null, responseType);
	}
	 ResponseEntity get(URI url, ParameterizedTypeReference responseType, HttpHeaders headers) {
		return execute(url, HttpMethod.GET, new HttpEntity<>(headers), responseType);
	}

	 ResponseEntity get(URI url, Class responseType) {
		return execute(url, HttpMethod.GET, null, responseType);
	}

	 ResponseEntity get(URI url, Class responseType, HttpHeaders header) {
		return execute(url, HttpMethod.GET, new HttpEntity<>(header), responseType);
	}

	 ResponseEntity post(URI url, ParameterizedTypeReference responseType) {
		return execute(url, HttpMethod.POST, HttpEntity.EMPTY, responseType);
	}

	 ResponseEntity post(URI url, ParameterizedTypeReference responseType, HttpHeaders headers) {
		return execute(url, HttpMethod.POST, new HttpEntity(headers), responseType);
	}

	 ResponseEntity post(URI uri, T entity, Class responseType) {
		return execute(uri, HttpMethod.POST, new HttpEntity<>(entity), responseType);
	}

	 ResponseEntity post(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.POST, entity, responseType);
	}

	 ResponseEntity patch(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.PATCH, entity, responseType);
	}

	 ResponseEntity put(URI uri, HttpEntity entity, Class responseType) {
		return execute(uri, HttpMethod.PUT, entity, responseType);
	}

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

	public  ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, Class responseType){
		return restTemplate.exchange(
				uri, httpMethod, entity, responseType
		);
	}

	public  ResponseEntity execute(URI uri, HttpMethod httpMethod, HttpEntity entity, ParameterizedTypeReference responseType){
		return restTemplate.exchange(
				uri, httpMethod, entity, responseType
		);
	}

}