src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/CookieFilter.java
branchhttp-client-branch
changeset 56079 d23b02f37fce
parent 56010 782b2f2d1e76
equal deleted inserted replaced
56078:6c11b48a0695 56079:d23b02f37fce
       
     1 /*
       
     2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package jdk.incubator.http.internal;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.net.CookieHandler;
       
    30 import java.util.List;
       
    31 import java.util.Map;
       
    32 import java.util.Optional;
       
    33 import jdk.incubator.http.HttpHeaders;
       
    34 import jdk.incubator.http.internal.common.HttpHeadersImpl;
       
    35 import jdk.incubator.http.internal.common.Log;
       
    36 
       
    37 class CookieFilter implements HeaderFilter {
       
    38 
       
    39     public CookieFilter() {
       
    40     }
       
    41 
       
    42     @Override
       
    43     public void request(HttpRequestImpl r, MultiExchange<?> e) throws IOException {
       
    44         HttpClientImpl client = e.client();
       
    45         Optional<CookieHandler> cookieHandlerOpt = client.cookieHandler();
       
    46         if (cookieHandlerOpt.isPresent()) {
       
    47             CookieHandler cookieHandler = cookieHandlerOpt.get();
       
    48             Map<String,List<String>> userheaders = r.getUserHeaders().map();
       
    49             Map<String,List<String>> cookies = cookieHandler.get(r.uri(), userheaders);
       
    50 
       
    51             // add the returned cookies
       
    52             HttpHeadersImpl systemHeaders = r.getSystemHeaders();
       
    53             if (cookies.isEmpty()) {
       
    54                 Log.logTrace("Request: no cookie to add for {0}",
       
    55                              r.uri());
       
    56             } else {
       
    57                 Log.logTrace("Request: adding cookies for {0}",
       
    58                              r.uri());
       
    59             }
       
    60             for (String hdrname : cookies.keySet()) {
       
    61                 List<String> vals = cookies.get(hdrname);
       
    62                 for (String val : vals) {
       
    63                     systemHeaders.addHeader(hdrname, val);
       
    64                 }
       
    65             }
       
    66         } else {
       
    67             Log.logTrace("Request: No cookie manager found for {0}",
       
    68                          r.uri());
       
    69         }
       
    70     }
       
    71 
       
    72     @Override
       
    73     public HttpRequestImpl response(Response r) throws IOException {
       
    74         HttpHeaders hdrs = r.headers();
       
    75         HttpRequestImpl request = r.request();
       
    76         Exchange<?> e = r.exchange;
       
    77         Log.logTrace("Response: processing cookies for {0}", request.uri());
       
    78         Optional<CookieHandler> cookieHandlerOpt = e.client().cookieHandler();
       
    79         if (cookieHandlerOpt.isPresent()) {
       
    80             CookieHandler cookieHandler = cookieHandlerOpt.get();
       
    81             Log.logTrace("Response: parsing cookies from {0}", hdrs.map());
       
    82             cookieHandler.put(request.uri(), hdrs.map());
       
    83         } else {
       
    84             Log.logTrace("Response: No cookie manager found for {0}",
       
    85                          request.uri());
       
    86         }
       
    87         return null;
       
    88     }
       
    89 }