< prev index next >

src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/CookieFilter.java

Print this page


   1 /*
   2  * Copyright (c) 2015, 2016, 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;
  27 
  28 import java.io.IOException;
  29 import java.net.CookieManager;
  30 import java.util.List;
  31 import java.util.Map;
  32 import java.util.Optional;
  33 import jdk.incubator.http.internal.common.HttpHeadersImpl;
  34 import jdk.incubator.http.internal.common.Log;
  35 
  36 class CookieFilter implements HeaderFilter {
  37 
  38     public CookieFilter() {
  39     }
  40 
  41     @Override
  42     public void request(HttpRequestImpl r, MultiExchange<?,?> e) throws IOException {
  43         HttpClientImpl client = e.client();
  44         Optional<CookieManager> cookieManOpt = client.cookieManager();
  45         if (cookieManOpt.isPresent()) {
  46             CookieManager cookieMan = cookieManOpt.get();
  47             Map<String,List<String>> userheaders = r.getUserHeaders().map();
  48             Map<String,List<String>> cookies = cookieMan.get(r.uri(), userheaders);
  49 
  50             // add the returned cookies
  51             HttpHeadersImpl systemHeaders = r.getSystemHeaders();
  52             if (cookies.isEmpty()) {
  53                 Log.logTrace("Request: no cookie to add for {0}",
  54                              r.uri());
  55             } else {
  56                 Log.logTrace("Request: adding cookies for {0}",
  57                              r.uri());
  58             }
  59             for (String hdrname : cookies.keySet()) {
  60                 List<String> vals = cookies.get(hdrname);
  61                 for (String val : vals) {
  62                     systemHeaders.addHeader(hdrname, val);
  63                 }
  64             }
  65         } else {
  66             Log.logTrace("Request: No cookie manager found for {0}",
  67                          r.uri());
  68         }
  69     }
  70 
  71     @Override
  72     public HttpRequestImpl response(Response r) throws IOException {
  73         HttpHeaders hdrs = r.headers();
  74         HttpRequestImpl request = r.request();
  75         Exchange<?> e = r.exchange;
  76         Log.logTrace("Response: processing cookies for {0}", request.uri());
  77         Optional<CookieManager> cookieManOpt = e.client().cookieManager();
  78         if (cookieManOpt.isPresent()) {
  79             CookieManager cookieMan = cookieManOpt.get();
  80             Log.logTrace("Response: parsing cookies from {0}", hdrs.map());
  81             cookieMan.put(request.uri(), hdrs.map());
  82         } else {
  83             Log.logTrace("Response: No cookie manager found for {0}",
  84                          request.uri());
  85         }
  86         return null;
  87     }
  88 }
   1 /*
   2  * Copyright (c) 2015, 2017, 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;
  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.internal.common.HttpHeadersImpl;
  34 import jdk.incubator.http.internal.common.Log;
  35 
  36 class CookieFilter implements HeaderFilter {
  37 
  38     public CookieFilter() {
  39     }
  40 
  41     @Override
  42     public void request(HttpRequestImpl r, MultiExchange<?,?> e) throws IOException {
  43         HttpClientImpl client = e.client();
  44         Optional<CookieHandler> cookieHandlerOpt = client.cookieHandler();
  45         if (cookieHandlerOpt.isPresent()) {
  46             CookieHandler cookieHandler = cookieHandlerOpt.get();
  47             Map<String,List<String>> userheaders = r.getUserHeaders().map();
  48             Map<String,List<String>> cookies = cookieHandler.get(r.uri(), userheaders);
  49 
  50             // add the returned cookies
  51             HttpHeadersImpl systemHeaders = r.getSystemHeaders();
  52             if (cookies.isEmpty()) {
  53                 Log.logTrace("Request: no cookie to add for {0}",
  54                              r.uri());
  55             } else {
  56                 Log.logTrace("Request: adding cookies for {0}",
  57                              r.uri());
  58             }
  59             for (String hdrname : cookies.keySet()) {
  60                 List<String> vals = cookies.get(hdrname);
  61                 for (String val : vals) {
  62                     systemHeaders.addHeader(hdrname, val);
  63                 }
  64             }
  65         } else {
  66             Log.logTrace("Request: No cookie manager found for {0}",
  67                          r.uri());
  68         }
  69     }
  70 
  71     @Override
  72     public HttpRequestImpl response(Response r) throws IOException {
  73         HttpHeaders hdrs = r.headers();
  74         HttpRequestImpl request = r.request();
  75         Exchange<?> e = r.exchange;
  76         Log.logTrace("Response: processing cookies for {0}", request.uri());
  77         Optional<CookieHandler> cookieHandlerOpt = e.client().cookieHandler();
  78         if (cookieHandlerOpt.isPresent()) {
  79             CookieHandler cookieHandler = cookieHandlerOpt.get();
  80             Log.logTrace("Response: parsing cookies from {0}", hdrs.map());
  81             cookieHandler.put(request.uri(), hdrs.map());
  82         } else {
  83             Log.logTrace("Response: No cookie manager found for {0}",
  84                          request.uri());
  85         }
  86         return null;
  87     }
  88 }
< prev index next >