Closed sendreams closed 3 years ago
i can not find any useful method on HttpRequestExecution class. how to forward the request? tks
public HttpResponse handle(HttpRequest request, HttpRequestExecution execution) { // get the cache file in redis byte[] file = getTile(request); if (file == null) { String uri = processorRepository.getFree(); // forword to other site String dest = buildForwardUri(request, uri); request.setUri(URI.create(dest)); logger.debug("request forward to {}", dest); HttpResponse response = execution.execute(request); return response; } else { // direct response to browser // HttpResponse response = execution.execute(request); // return response; return null; } }
in the Interceptor, how can i direct response file to browser?
`public HttpResponse handle(HttpRequest request, HttpRequestExecution execution) {
byte[] file = getTile(request);
if (file == null) {
// no cache, forward to other site
String uri = processorRepository.getFree();
String dest = buildForwardUri(request, uri);
request.setUri(URI.create(dest));
logger.debug("request forward to {}", dest);
HttpResponse response = execution.execute(request);
return response;
}
else {
// get the cache and response to browser
return null;
}
}`
the code format is not correct, show it again.
Hi I haven't tried it but this (or something similar) should work:
byte[] imageFile = null; // Get your file
HttpHeaders headers = new HttpHeaders();
headers.setContentDisposition(ContentDisposition.builder("inline").filename("filename.ext").build());
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // Or other, depends on the file type: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
HttpResponse response = new HttpResponse(HttpStatus.OK);
response.setHeaders(headers);
response.setBody(imageFile);
return response;
many thanks, the code can be working.
i have been created a custom Interceptor, which to forward the request to several destination website, i need dynamic decide which site should be forward to.
`public class CacheInterceptor implements RequestForwardingInterceptor { private ProcessorRepository processorRepository;
}`