001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.protocol;
029
030import java.util.Map;
031import java.util.concurrent.ConcurrentHashMap;
032
033import org.apache.http.annotation.ThreadingBehavior;
034import org.apache.http.annotation.Contract;
035import org.apache.http.util.Args;
036
037/**
038 * Default implementation of {@link HttpContext}.
039 * <p>
040 * Please note instances of this class can be thread unsafe if the
041 * parent context is not thread safe.
042 *
043 * @since 4.0
044 */
045@Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL)
046public class BasicHttpContext implements HttpContext {
047
048    private final HttpContext parentContext;
049    private final Map<String, Object> map;
050
051    public BasicHttpContext() {
052        this(null);
053    }
054
055    public BasicHttpContext(final HttpContext parentContext) {
056        super();
057        this.map = new ConcurrentHashMap<String, Object>();
058        this.parentContext = parentContext;
059    }
060
061    @Override
062    public Object getAttribute(final String id) {
063        Args.notNull(id, "Id");
064        Object obj = this.map.get(id);
065        if (obj == null && this.parentContext != null) {
066            obj = this.parentContext.getAttribute(id);
067        }
068        return obj;
069    }
070
071    @Override
072    public void setAttribute(final String id, final Object obj) {
073        Args.notNull(id, "Id");
074        if (obj != null) {
075            this.map.put(id, obj);
076        } else {
077            this.map.remove(id);
078        }
079    }
080
081    @Override
082    public Object removeAttribute(final String id) {
083        Args.notNull(id, "Id");
084        return this.map.remove(id);
085    }
086
087    /**
088     * @since 4.2
089     */
090    public void clear() {
091        this.map.clear();
092    }
093
094    @Override
095    public String toString() {
096        return this.map.toString();
097    }
098
099}