/*
* Ninja Metrics
* Copyright 2012
* Build-Id: EMPTY
*/
package com.ninjametrics.kapi;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import com.ninjametrics.models.ImportStatusResponse;
import com.ninjametrics.thread.CommandIFace;
/**
* extension of Spring's RestTemplate for POSTing JSON data
*
*/
public class JsonPost<T> implements CommandIFace<T> {
private URI myPostURI = null;
private RestTemplate myRestTemplate = null;
public JsonPost(final String thePostUrlStr) throws URISyntaxException {
this.myPostURI = new URI(thePostUrlStr);
this.myRestTemplate = new RestTemplate();
}
@Override
public void execute(final T theArgData) {
final HttpHeaders aHttpHeaders = new HttpHeaders();
aHttpHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
aHttpHeaders.setContentType(new MediaType("application", "json"));
final HttpEntity<T> aHttpEntity = new HttpEntity<T>(theArgData, aHttpHeaders);
try {
this.myRestTemplate.postForObject(this.myPostURI, aHttpEntity,
ImportStatusResponse.class);
} catch (final RestClientException ex) {
Logger.getLogger(JsonPost.class.getName()).log(Level.WARNING, null,
ex);
} catch (final HttpMessageNotReadableException ex) {
Logger.getLogger(JsonPost.class.getName()).log(Level.WARNING, null,
ex);
}
}
}