/trunk/org.mwc.asset.comms/docs/restlet_src/org.restlet/org/restlet/engine/http/header/ContentTypeReader.java

https://bitbucket.org/haris_peco/debrief · Java · 252 lines · 174 code · 16 blank · 62 comment · 56 complexity · 806faf31b24f7a96959fe4395166d3b9 MD5 · raw file

  1. /**
  2. * Copyright 2005-2010 Noelios Technologies.
  3. *
  4. * The contents of this file are subject to the terms of one of the following
  5. * open source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL 1.0 (the
  6. * "Licenses"). You can select the license that you prefer but you may not use
  7. * this file except in compliance with one of these Licenses.
  8. *
  9. * You can obtain a copy of the LGPL 3.0 license at
  10. * http://www.opensource.org/licenses/lgpl-3.0.html
  11. *
  12. * You can obtain a copy of the LGPL 2.1 license at
  13. * http://www.opensource.org/licenses/lgpl-2.1.php
  14. *
  15. * You can obtain a copy of the CDDL 1.0 license at
  16. * http://www.opensource.org/licenses/cddl1.php
  17. *
  18. * You can obtain a copy of the EPL 1.0 license at
  19. * http://www.opensource.org/licenses/eclipse-1.0.php
  20. *
  21. * See the Licenses for the specific language governing permissions and
  22. * limitations under the Licenses.
  23. *
  24. * Alternatively, you can obtain a royalty free commercial license with less
  25. * limitations, transferable or non-transferable, directly at
  26. * http://www.noelios.com/products/restlet-engine
  27. *
  28. * Restlet is a registered trademark of Noelios Technologies.
  29. */
  30. package org.restlet.engine.http.header;
  31. import java.io.IOException;
  32. import org.restlet.data.CharacterSet;
  33. import org.restlet.data.Form;
  34. import org.restlet.data.MediaType;
  35. import org.restlet.data.Parameter;
  36. import org.restlet.util.Series;
  37. /**
  38. * Content type header reader.
  39. *
  40. * @author Jerome Louvel
  41. */
  42. public class ContentTypeReader extends HeaderReader<ContentType> {
  43. /**
  44. * Constructor.
  45. *
  46. * @param header
  47. * The header to read.
  48. */
  49. public ContentTypeReader(String header) {
  50. super(header);
  51. }
  52. /**
  53. * Creates a content type.
  54. *
  55. * @param mediaType
  56. * The media type name.
  57. * @param parameters
  58. * The parameters parsed.
  59. * @return The content type.
  60. */
  61. private ContentType createContentType(StringBuilder mediaType,
  62. Series<Parameter> parameters) {
  63. // Attempt to extract the character set
  64. CharacterSet characterSet = null;
  65. if (parameters != null) {
  66. String charSet = parameters.getFirstValue("charset");
  67. if (charSet != null) {
  68. parameters.removeAll("charset");
  69. characterSet = new CharacterSet(charSet);
  70. }
  71. return new ContentType(new MediaType(mediaType.toString(),
  72. parameters), characterSet);
  73. }
  74. return new ContentType(new MediaType(mediaType.toString()), null);
  75. }
  76. @Override
  77. public ContentType readValue() throws IOException {
  78. ContentType result = null;
  79. boolean readingMediaType = true;
  80. boolean readingParamName = false;
  81. boolean readingParamValue = false;
  82. StringBuilder mediaTypeBuffer = new StringBuilder();
  83. StringBuilder paramNameBuffer = null;
  84. StringBuilder paramValueBuffer = null;
  85. Series<Parameter> parameters = null;
  86. String nextValue = readRawValue();
  87. int nextIndex = 0;
  88. if (nextValue != null) {
  89. int nextChar = nextValue.charAt(nextIndex++);
  90. while (result == null) {
  91. if (readingMediaType) {
  92. if (nextChar == -1) {
  93. if (mediaTypeBuffer.length() > 0) {
  94. // End of metadata section
  95. // No parameters detected
  96. result = createContentType(mediaTypeBuffer, null);
  97. paramNameBuffer = new StringBuilder();
  98. } else {
  99. // Ignore empty metadata name
  100. }
  101. } else if (nextChar == ';') {
  102. if (mediaTypeBuffer.length() > 0) {
  103. // End of mediaType section
  104. // Parameters detected
  105. readingMediaType = false;
  106. readingParamName = true;
  107. paramNameBuffer = new StringBuilder();
  108. parameters = new Form();
  109. } else {
  110. throw new IOException(
  111. "Empty mediaType name detected.");
  112. }
  113. } else if (HeaderUtils.isSpace(nextChar)) {
  114. // Ignore spaces
  115. } else if (HeaderUtils.isText(nextChar)) {
  116. mediaTypeBuffer.append((char) nextChar);
  117. } else {
  118. throw new IOException(
  119. "The "
  120. + (char) nextChar
  121. + " character isn't allowed in a media type name.");
  122. }
  123. } else if (readingParamName) {
  124. if (nextChar == '=') {
  125. if (paramNameBuffer.length() > 0) {
  126. // End of parameter name section
  127. readingParamName = false;
  128. readingParamValue = true;
  129. paramValueBuffer = new StringBuilder();
  130. } else {
  131. throw new IOException(
  132. "Empty parameter name detected.");
  133. }
  134. } else if (nextChar == -1) {
  135. if (paramNameBuffer.length() > 0) {
  136. // End of parameters section
  137. parameters.add(Parameter.create(paramNameBuffer,
  138. null));
  139. result = createContentType(mediaTypeBuffer,
  140. parameters);
  141. } else {
  142. throw new IOException(
  143. "Empty parameter name detected.");
  144. }
  145. } else if (nextChar == ';') {
  146. // End of parameter
  147. parameters.add(Parameter.create(paramNameBuffer, null));
  148. paramNameBuffer = new StringBuilder();
  149. readingParamName = true;
  150. readingParamValue = false;
  151. } else if (HeaderUtils.isSpace(nextChar)
  152. && (paramNameBuffer.length() == 0)) {
  153. // Ignore white spaces
  154. } else if (HeaderUtils.isTokenChar(nextChar)) {
  155. paramNameBuffer.append((char) nextChar);
  156. } else {
  157. throw new IOException(
  158. "The \""
  159. + (char) nextChar
  160. + "\" character isn't allowed in a media type parameter name.");
  161. }
  162. } else if (readingParamValue) {
  163. if (nextChar == -1) {
  164. if (paramValueBuffer.length() > 0) {
  165. // End of parameters section
  166. parameters.add(Parameter.create(paramNameBuffer,
  167. paramValueBuffer));
  168. result = createContentType(mediaTypeBuffer,
  169. parameters);
  170. } else {
  171. throw new IOException(
  172. "Empty parameter value detected");
  173. }
  174. } else if (nextChar == ';') {
  175. // End of parameter
  176. parameters.add(Parameter.create(paramNameBuffer,
  177. paramValueBuffer));
  178. paramNameBuffer = new StringBuilder();
  179. readingParamName = true;
  180. readingParamValue = false;
  181. } else if ((nextChar == '"')
  182. && (paramValueBuffer.length() == 0)) {
  183. // Parse the quoted string
  184. boolean done = false;
  185. boolean quotedPair = false;
  186. while ((!done) && (nextChar != -1)) {
  187. nextChar = (nextIndex < nextValue.length()) ? nextValue
  188. .charAt(nextIndex++)
  189. : -1;
  190. if (quotedPair) {
  191. // End of quoted pair (escape sequence)
  192. if (HeaderUtils.isText(nextChar)) {
  193. paramValueBuffer.append((char) nextChar);
  194. quotedPair = false;
  195. } else {
  196. throw new IOException(
  197. "Invalid character \""
  198. + (char) nextChar
  199. + "\" detected in quoted string. Please check your value");
  200. }
  201. } else if (HeaderUtils.isDoubleQuote(nextChar)) {
  202. // End of quoted string
  203. done = true;
  204. } else if (nextChar == '\\') {
  205. // Begin of quoted pair (escape sequence)
  206. quotedPair = true;
  207. } else if (HeaderUtils.isText(nextChar)) {
  208. paramValueBuffer.append((char) nextChar);
  209. } else {
  210. throw new IOException(
  211. "Invalid character \""
  212. + (char) nextChar
  213. + "\" detected in quoted string. Please check your value");
  214. }
  215. }
  216. } else if (HeaderUtils.isTokenChar(nextChar)) {
  217. paramValueBuffer.append((char) nextChar);
  218. } else {
  219. throw new IOException(
  220. "The \""
  221. + (char) nextChar
  222. + "\" character isn't allowed in a media type parameter value.");
  223. }
  224. }
  225. nextChar = (nextIndex < nextValue.length()) ? nextValue
  226. .charAt(nextIndex++) : -1;
  227. }
  228. }
  229. return result;
  230. }
  231. }