1/*
2 * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/params/HttpProtocolParams.java $
3 * $Revision: 576089 $
4 * $Date: 2007-09-16 05:39:56 -0700 (Sun, 16 Sep 2007) $
5 *
6 * ====================================================================
7 * Licensed to the Apache Software Foundation (ASF) under one
8 * or more contributor license agreements.  See the NOTICE file
9 * distributed with this work for additional information
10 * regarding copyright ownership.  The ASF licenses this file
11 * to you under the Apache License, Version 2.0 (the
12 * "License"); you may not use this file except in compliance
13 * with the License.  You may obtain a copy of the License at
14 *
15 *   http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing,
18 * software distributed under the License is distributed on an
19 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20 * KIND, either express or implied.  See the License for the
21 * specific language governing permissions and limitations
22 * under the License.
23 * ====================================================================
24 *
25 * This software consists of voluntary contributions made by many
26 * individuals on behalf of the Apache Software Foundation.  For more
27 * information on the Apache Software Foundation, please see
28 * <http://www.apache.org/>.
29 *
30 */
31
32package org.apache.http.params;
33
34import org.apache.http.HttpVersion;
35import org.apache.http.ProtocolVersion;
36import org.apache.http.protocol.HTTP;
37
38/**
39 * This class implements an adaptor around the {@link HttpParams} interface
40 * to simplify manipulation of the HTTP protocol specific parameters.
41 * <br/>
42 * Note that the <i>implements</i> relation to {@link CoreProtocolPNames}
43 * is for compatibility with existing application code only. References to
44 * the parameter names should use the interface, not this class.
45 *
46 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
47 *
48 * @version $Revision: 576089 $
49 *
50 * @since 4.0
51 *
52 * @see CoreProtocolPNames
53 */
54public final class HttpProtocolParams implements CoreProtocolPNames {
55
56    /**
57     */
58    private HttpProtocolParams() {
59        super();
60    }
61
62    /**
63     * Returns the charset to be used for writing HTTP headers.
64     * @return The charset
65     */
66    public static String getHttpElementCharset(final HttpParams params) {
67        if (params == null) {
68            throw new IllegalArgumentException("HTTP parameters may not be null");
69        }
70        String charset = (String) params.getParameter
71            (CoreProtocolPNames.HTTP_ELEMENT_CHARSET);
72        if (charset == null) {
73            charset = HTTP.DEFAULT_PROTOCOL_CHARSET;
74        }
75        return charset;
76    }
77
78    /**
79     * Sets the charset to be used for writing HTTP headers.
80     * @param charset The charset
81     */
82    public static void setHttpElementCharset(final HttpParams params, final String charset) {
83        if (params == null) {
84            throw new IllegalArgumentException("HTTP parameters may not be null");
85        }
86        params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset);
87    }
88
89    /**
90     * Returns the default charset to be used for writing content body,
91     * when no charset explicitly specified.
92     * @return The charset
93     */
94    public static String getContentCharset(final HttpParams params) {
95        if (params == null) {
96            throw new IllegalArgumentException("HTTP parameters may not be null");
97        }
98        String charset = (String) params.getParameter
99            (CoreProtocolPNames.HTTP_CONTENT_CHARSET);
100        if (charset == null) {
101            charset = HTTP.DEFAULT_CONTENT_CHARSET;
102        }
103        return charset;
104    }
105
106    /**
107     * Sets the default charset to be used for writing content body,
108     * when no charset explicitly specified.
109     * @param charset The charset
110     */
111    public static void setContentCharset(final HttpParams params, final String charset) {
112        if (params == null) {
113            throw new IllegalArgumentException("HTTP parameters may not be null");
114        }
115        params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset);
116    }
117
118    /**
119     * Returns {@link ProtocolVersion protocol version} to be used per default.
120     *
121     * @return {@link ProtocolVersion protocol version}
122     */
123    public static ProtocolVersion getVersion(final HttpParams params) {
124        if (params == null) {
125            throw new IllegalArgumentException("HTTP parameters may not be null");
126        }
127        Object param = params.getParameter
128            (CoreProtocolPNames.PROTOCOL_VERSION);
129        if (param == null) {
130            return HttpVersion.HTTP_1_1;
131        }
132        return (ProtocolVersion)param;
133    }
134
135    /**
136     * Assigns the {@link ProtocolVersion protocol version} to be used by the
137     * HTTP methods that this collection of parameters applies to.
138     *
139     * @param version the {@link ProtocolVersion protocol version}
140     */
141    public static void setVersion(final HttpParams params, final ProtocolVersion version) {
142        if (params == null) {
143            throw new IllegalArgumentException("HTTP parameters may not be null");
144        }
145        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version);
146    }
147
148    public static String getUserAgent(final HttpParams params) {
149        if (params == null) {
150            throw new IllegalArgumentException("HTTP parameters may not be null");
151        }
152        return (String) params.getParameter(CoreProtocolPNames.USER_AGENT);
153    }
154
155    public static void setUserAgent(final HttpParams params, final String useragent) {
156        if (params == null) {
157            throw new IllegalArgumentException("HTTP parameters may not be null");
158        }
159        params.setParameter(CoreProtocolPNames.USER_AGENT, useragent);
160    }
161
162    public static boolean useExpectContinue(final HttpParams params) {
163        if (params == null) {
164            throw new IllegalArgumentException("HTTP parameters may not be null");
165        }
166        return params.getBooleanParameter
167            (CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
168    }
169
170    public static void setUseExpectContinue(final HttpParams params, boolean b) {
171        if (params == null) {
172            throw new IllegalArgumentException("HTTP parameters may not be null");
173        }
174        params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b);
175    }
176}
177