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 org.apache.http.HttpConnection;
031import org.apache.http.HttpHost;
032import org.apache.http.HttpRequest;
033import org.apache.http.HttpResponse;
034import org.apache.http.util.Args;
035
036/**
037 * Implementation of {@link HttpContext} that provides convenience
038 * setters for user assignable attributes and getter for readable attributes.
039 *
040 * @since 4.3
041 */
042public class HttpCoreContext implements HttpContext {
043
044    /**
045     * Attribute name of a {@link org.apache.http.HttpConnection} object that
046     * represents the actual HTTP connection.
047     */
048    public static final String HTTP_CONNECTION  = "http.connection";
049
050    /**
051     * Attribute name of a {@link org.apache.http.HttpRequest} object that
052     * represents the actual HTTP request.
053     */
054    public static final String HTTP_REQUEST     = "http.request";
055
056    /**
057     * Attribute name of a {@link org.apache.http.HttpResponse} object that
058     * represents the actual HTTP response.
059     */
060    public static final String HTTP_RESPONSE    = "http.response";
061
062    /**
063     * Attribute name of a {@link org.apache.http.HttpHost} object that
064     * represents the connection target.
065     */
066    public static final String HTTP_TARGET_HOST = "http.target_host";
067
068    /**
069     * Attribute name of a {@link Boolean} object that represents the
070     * the flag indicating whether the actual request has been fully transmitted
071     * to the target host.
072     */
073    public static final String HTTP_REQ_SENT    = "http.request_sent";
074
075    public static HttpCoreContext create() {
076        return new HttpCoreContext(new BasicHttpContext());
077    }
078
079    public static HttpCoreContext adapt(final HttpContext context) {
080        Args.notNull(context, "HTTP context");
081        if (context instanceof HttpCoreContext) {
082            return (HttpCoreContext) context;
083        } else {
084            return new HttpCoreContext(context);
085        }
086    }
087
088    private final HttpContext context;
089
090    public HttpCoreContext(final HttpContext context) {
091        super();
092        this.context = context;
093    }
094
095    public HttpCoreContext() {
096        super();
097        this.context = new BasicHttpContext();
098    }
099
100    @Override
101    public Object getAttribute(final String id) {
102        return context.getAttribute(id);
103    }
104
105    @Override
106    public void setAttribute(final String id, final Object obj) {
107        context.setAttribute(id, obj);
108    }
109
110    @Override
111    public Object removeAttribute(final String id) {
112        return context.removeAttribute(id);
113    }
114
115    public <T> T getAttribute(final String attribname, final Class<T> clazz) {
116        Args.notNull(clazz, "Attribute class");
117        final Object obj = getAttribute(attribname);
118        if (obj == null) {
119            return null;
120        }
121        return clazz.cast(obj);
122    }
123
124    public <T extends HttpConnection> T getConnection(final Class<T> clazz) {
125        return getAttribute(HTTP_CONNECTION, clazz);
126    }
127
128    public HttpConnection getConnection() {
129        return getAttribute(HTTP_CONNECTION, HttpConnection.class);
130    }
131
132    public HttpRequest getRequest() {
133        return getAttribute(HTTP_REQUEST, HttpRequest.class);
134    }
135
136    public boolean isRequestSent() {
137        final Boolean b = getAttribute(HTTP_REQ_SENT, Boolean.class);
138        return b != null && b.booleanValue();
139    }
140
141    public HttpResponse getResponse() {
142        return getAttribute(HTTP_RESPONSE, HttpResponse.class);
143    }
144
145    public void setTargetHost(final HttpHost host) {
146        setAttribute(HTTP_TARGET_HOST, host);
147    }
148
149    public HttpHost getTargetHost() {
150        return getAttribute(HTTP_TARGET_HOST, HttpHost.class);
151    }
152
153}