Not able to add response headers to RestTemplate streaming response - java

In my spring-boot application, I have a GET end-point. When we call this GET endpoint, the application is sending a GET request to another service with RestTemplate and returns the same response file as the response of the GET request. With the below code I'm able to receive the response file. But I need to set the same headers that I have received to RestTempate request.
How to do that.
#GetMapping(value = URL_CONTENT_ID, produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE, APPLICATION_ZIP_VALUE,
MediaType.TEXT_HTML_VALUE})
#ControllerLogging
public ResponseEntity<StreamingResponseBody> getContentFile(#PathVariable String contentId) {
StreamingResponseBody responseBody = outputStream -> {
getContentFile(outputStream, contentId);
outputStream.close();
};
return ResponseEntity.ok()
.body(responseBody);
}
public void getContentFile(OutputStream outputStream, String nodeId) {
RequestCallback requestCallBack = request -> {
HttpHeaders headers = new HttpHeaders();
authenticationHelper.apply(headers::set);
request.getHeaders().addAll(headers);
};
ResponseExtractor<OutputStream> responseExtractor = clientHttpResponse -> {
InputStream inputStream = clientHttpResponse.getBody();
StreamUtils.copy(inputStream, outputStream);
return null;
};
restTemplate.execute(dcmUrl + nodeId, HttpMethod.GET, requestCallBack, responseExtractor);
}

Please find the solution to this issue, the change I did was adding "HttpServletResponse" to the parameters of the controller method.
#GetMapping(value = URL_CONTENT_ID, produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE, APPLICATION_ZIP_VALUE,
MediaType.TEXT_HTML_VALUE})
#ControllerLogging
public ResponseEntity<StreamingResponseBody> getContentFile(#PathVariable String contentId, HttpServletResponse response) {
StreamingResponseBody responseBody = outputStream -> {
getContentFile(outputStream, response, contentId);
outputStream.close();
};
return ResponseEntity.ok(responseBody);
}
public void getContentFile(OutputStream outputStream, HttpServletResponse response, String nodeId) {
RequestCallback requestCallBack = request -> {
HttpHeaders headers = new HttpHeaders();
authenticationHelper.apply(headers::set);
request.getHeaders().addAll(headers);
};
ResponseExtractor<OutputStream> responseExtractor = clientHttpResponse -> {
InputStream inputStream = clientHttpResponse.getBody();
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, modifyContentDispositionHeader(clientHttpResponse));
response.setHeader(HttpHeaders.CONTENT_TYPE, modifyContentTypeHeader(clientHttpResponse).toString());
StreamUtils.copy(inputStream, outputStream);
return null;
};
restTemplate.execute(dcmUrl + nodeId, HttpMethod.GET, requestCallBack, responseExtractor);
}

Related

StreamingResponseBody returning empty file

I'm trying to create a rest service to download files from a repository, using Springboot.
I'm trying to return a ResponseEntity with StreamingResponseBody, to return the file that i get from the repository, as an InputStream.
This is the current code i have:
#GetMapping(path = "/downloadFile")
public ResponseEntity<StreamingResponseBody> downloadFile(#RequestParam(value = "documentId") String documentId,
HttpServletRequest request, HttpServletResponse response) throws InterruptedException, IOException {
InputStream is = downloadService.getDocument(documentId);
StreamingResponseBody out = outputStream -> {
outputStream.write(IOUtils.toByteArray(is));
};
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/csv");
headers.add("Content-Disposition", "attachment; filename=" + documentId);
headers.add("Pragma", "no-cache");
headers.add("Cache-Control", "no-cache");
return (new ResponseEntity<>(out, headers, HttpStatus.OK));
}
When I consume this endpoint, using directly the browser, or postman, the file that is downloaded comes empty.
I understand that the OutputStream is written to asynchronously (Async is enabled in the config class).
How can I consume this service and get the file completely written, the way it comes from the repository I'm using ? ( if possible using Postman, just for testing purposes)
Am i building the service correctly?
I have modified the code bit little, in my documentId is the name of the file to be downloaded. I have tested, it is working fine. Check below the code.
#GetMapping(path = "/downloadFile")
public ResponseEntity<StreamingResponseBody> downloadFile(
#RequestParam(value = "documentId") String documentId,
HttpServletRequest request,
HttpServletResponse response)
throws InterruptedException, IOException {
String dirPath = "E:/sure-delete/"; //Directory having the files
InputStream inputStream = new FileInputStream(new File(dirPath + documentId));
final StreamingResponseBody out =
outputStream -> {
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
System.out.println("Writing some bytes of file...");
outputStream.write(data, 0, nRead);
}
};
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/csv");
headers.add("Content-Disposition", "attachment; filename=" + documentId);
headers.add("Pragma", "no-cache");
headers.add("Cache-Control", "no-cache");
return ResponseEntity.ok().headers(headers).body(out);
}

RestTemplate handle [image/jpg] response content type in java

Sorry, i am newbie on java web development.
I got some task to fetch user profile picture from 3rd party company via HTTP rest(GET method). Their api only can be accessed using session id on the header parameter and the api will return some byte[] array looks like ’ÑÒBRSb¢ÂáTr²ñ#‚4“â3C etc.
How to handle rest response with content type image/jpg in Rest Template?
I do my best like this
private RestTemplate restTemplate;
public byte[] getProfilePic(){
String canonicalPath = "http://dockertest/bankingapp/customer/profpicFile";
String sessionId= "MTQ4NzE5Mz...etc";
HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
//getHeaders() will return HttpHeaders with those parameter
ResponseEntity<byte[]> response = null;
try {
response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class);
} catch( HttpServerErrorException hse ){
throw hse;
}
return response;
}
This code will return an error
org.springframework.web.client.RestClientException: Could not extract
response: no suitable HttpMessageConverter found for response type
[[B] and content type [image/jpg]
Any suggestion or help will be appreciated!
Thank you
Update
Using stackoveflower suggestions i can manage to solve this.
private RestTemplate restTemplate;
public byte[] getProfilePic(){
String canonicalPath = "/mobile/customer/profpicFile";
String sessionId= "MTQ4NzE5Mz...etc";
HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
//getHeaders() will return HttpHeaders with those parameter
ResponseEntity<byte[]> response = null;
try {
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class).getBody();
return response;
} catch( HttpServerErrorException hse ){
throw hse;
}
return null;
}
Note about HttpMessageConverter, instead using list, i can directly add a ByteArrayHttpMessageConverter()
As said I guess you must use the right messageconverter
I would do in this way:
private RestTemplate restTemplate;
public byte[] getProfilePic(){
String canonicalPath = "http://dockertest/bankingapp/customer/profpicFile";
String sessionId= "MTQ4NzE5Mz...etc";
List<HttpMessageConverter> converters = new ArrayList<>(1);
converters.add(new ByteArrayHttpMessageConverter());
restTemplate.setMessageConverters(converters);
HttpEntity<byte[]> request = new HttpEntity<byte[]>(null, getHeaders(true, "GET", null, canonicalPath, sessionId));
//getHeaders() will return HttpHeaders with those parameter
ResponseEntity<byte[]> response = null;
try {
response = this.restTemplate.exchange(uri, HttpMethod.GET, request, byte[].class);
} catch( HttpServerErrorException hse ){
throw hse;
}
return response;
}
More information can be found here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#setMessageConverters-java.util.List- and here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/HttpMessageConverter.html and here https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/converter/ByteArrayHttpMessageConverter.html
Thank you very much,this problem takes up my a lot of time. Now,it was resolved.
following:
#Configuration
#Slf4j
public class RestTemplateConfiguration implements ApplicationContextAware {
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
RestTemplate restTemplate = (RestTemplate) applicationContext.getBean("restTemplate");
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.setUriTemplateHandler(new GetUriTemplateHandler());
}
}

How to handle HttpClientException properly

I have a webservice which gets data from other webservice and return back to the browser.
I want to hide internal client errors
Want to throw 404, 400 etc which
are returned from the webservice in the below method.
How to resolve this problem in a neat way?
Option 1 or Option 2 is clean way?
Option 1
public <T> Optional<T> get(String url, Class<T> responseType) {
String fullUrl = url;
LOG.info("Retrieving data from url: "+fullUrl);
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(ImmutableList.of(MediaType.APPLICATION_JSON));
headers.add("Authorization", "Basic " + httpAuthCredentials);
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<T> exchange = restTemplate.exchange(fullUrl, HttpMethod.GET, request, responseType);
if(exchange !=null)
return Optional.of(exchange.getBody());
} catch (HttpClientErrorException e) {
LOG.error("Client Exception ", e);
throw new HttpClientError("Client Exception: "+e.getStatusCode());
}
return Optional.empty();
}
(or)
Option 2
public <T> Optional<T> get(String url, Class<T> responseType) {
String fullUrl = url;
LOG.info("Retrieving data from url: "+fullUrl);
try {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(ImmutableList.of(MediaType.APPLICATION_JSON));
headers.add("Authorization", "Basic " + httpAuthCredentials);
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<T> exchange = restTemplate.exchange(fullUrl, HttpMethod.GET, request, responseType);
if(exchange !=null)
return Optional.of(exchange.getBody());
throw new RestClientResponseException("", 400, "", null, null, null);
} catch (HttpStatusCodeException e) {
LOG.error("HttpStatusCodeException ", e);
throw new RestClientResponseException(e.getMessage(), e.getStatusCode().value(), e.getStatusText(), e.getResponseHeaders(), e.getResponseBodyAsByteArray(), Charset.defaultCharset());
}
return Optional.empty();
}
I have written a sample ResponseErrorHandler for you,
public class RestTemplateClientErrorHandler implements ResponseErrorHandler {
private static final Logger logger = LoggerFactory.getLogger(RestTemplateClientErrorHandler.class);
#Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
return RestUtil.isError(clientHttpResponse.getStatusCode());
}
#Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
String responseBody = "";
if(clientHttpResponse != null && clientHttpResponse.getBody() != null){
responseBody = IOUtils.toString(clientHttpResponse.getBody());
}
switch(clientHttpResponse.getRawStatusCode()){
case 404:
logger.error("Entity not found. Message: {}. Status: {} ",responseBody,clientHttpResponse.getStatusCode());
throw new RestClientResponseException(responseBody);
case 400:
logger.error("Bad request for entity. Message: {}. Status: {}",responseBody, clientHttpResponse.getStatusCode());
throw new RestClientResponseException(StringUtils.EMPTY, 400,StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY);
default:
logger.error("Unexpected HTTP status: {} received when trying to delete entity in device repository.", clientHttpResponse.getStatusCode());
throw new RestClientResponseException(responseBody);
}
}
public static class RestUtil {
private RestUtil() {
throw new IllegalAccessError("Utility class");
}
public static boolean isError(HttpStatus status) {
HttpStatus.Series series = status.series();
return HttpStatus.Series.CLIENT_ERROR.equals(series)
|| HttpStatus.Series.SERVER_ERROR.equals(series);
}
}
}
Note : This is common ResponseErrorHandler for your restTemplate and it will catch all the exceptions thrown by restTemplate you don't require try,catch block in each method and you don't need to catch "HttpStatusCodeException" or any other exception.
Please use the below code to register this ErrorHandler.
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new RestTemplateClientErrorHandler());
You can also find examples here.
You can refactor your client class like this,
public <T> Optional<T> get(String url, Class<T> responseType) {
String fullUrl = url;
LOG.info("Retrieving data from url: "+fullUrl);
HttpHeaders headers = new HttpHeaders();
headers.setAccept(ImmutableList.of(MediaType.APPLICATION_JSON));
headers.add("Authorization", "Basic " + httpAuthCredentials);
HttpEntity<String> request = new HttpEntity<>(headers);
ResponseEntity<T> exchange = restTemplate.exchange(fullUrl, HttpMethod.GET, request, responseType);
if(exchange !=null)
return Optional.of(exchange.getBody());
return Optional.empty();
}
So your method not looking beautiful now ? Suggestions welcome.

RestTemplate send file as bytes from one controller to another

assume we have a one controller on third party service which accepts multipart files and its code is like (assume it's running on localhost:9090)
#RequestMapping("/file")
#RestController
public class FileController {
#RequestMapping(value = "/load", method = RequestMethod.POST)
public String getFile(#RequestPart("file") MultipartFile file){
return file.getName();
}
}
The question is:
How write a correct code in my controller, with RestTemplate, that calls the third party service, with file in body?
A few examples that do not work:
First one:
#RequestMapping("/file")
#RestController
public class FileSendController {
private RestTemplate restTemplate = new RestTemplate();
#RequestMapping(value = "/send", method = RequestMethod.POST)
public ResponseEntity<?> sendFile(#RequestPart MultipartFile file)
throws IOException {
String url = "http://localhost:9090/file/load";
return restTemplate.postForEntity(url, file.getBytes(),
ResponseEntity.class);
}
}
Second one:
#RequestMapping("/file")
#RestController
public class FileSendController {
private RestTemplate restTemplate = new RestTemplate();
#RequestMapping(value = "/send", method = RequestMethod.POST)
public ResponseEntity<?> sendFile(#RequestPart MultipartFile file)
throws IOException {
String url = "http://localhost:9090/file/load";
byte[] bytes = file.getBytes();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<byte[]> entity = new HttpEntity<>(bytes, headers);
return restTemplate.exchange(url, HttpMethod.POST,
entity,ResponseEntity.class);
}
}
One restriction: i should load files from memory, so it forces me to use byte[]
All of this examples throw 500 on third party service with message:
org.springframework.web.multipart.MultipartException: Current request is not
a multipart request.
Thanks for your advices.
Try this:
MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
#Override
public String getFilename() {
return file.getName();
}
};
data.add("file", resource);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data, requestHeaders);
final ResponseEntity<Response<ImportDto>> responseEntity = restTemplate.exchange(url,
HttpMethod.POST, requestEntity, new ParameterizedTypeReference<Response<ResponseDto>>(){});

How to make a POST with empty body using OKHttp

I'm facing a different Api Service which I have to request using POST but with no body content, I'm sending a image converted to base64, I've been searching about that issue and I found this "solution", which it didn't work :
1 :
RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request.Builder formBody = new Request.Builder().url(url).method("POST",reqbody).header("Content-Length", "0");
2 :
request = new Request.Builder()
.url(BASE_URL + route)
.method("POST", RequestBody.create(null, new byte[0]))
.post(requestBody)
.build();
Even I explicit saying that is a POST method, it keeps send a GET request and not a POST request. Thanks!
My Activity :
public String SendImage(String image64) throws IOException{
//RequestBody reqbody = RequestBody.create(null, new byte[0]);
Request request = new Request.Builder()
.url("http://ap.imagensbrasil.org/api/1/upload/?key=9c9dfe77cd3bdbaa7220c6bbaf7452e7&source=" + image64 + "&format=txt")
.method("POST", RequestBody.create(null, new byte[0]))
.header("Content-Length", "0")
.build();
OkHttpClient Client = client.newBuilder() .readTimeout(25, TimeUnit.SECONDS).build();
Response response = Client.newCall(request).execute();
return response.body().string();
}
It is working on retrofit , so if you continue with Retrofit v2.0 you can use this :
public class Base64EncodeRequestInterceptor implements Interceptor {
#Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder();
if (originalRequest.method().equalsIgnoreCase(POST)) {
builder = originalRequest.newBuilder()
.method(originalRequest.method(), encode(originalRequest.body()));
}
return chain.proceed(builder.build());
}
private RequestBody encode(RequestBody body) {
return new RequestBody() {
#Override
public MediaType contentType() {
return body.contentType();
}
#Override
public void writeTo(BufferedSink sink) throws IOException {
Buffer buffer = new Buffer();
body.writeTo(buffer);
byte[] encoded = Base64.encode(buffer.readByteArray(), Base64.DEFAULT);
sink.write(encoded);
buffer.close();
sink.close();
}
};
}
}

Categories