michael@0: /* michael@0: * ==================================================================== michael@0: * Licensed to the Apache Software Foundation (ASF) under one michael@0: * or more contributor license agreements. See the NOTICE file michael@0: * distributed with this work for additional information michael@0: * regarding copyright ownership. The ASF licenses this file michael@0: * to you under the Apache License, Version 2.0 (the michael@0: * "License"); you may not use this file except in compliance michael@0: * with the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, michael@0: * software distributed under the License is distributed on an michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY michael@0: * KIND, either express or implied. See the License for the michael@0: * specific language governing permissions and limitations michael@0: * under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.params; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpVersion; michael@0: import ch.boye.httpclientandroidlib.ProtocolVersion; michael@0: import ch.boye.httpclientandroidlib.protocol.HTTP; michael@0: michael@0: /** michael@0: * Utility class for accessing protocol parameters in {@link HttpParams}. michael@0: * michael@0: * @since 4.0 michael@0: * michael@0: * @see CoreProtocolPNames michael@0: */ michael@0: public final class HttpProtocolParams implements CoreProtocolPNames { michael@0: michael@0: private HttpProtocolParams() { michael@0: super(); michael@0: } michael@0: michael@0: /** michael@0: * Obtains value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter. michael@0: * If not set, defaults to US-ASCII. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @return HTTP element charset. michael@0: */ michael@0: public static String getHttpElementCharset(final HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: String charset = (String) params.getParameter michael@0: (CoreProtocolPNames.HTTP_ELEMENT_CHARSET); michael@0: if (charset == null) { michael@0: charset = HTTP.DEFAULT_PROTOCOL_CHARSET; michael@0: } michael@0: return charset; michael@0: } michael@0: michael@0: /** michael@0: * Sets value of the {@link CoreProtocolPNames#HTTP_ELEMENT_CHARSET} parameter. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @param charset HTTP element charset. michael@0: */ michael@0: public static void setHttpElementCharset(final HttpParams params, final String charset) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: params.setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, charset); michael@0: } michael@0: michael@0: /** michael@0: * Obtains value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter. michael@0: * If not set, defaults to ISO-8859-1. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @return HTTP content charset. michael@0: */ michael@0: public static String getContentCharset(final HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: String charset = (String) params.getParameter michael@0: (CoreProtocolPNames.HTTP_CONTENT_CHARSET); michael@0: if (charset == null) { michael@0: charset = HTTP.DEFAULT_CONTENT_CHARSET; michael@0: } michael@0: return charset; michael@0: } michael@0: michael@0: /** michael@0: * Sets value of the {@link CoreProtocolPNames#HTTP_CONTENT_CHARSET} parameter. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @param charset HTTP content charset. michael@0: */ michael@0: public static void setContentCharset(final HttpParams params, final String charset) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, charset); michael@0: } michael@0: michael@0: /** michael@0: * Obtains value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter. michael@0: * If not set, defaults to {@link HttpVersion#HTTP_1_1}. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @return HTTP protocol version. michael@0: */ michael@0: public static ProtocolVersion getVersion(final HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: Object param = params.getParameter michael@0: (CoreProtocolPNames.PROTOCOL_VERSION); michael@0: if (param == null) { michael@0: return HttpVersion.HTTP_1_1; michael@0: } michael@0: return (ProtocolVersion)param; michael@0: } michael@0: michael@0: /** michael@0: * Sets value of the {@link CoreProtocolPNames#PROTOCOL_VERSION} parameter. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @param version HTTP protocol version. michael@0: */ michael@0: public static void setVersion(final HttpParams params, final ProtocolVersion version) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, version); michael@0: } michael@0: michael@0: /** michael@0: * Obtains value of the {@link CoreProtocolPNames#USER_AGENT} parameter. michael@0: * If not set, returns null. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @return User agent string. michael@0: */ michael@0: public static String getUserAgent(final HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: return (String) params.getParameter(CoreProtocolPNames.USER_AGENT); michael@0: } michael@0: michael@0: /** michael@0: * Sets value of the {@link CoreProtocolPNames#USER_AGENT} parameter. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @param useragent User agent string. michael@0: */ michael@0: public static void setUserAgent(final HttpParams params, final String useragent) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: params.setParameter(CoreProtocolPNames.USER_AGENT, useragent); michael@0: } michael@0: michael@0: /** michael@0: * Obtains value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter. michael@0: * If not set, returns false. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @return User agent string. michael@0: */ michael@0: public static boolean useExpectContinue(final HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: return params.getBooleanParameter michael@0: (CoreProtocolPNames.USE_EXPECT_CONTINUE, false); michael@0: } michael@0: michael@0: /** michael@0: * Sets value of the {@link CoreProtocolPNames#USE_EXPECT_CONTINUE} parameter. michael@0: * michael@0: * @param params HTTP parameters. michael@0: * @param b expect-continue flag. michael@0: */ michael@0: public static void setUseExpectContinue(final HttpParams params, boolean b) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("HTTP parameters may not be null"); michael@0: } michael@0: params.setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, b); michael@0: } michael@0: michael@0: }