| 1 |
/* |
| 2 |
* Conditions Of Use |
| 3 |
* |
| 4 |
* This software was developed by employees of the National Institute of |
| 5 |
* Standards and Technology (NIST), an agency of the Federal Government. |
| 6 |
* Pursuant to title 15 Untied States Code Section 105, works of NIST |
| 7 |
* employees are not subject to copyright protection in the United States |
| 8 |
* and are considered to be in the public domain. As a result, a formal |
| 9 |
* license is not needed to use the software. |
| 10 |
* |
| 11 |
* This software is provided by NIST as a service and is expressly |
| 12 |
* provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED |
| 13 |
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF |
| 14 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT |
| 15 |
* AND DATA ACCURACY. NIST does not warrant or make any representations |
| 16 |
* regarding the use of the software or the results thereof, including but |
| 17 |
* not limited to the correctness, accuracy, reliability or usefulness of |
| 18 |
* the software. |
| 19 |
* |
| 20 |
* Permission to use this software is contingent upon your acceptance |
| 21 |
* of the terms of this agreement |
| 22 |
* |
| 23 |
* . |
| 24 |
* |
| 25 |
*/ |
| 26 |
/**************************************************************************** |
| 27 |
* Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * |
| 28 |
****************************************************************************/ |
| 29 |
package gov.nist.javax.sip.header; |
| 30 |
import gov.nist.core.*; |
| 31 |
import javax.sip.header.AcceptLanguageHeader; |
| 32 |
import javax.sip.InvalidArgumentException; |
| 33 |
import java.util.Locale; |
| 34 |
|
| 35 |
/** |
| 36 |
* Accept Language body. |
| 37 |
* |
| 38 |
* @author M. Ranganathan |
| 39 |
* @version 1.2 $Revision: 1.8 $ $Date: 2009/10/18 13:46:32 $ |
| 40 |
* @since 1.1 |
| 41 |
* |
| 42 |
* |
| 43 |
* <pre> |
| 44 |
* HTTP RFC 2616 Section 14.4 |
| 45 |
* Accept-Language = "Accept-Language" ":" |
| 46 |
* 1#( language-range [ ";" "q" "=" qvalue ] ) |
| 47 |
* language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" ) |
| 48 |
* |
| 49 |
* </pre> |
| 50 |
* |
| 51 |
* @see AcceptLanguageList |
| 52 |
*/ |
| 53 |
public final class AcceptLanguage |
| 54 |
extends ParametersHeader |
| 55 |
implements AcceptLanguageHeader { |
| 56 |
|
| 57 |
/** |
| 58 |
* Comment for <code>serialVersionUID</code> |
| 59 |
*/ |
| 60 |
private static final long serialVersionUID = -4473982069737324919L; |
| 61 |
/** languageRange field |
| 62 |
*/ |
| 63 |
protected String languageRange; |
| 64 |
|
| 65 |
/** default constructor |
| 66 |
*/ |
| 67 |
public AcceptLanguage() { |
| 68 |
super(NAME); |
| 69 |
} |
| 70 |
|
| 71 |
/** Encode the value of this header to a string. |
| 72 |
*@return encoded header as a string. |
| 73 |
*/ |
| 74 |
protected String encodeBody() { |
| 75 |
StringBuffer encoding = new StringBuffer(); |
| 76 |
if (languageRange != null) { |
| 77 |
encoding.append(languageRange); |
| 78 |
} |
| 79 |
if (!parameters.isEmpty()) { |
| 80 |
encoding.append(SEMICOLON).append(parameters.encode()); |
| 81 |
} |
| 82 |
return encoding.toString(); |
| 83 |
} |
| 84 |
|
| 85 |
/** get the LanguageRange field |
| 86 |
* @return String |
| 87 |
*/ |
| 88 |
public String getLanguageRange() { |
| 89 |
return languageRange; |
| 90 |
} |
| 91 |
|
| 92 |
/** get the QValue field. Return -1 if the parameter has not been |
| 93 |
* set. |
| 94 |
* @return float |
| 95 |
*/ |
| 96 |
|
| 97 |
public float getQValue() { |
| 98 |
if (!hasParameter("q")) |
| 99 |
return -1; |
| 100 |
return ((Float) parameters.getValue("q")).floatValue(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Return true if the q value has been set. |
| 105 |
* @since 1.0 |
| 106 |
* @return boolean |
| 107 |
*/ |
| 108 |
public boolean hasQValue() { |
| 109 |
return hasParameter("q"); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Remove the q value. |
| 114 |
* @since 1.0 |
| 115 |
*/ |
| 116 |
public void removeQValue() { |
| 117 |
removeParameter("q"); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Set the languageRange. |
| 122 |
* |
| 123 |
* @param languageRange is the language range to set. |
| 124 |
* |
| 125 |
*/ |
| 126 |
public void setLanguageRange(String languageRange) { |
| 127 |
this.languageRange = languageRange.trim(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Sets q-value for media-range in AcceptLanguageHeader. Q-values allow the |
| 132 |
* |
| 133 |
* user to indicate the relative degree of preference for that media-range, |
| 134 |
* |
| 135 |
* using the qvalue scale from 0 to 1. If no q-value is present, the |
| 136 |
* |
| 137 |
* media-range should be treated as having a q-value of 1. |
| 138 |
* |
| 139 |
* |
| 140 |
* |
| 141 |
* @param q The new float value of the q-value, a value of -1 resets |
| 142 |
* the qValue. |
| 143 |
* |
| 144 |
* @throws InvalidArgumentException if the q parameter value is not |
| 145 |
* |
| 146 |
* <code>-1</code> or between <code>0 and 1</code>. |
| 147 |
* |
| 148 |
*/ |
| 149 |
public void setQValue(float q) throws InvalidArgumentException { |
| 150 |
if (q < 0.0 || q > 1.0) |
| 151 |
throw new InvalidArgumentException("qvalue out of range!"); |
| 152 |
if (q == -1) |
| 153 |
this.removeParameter("q"); |
| 154 |
else |
| 155 |
this.setParameter(new NameValue("q", Float.valueOf(q))); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Gets the language value of the AcceptLanguageHeader. |
| 160 |
* |
| 161 |
* |
| 162 |
* |
| 163 |
* @return the language Locale value of this AcceptLanguageHeader |
| 164 |
* |
| 165 |
*/ |
| 166 |
public Locale getAcceptLanguage() { |
| 167 |
if (this.languageRange == null) |
| 168 |
return null; |
| 169 |
else { |
| 170 |
int dash = languageRange.indexOf('-'); |
| 171 |
if (dash>=0) { |
| 172 |
return new Locale( languageRange.substring(0,dash), languageRange.substring(dash+1) ); |
| 173 |
} else return new Locale( this.languageRange ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Sets the language parameter of this AcceptLanguageHeader. |
| 179 |
* |
| 180 |
* |
| 181 |
* |
| 182 |
* @param language - the new Locale value of the language of |
| 183 |
* |
| 184 |
* AcceptLanguageHeader |
| 185 |
* |
| 186 |
* |
| 187 |
*/ |
| 188 |
public void setAcceptLanguage(Locale language) { |
| 189 |
// JvB: need to take sub-tag into account |
| 190 |
if ( "".equals(language.getCountry())) { |
| 191 |
this.languageRange = language.getLanguage(); |
| 192 |
} else { |
| 193 |
this.languageRange = language.getLanguage() + '-' + language.getCountry(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
} |