mkopylec / charon-spring-boot-starter

Reverse proxy implementation in form of a Spring Boot starter.
Apache License 2.0
240 stars 54 forks source link

how to forward request dynmic in Interceptor? #109

Closed sendreams closed 3 years ago

sendreams commented 3 years ago

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;

public void setProcessorRepository(ProcessorRepository processorRepository) {
    this.processorRepository = processorRepository;
}

private Logger logger = LoggerFactory.getLogger(CacheInterceptor.class);

@Override
public HttpResponse forward(HttpRequest request, HttpRequestExecution execution) {
    if (execution.getMappingName().equalsIgnoreCase("tile")) {
        if (request.getMethod() == HttpMethod.POST) {  
                          // check request body 
                          // chose the destination site to forward the request (the decision will be making here)
        }
    }
    return null;
}

}`

sendreams commented 3 years ago

i can not find any useful method on HttpRequestExecution class. how to forward the request? tks

sendreams commented 3 years ago

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?

sendreams commented 3 years ago

`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.

mkopylec commented 3 years ago

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;
sendreams commented 3 years ago

many thanks, the code can be working.