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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/cookie/CookieSpecRegistry.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,157 @@
     1.4 +/*
     1.5 + * ====================================================================
     1.6 + * Licensed to the Apache Software Foundation (ASF) under one
     1.7 + * or more contributor license agreements.  See the NOTICE file
     1.8 + * distributed with this work for additional information
     1.9 + * regarding copyright ownership.  The ASF licenses this file
    1.10 + * to you under the Apache License, Version 2.0 (the
    1.11 + * "License"); you may not use this file except in compliance
    1.12 + * with the License.  You may obtain a copy of the License at
    1.13 + *
    1.14 + *   http://www.apache.org/licenses/LICENSE-2.0
    1.15 + *
    1.16 + * Unless required by applicable law or agreed to in writing,
    1.17 + * software distributed under the License is distributed on an
    1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    1.19 + * KIND, either express or implied.  See the License for the
    1.20 + * specific language governing permissions and limitations
    1.21 + * under the License.
    1.22 + * ====================================================================
    1.23 + *
    1.24 + * This software consists of voluntary contributions made by many
    1.25 + * individuals on behalf of the Apache Software Foundation.  For more
    1.26 + * information on the Apache Software Foundation, please see
    1.27 + * <http://www.apache.org/>.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +package ch.boye.httpclientandroidlib.cookie;
    1.32 +
    1.33 +import java.util.ArrayList;
    1.34 +import java.util.List;
    1.35 +import java.util.Locale;
    1.36 +import java.util.Map;
    1.37 +import java.util.concurrent.ConcurrentHashMap;
    1.38 +
    1.39 +import ch.boye.httpclientandroidlib.annotation.ThreadSafe;
    1.40 +
    1.41 +import ch.boye.httpclientandroidlib.params.HttpParams;
    1.42 +
    1.43 +/**
    1.44 + * Cookie specification registry that can be used to obtain the corresponding
    1.45 + * cookie specification implementation for a given type of type or version of
    1.46 + * cookie.
    1.47 + *
    1.48 + *
    1.49 + * @since 4.0
    1.50 + */
    1.51 +@ThreadSafe
    1.52 +public final class CookieSpecRegistry {
    1.53 +
    1.54 +    private final ConcurrentHashMap<String,CookieSpecFactory> registeredSpecs;
    1.55 +
    1.56 +    public CookieSpecRegistry() {
    1.57 +        super();
    1.58 +        this.registeredSpecs = new ConcurrentHashMap<String,CookieSpecFactory>();
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Registers a {@link CookieSpecFactory} with the given identifier.
    1.63 +     * If a specification with the given name already exists it will be overridden.
    1.64 +     * This nameis the same one used to retrieve the {@link CookieSpecFactory}
    1.65 +     * from {@link #getCookieSpec(String)}.
    1.66 +     *
    1.67 +     * @param name the identifier for this specification
    1.68 +     * @param factory the {@link CookieSpecFactory} class to register
    1.69 +     *
    1.70 +     * @see #getCookieSpec(String)
    1.71 +     */
    1.72 +    public void register(final String name, final CookieSpecFactory factory) {
    1.73 +         if (name == null) {
    1.74 +             throw new IllegalArgumentException("Name may not be null");
    1.75 +         }
    1.76 +        if (factory == null) {
    1.77 +            throw new IllegalArgumentException("Cookie spec factory may not be null");
    1.78 +        }
    1.79 +        registeredSpecs.put(name.toLowerCase(Locale.ENGLISH), factory);
    1.80 +    }
    1.81 +
    1.82 +    /**
    1.83 +     * Unregisters the {@link CookieSpecFactory} with the given ID.
    1.84 +     *
    1.85 +     * @param id the identifier of the {@link CookieSpec cookie specification} to unregister
    1.86 +     */
    1.87 +    public void unregister(final String id) {
    1.88 +         if (id == null) {
    1.89 +             throw new IllegalArgumentException("Id may not be null");
    1.90 +         }
    1.91 +         registeredSpecs.remove(id.toLowerCase(Locale.ENGLISH));
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * Gets the {@link CookieSpec cookie specification} with the given ID.
    1.96 +     *
    1.97 +     * @param name the {@link CookieSpec cookie specification} identifier
    1.98 +     * @param params the {@link HttpParams HTTP parameters} for the cookie
    1.99 +     *  specification.
   1.100 +     *
   1.101 +     * @return {@link CookieSpec cookie specification}
   1.102 +     *
   1.103 +     * @throws IllegalStateException if a policy with the given name cannot be found
   1.104 +     */
   1.105 +    public CookieSpec getCookieSpec(final String name, final HttpParams params)
   1.106 +        throws IllegalStateException {
   1.107 +
   1.108 +        if (name == null) {
   1.109 +            throw new IllegalArgumentException("Name may not be null");
   1.110 +        }
   1.111 +        CookieSpecFactory factory = registeredSpecs.get(name.toLowerCase(Locale.ENGLISH));
   1.112 +        if (factory != null) {
   1.113 +            return factory.newInstance(params);
   1.114 +        } else {
   1.115 +            throw new IllegalStateException("Unsupported cookie spec: " + name);
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * Gets the {@link CookieSpec cookie specification} with the given name.
   1.121 +     *
   1.122 +     * @param name the {@link CookieSpec cookie specification} identifier
   1.123 +     *
   1.124 +     * @return {@link CookieSpec cookie specification}
   1.125 +     *
   1.126 +     * @throws IllegalStateException if a policy with the given name cannot be found
   1.127 +     */
   1.128 +    public CookieSpec getCookieSpec(final String name)
   1.129 +        throws IllegalStateException {
   1.130 +        return getCookieSpec(name, null);
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Obtains a list containing the names of all registered {@link CookieSpec cookie
   1.135 +     * specs}.
   1.136 +     *
   1.137 +     * Note that the DEFAULT policy (if present) is likely to be the same
   1.138 +     * as one of the other policies, but does not have to be.
   1.139 +     *
   1.140 +     * @return list of registered cookie spec names
   1.141 +     */
   1.142 +    public List<String> getSpecNames(){
   1.143 +        return new ArrayList<String>(registeredSpecs.keySet());
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Populates the internal collection of registered {@link CookieSpec cookie
   1.148 +     * specs} with the content of the map passed as a parameter.
   1.149 +     *
   1.150 +     * @param map cookie specs
   1.151 +     */
   1.152 +    public void setItems(final Map<String, CookieSpecFactory> map) {
   1.153 +        if (map == null) {
   1.154 +            return;
   1.155 +        }
   1.156 +        registeredSpecs.clear();
   1.157 +        registeredSpecs.putAll(map);
   1.158 +    }
   1.159 +
   1.160 +}

mercurial