Cisco Cisco Prime Home 6.3 Developer's Guide

Page of 78
Cisco Prime Home – Integration Guide
 
 
OL-28557-01   v5.0.1
 
76 
com.clearaccess.rest.RestClient 
/* 
* Copyright (c) 2010 ClearAccess, Inc. 
* This code is provided AS-IS for illustration purposes. 
*/ 
 
package com.clearaccess.rest; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import org.springframework.web.client.RestTemplate; 
import org.springframework.web.client.RequestCallback; 
import org.springframework.web.client.ResponseExtractor; 
import org.springframework.http.HttpMethod; 
import org.springframework.http.client.ClientHttpRequest; 
import org.springframework.http.client.ClientHttpResponse; 
import org.apache.commons.io.IOUtils; 
 
import java.io.IOException; 
 
@Component 
public class RestClient extends RestTemplate { 
/** 
* spring's RestTemplate does not support authentication, this 
class 
* requires the use of our own http client request factory 
* @param clientHttpRequestFactory our own client request factory 
which can perform 
* authentication. 
*/ 
@Autowired 
public RestClient(HttpClientFactory clientHttpRequestFactory) { 
super(clientHttpRequestFactory); 

 
/** 
* spring's RestTemplate does not support a 'put' which can also 
return data. 
* @param uri the uri of the PUT request 
* @param requestData the data to send 
* @return the data which was returned 
*/ 
public String putForObject(final String uri, final String 
requestData) { 
return execute(uri, HttpMethod.PUT, new RequestCallback() { 
public void doWithRequest(ClientHttpRequest 
clientHttpRequest) throws IOException { 
clientHttpRequest.getBody().write(requestData.getBytes()); 
}, new ResponseExtractor<String>() { 
public String extractData(ClientHttpResponse  
clientHttpResponse) throws IOException { 
return new 
String(IOUtils.toByteArray(clientHttpResponse.getBody())); 
});