/simple/src/main/java/org/simpleframework/http/message/MessageHeader.java

https://gitlab.com/UnderSampled/aard2-build · Java · 477 lines · 133 code · 45 blank · 299 comment · 18 complexity · 30c74c5a50cf7bd91956da185ea37254 MD5 · raw file

  1. /*
  2. * Message.java February 2007
  3. *
  4. * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  15. * implied. See the License for the specific language governing
  16. * permissions and limitations under the License.
  17. */
  18. package org.simpleframework.http.message;
  19. import java.util.LinkedList;
  20. import java.util.List;
  21. import org.simpleframework.http.Cookie;
  22. import org.simpleframework.http.parse.DateParser;
  23. import org.simpleframework.http.parse.ValueParser;
  24. import org.simpleframework.util.KeyMap;
  25. /**
  26. * The <code>Message</code> object is used to store an retrieve the
  27. * headers for both a request and response. Headers are stored and
  28. * retrieved in a case insensitive manner according to RFC 2616.
  29. * The message also allows multiple header values to be added to a
  30. * single header name, headers such as Cookie and Set-Cookie can be
  31. * added multiple times with different values.
  32. *
  33. * @author Niall Gallagher
  34. */
  35. public class MessageHeader implements Message {
  36. /**
  37. * This is used to store the cookies added to the HTTP header.
  38. */
  39. private final KeyMap<Cookie> cookies;
  40. /**
  41. * This is used to store multiple header values for a name.
  42. */
  43. private final KeyMap<Series> values;
  44. /**
  45. * This is used to store the individual names for the header.
  46. */
  47. private final KeyMap<String> names;
  48. /**
  49. * This is used to parse all date headers added to the message.
  50. */
  51. private final DateParser parser;
  52. /**
  53. * Constructor for the <code>Message</code> object. This is used
  54. * to create a case insensitive means for storing HTTP header
  55. * names and values. Dates can also be added to message as a
  56. * long value and is converted to RFC 1123 compliant date string.
  57. */
  58. public MessageHeader() {
  59. this.cookies = new KeyMap<Cookie>();
  60. this.values = new KeyMap<Series>();
  61. this.names = new KeyMap<String>();
  62. this.parser = new DateParser();
  63. }
  64. /**
  65. * This is used to acquire the names of the of the headers that
  66. * have been set in the response. This can be used to acquire all
  67. * header values by name that have been set within the response.
  68. * If no headers have been set this will return an empty list.
  69. *
  70. * @return a list of strings representing the set header names
  71. */
  72. public List<String> getNames() {
  73. return names.getValues();
  74. }
  75. /**
  76. * This can be used to set a HTTP message header to this object.
  77. * The name and value of the HTTP message header will be used to
  78. * create a HTTP message header object which can be retrieved using
  79. * the <code>getValue</code> in combination with the get methods.
  80. * This will perform a <code>remove</code> using the issued header
  81. * name before the header value is set.
  82. *
  83. * @param name the name of the HTTP message header to be added
  84. * @param value the value the HTTP message header will have
  85. */
  86. public void setValue(String name, String value) {
  87. List<String> list = getAll(name);
  88. if(value == null) {
  89. String token = name.toLowerCase();
  90. values.remove(token);
  91. names.remove(token);
  92. } else {
  93. list.clear();
  94. list.add(value);
  95. }
  96. }
  97. /**
  98. * This can be used to set a HTTP message header to this object.
  99. * The name and value of the HTTP message header will be used to
  100. * create a HTTP message header object which can be retrieved using
  101. * the <code>getValue</code> in combination with the get methods.
  102. * This will perform a <code>remove</code> using the issued header
  103. * name before the header value is set.
  104. *
  105. * @param name the name of the HTTP message header to be added
  106. * @param value the value the HTTP message header will have
  107. */
  108. public void setInteger(String name, int value) {
  109. setValue(name, String.valueOf(value));
  110. }
  111. /**
  112. * This can be used to set a HTTP message header to this object.
  113. * The name and value of the HTTP message header will be used to
  114. * create a HTTP message header object which can be retrieved using
  115. * the <code>getValue</code> in combination with the get methods.
  116. * This will perform a <code>remove</code> using the issued header
  117. * name before the header value is set.
  118. *
  119. * @param name the name of the HTTP message header to be added
  120. * @param value the value the HTTP message header will have
  121. */
  122. public void setLong(String name, long value) {
  123. setValue(name, String.valueOf(value));
  124. }
  125. /**
  126. * This is used as a convenience method for adding a header that
  127. * needs to be parsed into a HTTP date string. This will convert
  128. * the date given into a date string defined in RFC 2616 sec 3.3.1.
  129. * This will perform a <code>remove</code> using the issued header
  130. * name before the header value is set.
  131. *
  132. * @param name the name of the HTTP message header to be added
  133. * @param date the value constructed as an RFC 1123 date string
  134. */
  135. public void setDate(String name, long date) {
  136. setValue(name, parser.convert(date));
  137. }
  138. /**
  139. * This can be used to add a HTTP message header to this object.
  140. * The name and value of the HTTP message header will be used to
  141. * create a HTTP message header object which can be retrieved using
  142. * the <code>getValue</code> in combination with the get methods.
  143. *
  144. * @param name the name of the HTTP message header to be added
  145. * @param value the value the HTTP message header will have
  146. */
  147. public void addValue(String name, String value) {
  148. List<String> list = getAll(name);
  149. if(value != null) {
  150. list.add(value);
  151. }
  152. }
  153. /**
  154. * This can be used to add a HTTP message header to this object.
  155. * The name and value of the HTTP message header will be used to
  156. * create a HTTP message header object which can be retrieved using
  157. * the <code>getInteger</code> in combination with the get methods.
  158. *
  159. * @param name the name of the HTTP message header to be added
  160. * @param value the value the HTTP message header will have
  161. */
  162. public void addInteger(String name, int value) {
  163. addValue(name, String.valueOf(value));
  164. }
  165. /**
  166. * This is used as a convenience method for adding a header that
  167. * needs to be parsed into a HTTPdate string. This will convert
  168. * the date given into a date string defined in RFC 2616 sec 3.3.1.
  169. *
  170. * @param name the name of the HTTP message header to be added
  171. * @param date the value constructed as an RFC 1123 date string
  172. */
  173. public void addDate(String name, long date) {
  174. addValue(name, parser.convert(date));
  175. }
  176. /**
  177. * This can be used to get the value of the first message header
  178. * that has the specified name. This will return the full string
  179. * representing the named header value. If the named header does
  180. * not exist then this will return a null value.
  181. *
  182. * @param name the HTTP message header to get the value from
  183. *
  184. * @return this returns the value that the HTTP message header
  185. */
  186. public String getValue(String name) {
  187. return getValue(name, 0);
  188. }
  189. /**
  190. * This can be used to get the value of the first message header
  191. * that has the specified name. This will return the full string
  192. * representing the named header value. If the named header does
  193. * not exist then this will return a null value.
  194. *
  195. * @param name the HTTP message header to get the value from
  196. * @param index this is the index to get the value from
  197. *
  198. * @return this returns the value that the HTTP message header
  199. */
  200. public String getValue(String name, int index) {
  201. List<String> list = getAll(name);
  202. if(list.size() > index) {
  203. return list.get(index);
  204. }
  205. return null;
  206. }
  207. /**
  208. * This can be used to get the value of the first message header
  209. * that has the specified name. This will return the integer
  210. * representing the named header value. If the named header does
  211. * not exist then this will return a value of minus one, -1.
  212. *
  213. * @param name the HTTP message header to get the value from
  214. *
  215. * @return this returns the value that the HTTP message header
  216. */
  217. public int getInteger(String name) {
  218. String value = getValue(name);
  219. if(value == null) {
  220. return -1;
  221. }
  222. return Integer.parseInt(value);
  223. }
  224. /**
  225. * This can be used to get the value of the first message header
  226. * that has the specified name. This will return the long
  227. * representing the named header value. If the named header does
  228. * not exist then this will return a value of minus one, -1.
  229. *
  230. * @param name the HTTP message header to get the value from
  231. *
  232. * @return this returns the value that the HTTP message header
  233. */
  234. public long getLong(String name) {
  235. String value = getValue(name);
  236. if(value == null) {
  237. return -1L;
  238. }
  239. return Long.parseLong(value);
  240. }
  241. /**
  242. * This can be used to get the value of the first message header
  243. * that has the specified name. This will return the long value
  244. * representing the named header value. If the named header does
  245. * not exist then this will return a value of minus one, -1.
  246. *
  247. * @param name the HTTP message header to get the value from
  248. *
  249. * @return this returns the value that the HTTP message header
  250. */
  251. public long getDate(String name) {
  252. String value = getValue(name);
  253. if(value == null) {
  254. return -1;
  255. }
  256. return parser.convert(value);
  257. }
  258. /**
  259. * This returns the <code>Cookie</code> object stored under the
  260. * specified name. This is used to retrieve cookies that have been
  261. * set with the <code>setCookie</code> methods. If the cookie does
  262. * not exist under the specified name this will return null.
  263. *
  264. * @param name this is the name of the cookie to be retrieved
  265. *
  266. * @return returns the <code>Cookie</code> by the given name
  267. */
  268. public Cookie getCookie(String name) {
  269. return cookies.get(name);
  270. }
  271. /**
  272. * This returns all <code>Cookie</code> objects stored under the
  273. * specified name. This is used to retrieve cookies that have been
  274. * set with the <code>setCookie</code> methods. If there are no
  275. * cookies then this will return an empty list.
  276. *
  277. * @return returns all the <code>Cookie</code> in the response
  278. */
  279. public List<Cookie> getCookies() {
  280. return cookies.getValues();
  281. }
  282. /**
  283. * The <code>setCookie</code> method is used to set a cookie value
  284. * with the cookie name. This will add a cookie to the response
  285. * stored under the name of the cookie, when this is committed it
  286. * will be added as a Set-Cookie header to the resulting response.
  287. * This is a convenience method that avoids cookie creation.
  288. *
  289. * @param name this is the cookie to be added to the response
  290. * @param value this is the cookie value that is to be used
  291. *
  292. * @return returns the cookie that has been set in the response
  293. */
  294. public Cookie setCookie(String name, String value) {
  295. return setCookie(new Cookie(name, value, true));
  296. }
  297. /**
  298. * The <code>setCookie</code> method is used to set a cookie value
  299. * with the cookie name. This will add a cookie to the response
  300. * stored under the name of the cookie, when this is committed it
  301. * will be added as a Set-Cookie header to the resulting response.
  302. *
  303. * @param cookie this is the cookie to be added to the response
  304. *
  305. * @return returns the cookie that has been set in the response
  306. */
  307. public Cookie setCookie(Cookie cookie) {
  308. String name = cookie.getName();
  309. if(name != null) {
  310. cookies.put(name, cookie);
  311. }
  312. return cookie;
  313. }
  314. /**
  315. * This can be used to get the values of HTTP message headers
  316. * that have the specified name. This is a convenience method that
  317. * will present that values as tokens extracted from the header.
  318. * This has obvious performance benefits as it avoids having to
  319. * deal with <code>substring</code> and <code>trim</code> calls.
  320. * <p>
  321. * The tokens returned by this method are ordered according to
  322. * there HTTP quality values, or "q" values, see RFC 2616 section
  323. * 3.9. This also strips out the quality parameter from tokens
  324. * returned. So "image/html; q=0.9" results in "image/html". If
  325. * there are no "q" values present then order is by appearance.
  326. * <p>
  327. * The result from this is either the trimmed header value, that
  328. * is, the header value with no leading or trailing whitespace
  329. * or an array of trimmed tokens ordered with the most preferred
  330. * in the lower indexes, so index 0 is has highest preference.
  331. *
  332. * @param name the name of the headers that are to be retrieved
  333. *
  334. * @return ordered list of tokens extracted from the header(s)
  335. */
  336. public List<String> getValues(String name) {
  337. return getValues(getAll(name));
  338. }
  339. /**
  340. * This can be used to get the values of HTTP message headers
  341. * that have the specified name. This is a convenience method that
  342. * will present that values as tokens extracted from the header.
  343. * This has obvious performance benefits as it avoids having to
  344. * deal with <code>substring</code> and <code>trim</code> calls.
  345. * <p>
  346. * The tokens returned by this method are ordered according to
  347. * there HTTP quality values, or "q" values, see RFC 2616 section
  348. * 3.9. This also strips out the quality parameter from tokens
  349. * returned. So "image/html; q=0.9" results in "image/html". If
  350. * there are no "q" values present then order is by appearance.
  351. * <p>
  352. * The result from this is either the trimmed header value, that
  353. * is, the header value with no leading or trailing whitespace
  354. * or an array of trimmed tokens ordered with the most preferred
  355. * in the lower indexes, so index 0 is has highest preference.
  356. *
  357. * @param list this is the list of individual header values
  358. *
  359. * @return ordered list of tokens extracted from the header(s)
  360. */
  361. public List<String> getValues(List<String> list) {
  362. return new ValueParser(list).list();
  363. }
  364. /**
  365. * This is used to acquire all the individual header values from
  366. * the message. The header values provided by this are unparsed
  367. * and represent the actual string values that have been added to
  368. * the message keyed by a given header name.
  369. *
  370. * @param name the name of the header to get the values for
  371. *
  372. * @return this returns a list of the values for the header name
  373. */
  374. public List<String> getAll(String name) {
  375. String token = name.toLowerCase();
  376. Series series = values.get(token);
  377. if(series == null) {
  378. return getAll(name, token);
  379. }
  380. return series.getValues();
  381. }
  382. /**
  383. * This is used to acquire all the individual header values from
  384. * the message. The header values provided by this are unparsed
  385. * and represent the actual string values that have been added to
  386. * the message keyed by a given header name.
  387. *
  388. * @param name the name of the header to get the values for
  389. * @param token this provides a lower case version of the header
  390. *
  391. * @return this returns a list of the values for the header name
  392. */
  393. private List<String> getAll(String name, String token) {
  394. Series series = new Series();
  395. String value = names.get(token);
  396. if(value == null) {
  397. names.put(token, name);
  398. }
  399. values.put(token, series);
  400. return series.getValues();
  401. }
  402. /**
  403. * The <code>Series</code> object is used to represent a list of
  404. * HTTP header value for a given name. It allows multiple values
  405. * to exist for a given header, such as the Cookie header. Most
  406. * entries will contain a single value.
  407. */
  408. private class Series {
  409. /**
  410. * Contains the header values that belong to the entry name.
  411. */
  412. private List<String> value;
  413. /**
  414. * Constructor for the <code>Entry</code> object. The entry is
  415. * created using the name of the HTTP header. Values can be
  416. * added to the entry list in order to build up the header.
  417. */
  418. public Series() {
  419. this.value = new LinkedList<String>();
  420. }
  421. /**
  422. * This returns the list of header values associated with the
  423. * header name. Each value is added as an individual header
  424. * prefixed by the header name and a semicolon character.
  425. *
  426. * @return this returns the list of values for the header
  427. */
  428. public List<String> getValues() {
  429. return value;
  430. }
  431. }
  432. }