1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.hc.client5.http.cookie;
29
30 import java.util.List;
31
32 import org.apache.hc.core5.http.Header;
33
34 /**
35 * Defines the cookie management specification.
36 * <p>Cookie management specification must define
37 * <ul>
38 * <li> rules of parsing "Set-Cookie" header
39 * <li> rules of validation of parsed cookies
40 * <li> formatting of "Cookie" header
41 * </ul>
42 * for a given host, port and path of origin
43 *
44 * @since 4.0
45 */
46 public interface CookieSpec {
47
48 /**
49 * Parse the {@code "Set-Cookie"} Header into an array of Cookies.
50 *
51 * <p>This method will not perform the validation of the resultant
52 * {@link Cookie}s</p>
53 *
54 * @see #validate
55 *
56 * @param header the {@code Set-Cookie} received from the server
57 * @param origin details of the cookie origin
58 * @return an array of {@code Cookie}s parsed from the header
59 * @throws MalformedCookieException if an exception occurs during parsing
60 */
61 List<Cookie> parse(Header header, CookieOrigin origin) throws MalformedCookieException;
62
63 /**
64 * Validate the cookie according to validation rules defined by the
65 * cookie specification.
66 *
67 * @param cookie the Cookie to validate
68 * @param origin details of the cookie origin
69 * @throws MalformedCookieException if the cookie is invalid
70 */
71 void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException;
72
73 /**
74 * Determines if a Cookie matches the target location.
75 *
76 * @param cookie the Cookie to be matched
77 * @param origin the target to test against
78 *
79 * @return {@code true} if the cookie should be submitted with a request
80 * with given attributes, {@code false} otherwise.
81 */
82 boolean match(Cookie cookie, CookieOrigin origin);
83
84 /**
85 * Create {@code "Cookie"} headers for an array of Cookies.
86 *
87 * @param cookies the Cookies format into a Cookie header
88 * @return a Header for the given Cookies.
89 * @throws IllegalArgumentException if an input parameter is illegal
90 */
91 List<Header> formatCookies(List<Cookie> cookies);
92
93 }