mobile/android/thirdparty/ch/boye/httpclientandroidlib/cookie/CookieSpecRegistry.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * ====================================================================
michael@0 3 * Licensed to the Apache Software Foundation (ASF) under one
michael@0 4 * or more contributor license agreements. See the NOTICE file
michael@0 5 * distributed with this work for additional information
michael@0 6 * regarding copyright ownership. The ASF licenses this file
michael@0 7 * to you under the Apache License, Version 2.0 (the
michael@0 8 * "License"); you may not use this file except in compliance
michael@0 9 * with the License. You may obtain a copy of the License at
michael@0 10 *
michael@0 11 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 12 *
michael@0 13 * Unless required by applicable law or agreed to in writing,
michael@0 14 * software distributed under the License is distributed on an
michael@0 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0 16 * KIND, either express or implied. See the License for the
michael@0 17 * specific language governing permissions and limitations
michael@0 18 * under the License.
michael@0 19 * ====================================================================
michael@0 20 *
michael@0 21 * This software consists of voluntary contributions made by many
michael@0 22 * individuals on behalf of the Apache Software Foundation. For more
michael@0 23 * information on the Apache Software Foundation, please see
michael@0 24 * <http://www.apache.org/>.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 package ch.boye.httpclientandroidlib.cookie;
michael@0 29
michael@0 30 import java.util.ArrayList;
michael@0 31 import java.util.List;
michael@0 32 import java.util.Locale;
michael@0 33 import java.util.Map;
michael@0 34 import java.util.concurrent.ConcurrentHashMap;
michael@0 35
michael@0 36 import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
michael@0 37
michael@0 38 import ch.boye.httpclientandroidlib.params.HttpParams;
michael@0 39
michael@0 40 /**
michael@0 41 * Cookie specification registry that can be used to obtain the corresponding
michael@0 42 * cookie specification implementation for a given type of type or version of
michael@0 43 * cookie.
michael@0 44 *
michael@0 45 *
michael@0 46 * @since 4.0
michael@0 47 */
michael@0 48 @ThreadSafe
michael@0 49 public final class CookieSpecRegistry {
michael@0 50
michael@0 51 private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs;
michael@0 52
michael@0 53 public CookieSpecRegistry() {
michael@0 54 super();
michael@0 55 this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>();
michael@0 56 }
michael@0 57
michael@0 58 /**
michael@0 59 * Registers a {@link CookieSpecFactory} with the given identifier.
michael@0 60 * If a specification with the given name already exists it will be overridden.
michael@0 61 * This nameis the same one used to retrieve the {@link CookieSpecFactory}
michael@0 62 * from {@link #getCookieSpec(String)}.
michael@0 63 *
michael@0 64 * @param name the identifier for this specification
michael@0 65 * @param factory the {@link CookieSpecFactory} class to register
michael@0 66 *
michael@0 67 * @see #getCookieSpec(String)
michael@0 68 */
michael@0 69 public void register(final String name, final CookieSpecFactory factory) {
michael@0 70 if (name == null) {
michael@0 71 throw new IllegalArgumentException("Name may not be null");
michael@0 72 }
michael@0 73 if (factory == null) {
michael@0 74 throw new IllegalArgumentException("Cookie spec factory may not be null");
michael@0 75 }
michael@0 76 registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
michael@0 77 }
michael@0 78
michael@0 79 /**
michael@0 80 * Unregisters the {@link CookieSpecFactory} with the given ID.
michael@0 81 *
michael@0 82 * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
michael@0 83 */
michael@0 84 public void unregister(final String id) {
michael@0 85 if (id == null) {
michael@0 86 throw new IllegalArgumentException("Id may not be null");
michael@0 87 }
michael@0 88 registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
michael@0 89 }
michael@0 90
michael@0 91 /**
michael@0 92 * Gets the {@link CookieSpec cookie specification} with the given ID.
michael@0 93 *
michael@0 94 * @param name the {@link CookieSpec cookie specification} identifier
michael@0 95 * @param params the {@link HttpParams HTTP parameters} for the cookie
michael@0 96 * specification.
michael@0 97 *
michael@0 98 * @return {@link CookieSpec cookie specification}
michael@0 99 *
michael@0 100 * @throws IllegalStateException if a policy with the given name cannot be found
michael@0 101 */
michael@0 102 public CookieSpec getCookieSpec(final String name, final HttpParams params)
michael@0 103 throws IllegalStateException {
michael@0 104
michael@0 105 if (name == null) {
michael@0 106 throw new IllegalArgumentException("Name may not be null");
michael@0 107 }
michael@0 108 CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
michael@0 109 if (factory != null) {
michael@0 110 return factory.newInstance(params);
michael@0 111 } else {
michael@0 112 throw new IllegalStateException("Unsupported cookie spec: " + name);
michael@0 113 }
michael@0 114 }
michael@0 115
michael@0 116 /**
michael@0 117 * Gets the {@link CookieSpec cookie specification} with the given name.
michael@0 118 *
michael@0 119 * @param name the {@link CookieSpec cookie specification} identifier
michael@0 120 *
michael@0 121 * @return {@link CookieSpec cookie specification}
michael@0 122 *
michael@0 123 * @throws IllegalStateException if a policy with the given name cannot be found
michael@0 124 */
michael@0 125 public CookieSpec getCookieSpec(final String name)
michael@0 126 throws IllegalStateException {
michael@0 127 return getCookieSpec(name, null);
michael@0 128 }
michael@0 129
michael@0 130 /**
michael@0 131 * Obtains a list containing the names of all registered {@link CookieSpec cookie
michael@0 132 * specs}.
michael@0 133 *
michael@0 134 * Note that the DEFAULT policy (if present) is likely to be the same
michael@0 135 * as one of the other policies, but does not have to be.
michael@0 136 *
michael@0 137 * @return list of registered cookie spec names
michael@0 138 */
michael@0 139 public List<String> getSpecNames(){
michael@0 140 return new ArrayList<String>(registeredSpecs.keySet());
michael@0 141 }
michael@0 142
michael@0 143 /**
michael@0 144 * Populates the internal collection of registered {@link CookieSpec cookie
michael@0 145 * specs} with the content of the map passed as a parameter.
michael@0 146 *
michael@0 147 * @param map cookie specs
michael@0 148 */
michael@0 149 public void setItems(final Map<String, CookieSpecFactory> map) {
michael@0 150 if (map == null) {
michael@0 151 return;
michael@0 152 }
michael@0 153 registeredSpecs.clear();
michael@0 154 registeredSpecs.putAll(map);
michael@0 155 }
michael@0 156
michael@0 157 }

mercurial