spring has provided Rest Template class to interact with the rest web services.
In our situation we are going to download banary stream data(e.g. any file) from rest template
We need to call rest template havin url as https://xyz.com/rest
note:For https service you need to install all necessary certificates
package service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class DownloadRestClient {
private Logger logger = Logger.getLogger(DownloadRestClient.class);
private String uri = "https://xyz.com/rest";
private String name;
public DownloadRestClient(String name) {
super();
this.name = name;
}
/**
* @param args
* @throws IOException
*/
public String run(String token) throws IOException {
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("encryptedToken", token);
logger.info("Created multipart request: " + multipartMap);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
template.getMessageConverters()
.add(new ByteArrayHttpMessageConverter());
HttpEntity<Object> request = new HttpEntity<Object>(multipartMap,
headers);
logger.info("Posting request to: " + uri);
ResponseEntity<byte[]> httpResponse = template.exchange(uri,
HttpMethod.POST, request, byte[].class);
if (!httpResponse.getStatusCode().equals(HttpStatus.OK)) {
logger.error("Problems with the request. Http status: "
+ httpResponse.getStatusCode());
} else {
System.out.println("Got successfull message "
+ httpResponse.getHeaders().CONTENT_DISPOSITION);
FileOutputStream fileOutputStream = new FileOutputStream(
"D:\\Sagar\\LunaWorkspace\\src\\resource\\worddownload.doc");
org.apache.commons.io.IOUtils.write(httpResponse.getBody(),
fileOutputStream);
}
return httpResponse.toString();
}
}
In rest template spring has provided many converts to handle multiple data types e.g. binary data,json,xml etc. so we are using ByteArrayHttpMessageConverter to receive binary stream data and convert it to byte array over the wire .
Read below documentation for more details
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html
In our situation we are going to download banary stream data(e.g. any file) from rest template
We need to call rest template havin url as https://xyz.com/rest
note:For https service you need to install all necessary certificates
package service;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.log4j.Logger;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class DownloadRestClient {
private Logger logger = Logger.getLogger(DownloadRestClient.class);
private String uri = "https://xyz.com/rest";
private String name;
public DownloadRestClient(String name) {
super();
this.name = name;
}
/**
* @param args
* @throws IOException
*/
public String run(String token) throws IOException {
RestTemplate template = new RestTemplate();
MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<String, Object>();
multipartMap.add("encryptedToken", token);
logger.info("Created multipart request: " + multipartMap);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
template.getMessageConverters()
.add(new ByteArrayHttpMessageConverter());
HttpEntity<Object> request = new HttpEntity<Object>(multipartMap,
headers);
logger.info("Posting request to: " + uri);
ResponseEntity<byte[]> httpResponse = template.exchange(uri,
HttpMethod.POST, request, byte[].class);
if (!httpResponse.getStatusCode().equals(HttpStatus.OK)) {
logger.error("Problems with the request. Http status: "
+ httpResponse.getStatusCode());
} else {
System.out.println("Got successfull message "
+ httpResponse.getHeaders().CONTENT_DISPOSITION);
FileOutputStream fileOutputStream = new FileOutputStream(
"D:\\Sagar\\LunaWorkspace\\src\\resource\\worddownload.doc");
org.apache.commons.io.IOUtils.write(httpResponse.getBody(),
fileOutputStream);
}
return httpResponse.toString();
}
}
In rest template spring has provided many converts to handle multiple data types e.g. binary data,json,xml etc. so we are using ByteArrayHttpMessageConverter to receive binary stream data and convert it to byte array over the wire .
Read below documentation for more details
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html
No comments:
Post a Comment