RestTemplate client with cookies

后端 未结 7 701
谎友^
谎友^ 2020-12-08 15:08

I\'m writing a simple client in Java to allow reusable use of proprietary virus scanning software accessible through a RESTful API. To upload a file for scanning the API req

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 15:46

    Small update to handle sessions in a complete test with 'java.net.HttpCookie' Object.

    @Thanks Shedon

    import java.io.IOException;
    import java.net.HttpCookie;
    import java.net.URI;
    import java.util.ArrayList;
    import java.util.List;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.client.ClientHttpRequest;
    import org.springframework.http.client.ClientHttpRequestFactory;
    import org.springframework.http.client.ClientHttpResponse;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RequestCallback;
    import org.springframework.web.client.ResponseExtractor;
    import org.springframework.web.client.RestClientException;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @link https://stackoverflow.com/questions/22853321/resttemplate-client-with-cookies
     */
    @Component
    public class RestTemplateWithCookies extends RestTemplate {
    
        private final List cookies = new ArrayList<>();
    
        public RestTemplateWithCookies() {
        }
    
        public RestTemplateWithCookies(ClientHttpRequestFactory requestFactory) {
            super(requestFactory);
        }
    
        public synchronized List getCoookies() {
            return cookies;
        }
    
        public synchronized void resetCoookies() {
            cookies.clear();
        }
    
        private void processHeaders(HttpHeaders headers) {
            final List cooks = headers.get("Set-Cookie");
            if (cooks != null && !cooks.isEmpty()) {
                cooks.stream().map((c) -> HttpCookie.parse(c)).forEachOrdered((cook) -> {
                    cook.forEach((a) -> {
                        HttpCookie cookieExists = cookies.stream().filter(x -> a.getName().equals(x.getName())).findAny().orElse(null);
                        if (cookieExists != null) {
                            cookies.remove(cookieExists);
                        }
                        cookies.add(a);
                    });
                });
            }
        }
    
        @Override
        protected  T doExecute(URI url, HttpMethod method, final RequestCallback requestCallback, final ResponseExtractor responseExtractor) throws RestClientException {
            final List cookies = getCoookies();
    
            return super.doExecute(url, method, new RequestCallback() {
                @Override
                public void doWithRequest(ClientHttpRequest chr) throws IOException {
                    if (cookies != null) {
                        StringBuilder sb = new StringBuilder();
                        for (HttpCookie cookie : cookies) {
                            sb.append(cookie.getName()).append(cookie.getValue()).append(";");
                        }
                        chr.getHeaders().add("Cookie", sb.toString());
                    }
                    requestCallback.doWithRequest(chr);
                }
    
            }, new ResponseExtractor() {
                @Override
                public T extractData(ClientHttpResponse chr) throws IOException {
                    processHeaders(chr.getHeaders());
                    return responseExtractor.extractData(chr);
                }
            });
        }
    
    }
    

提交回复
热议问题