<commit_before>/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.rest.policy;

import com.microsoft.rest.http.HttpHeader;
import com.microsoft.rest.http.HttpRequest;
import com.microsoft.rest.http.HttpResponse;
import rx.Single;
import rx.exceptions.Exceptions;
import rx.functions.Func1;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A RequestPolicy which stores cookies based on the response Set-Cookie header and adds cookies to requests.
 */
public final class AddCookiesPolicy implements RequestPolicy {
    private final CookieHandler cookies;
    private final RequestPolicy next;

    private AddCookiesPolicy(CookieHandler cookies, RequestPolicy next) {
        this.cookies = cookies;
        this.next = next;
    }

    @Override
    public Single<HttpResponse> sendAsync(HttpRequest request) {
        try {
            final URI uri = new URI(request.url());

            Map<String, List<String>> cookieHeaders = new HashMap<>();
            for (HttpHeader header : request.headers()) {
                cookieHeaders.put(header.name(), Arrays.asList(request.headers().values(header.name())));
            }

            Map<String, List<String>> requestCookies = cookies.get(uri, cookieHeaders);
            for (Map.Entry<String, List<String>> entry : requestCookies.entrySet()) {
                for (String headerValue : entry.getValue()) {
                    request.headers().add(entry.getKey(), headerValue);
                }
            }

            return next.sendAsync(request).map(new Func1<HttpResponse, HttpResponse>() {
                @Override
                public HttpResponse call(HttpResponse httpResponse) {
                    Map<String, List<String>> responseHeaders = new HashMap<>();
                    for (HttpHeader header : httpResponse.headers()) {
                        responseHeaders.put(header.name(), Arrays.asList(header.values()));
                    }

                    try {
                        cookies.put(uri, responseHeaders);
                    } catch (IOException e) {
                        throw Exceptions.propagate(e);
                    }

                    return httpResponse;
                }
            });
        } catch (URISyntaxException | IOException e) {
            return Single.error(e);
        }
    }

    /**
     * Factory for creating AddCookiesPolicy.
     */<commit_msg>Get single header value when putting cookies to work around commas in dates for Expires field<commit_after>/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 */

package com.microsoft.rest.policy;

import com.microsoft.rest.http.HttpHeader;
import com.microsoft.rest.http.HttpRequest;
import com.microsoft.rest.http.HttpResponse;
import rx.Single;
import rx.exceptions.Exceptions;
import rx.functions.Func1;

import java.io.IOException;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A RequestPolicy which stores cookies based on the response Set-Cookie header and adds cookies to requests.
 */
public final class AddCookiesPolicy implements RequestPolicy {
    private final CookieHandler cookies;
    private final RequestPolicy next;

    private AddCookiesPolicy(CookieHandler cookies, RequestPolicy next) {
        this.cookies = cookies;
        this.next = next;
    }

    @Override
    public Single<HttpResponse> sendAsync(HttpRequest request) {
        try {
            final URI uri = new URI(request.url());

            Map<String, List<String>> cookieHeaders = new HashMap<>();
            for (HttpHeader header : request.headers()) {
                cookieHeaders.put(header.name(), Arrays.asList(request.headers().values(header.name())));
            }

            Map<String, List<String>> requestCookies = cookies.get(uri, cookieHeaders);
            for (Map.Entry<String, List<String>> entry : requestCookies.entrySet()) {
                for (String headerValue : entry.getValue()) {
                    request.headers().add(entry.getKey(), headerValue);
                }
            }

            return next.sendAsync(request).map(new Func1<HttpResponse, HttpResponse>() {
                @Override
                public HttpResponse call(HttpResponse httpResponse) {
                    Map<String, List<String>> responseHeaders = new HashMap<>();
                    for (HttpHeader header : httpResponse.headers()) {
                        responseHeaders.put(header.name(), Collections.singletonList(header.value()));
                    }

                    try {
                        cookies.put(uri, responseHeaders);
                    } catch (IOException e) {
                        throw Exceptions.propagate(e);
                    }

                    return httpResponse;
                }
            });
        } catch (URISyntaxException | IOException e) {
            return Single.error(e);
        }
    }

    /**
     * Factory for creating AddCookiesPolicy.
     */
{'fraction_non_alphanumeric': 0.09983868076716257, 'fraction_numerical': 0.000716974368166338, 'mean_word_length': 2.2708089097303636, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '19395', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1359, 'n_words': 359}