PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/simple/src/main/java/org/simpleframework/http/core/RequestEntity.java

https://gitlab.com/UnderSampled/aard2-build
Java | 398 lines | 128 code | 38 blank | 232 comment | 16 complexity | 90dfd7ac73a56bdc850a78dfb29fd602 MD5 | raw file
  1. /*
  2. * RequestEntity.java February 2001
  3. *
  4. * Copyright (C) 2001, 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.core;
  19. import static org.simpleframework.http.Protocol.CLOSE;
  20. import static org.simpleframework.http.Protocol.CONNECTION;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.InetAddress;
  24. import java.net.InetSocketAddress;
  25. import java.net.Socket;
  26. import java.nio.channels.Channels;
  27. import java.nio.channels.ReadableByteChannel;
  28. import java.nio.channels.SocketChannel;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. import org.simpleframework.http.ContentType;
  33. import org.simpleframework.http.Part;
  34. import org.simpleframework.http.Query;
  35. import org.simpleframework.http.Request;
  36. import org.simpleframework.http.message.Body;
  37. import org.simpleframework.http.message.Entity;
  38. import org.simpleframework.transport.Certificate;
  39. import org.simpleframework.transport.Channel;
  40. /**
  41. * This object is used to represent a HTTP request. This defines the
  42. * attributes that a HTTP request has such as a request line and the
  43. * headers that come with the message header.
  44. * <p>
  45. * The <code>Request</code> is used to provide an interface to the
  46. * HTTP <code>InputStream</code> and message header. The stream can
  47. * have certain characteristics, these characteristics are available
  48. * by this object. The <code>Request</code> provides methods that
  49. * allow the <code>InputStream</code>'s semantics to be known, for
  50. * example if the stream is keep-alive or if the stream has a length.
  51. * <p>
  52. * The <code>Request</code> origin is also retrievable from the
  53. * <code>Request</code> as is the attributes <code>Map</code> object
  54. * which defines specific connection attributes. And acts as a
  55. * simple model for the request transaction.
  56. * <p>
  57. * It is important to note that the <code>Request</code> controls
  58. * the processing of the HTTP pipeline. The next HTTP request is
  59. * not processed until the request has read all of the content body
  60. * within the <code>InputStream</code>. The stream must be fully
  61. * read or closed for the next request to be processed.
  62. *
  63. * @author Niall Gallagher
  64. */
  65. class RequestEntity extends RequestMessage implements Request {
  66. /**
  67. * This is the certificate associated with the connection.
  68. */
  69. private Certificate certificate;
  70. /**
  71. * This will create the form object using the query and body.
  72. */
  73. private QueryBuilder builder;
  74. /**
  75. * This channel represents the connected pipeline used.
  76. */
  77. private Channel channel;
  78. /**
  79. * The query contains all the parameters for the request.
  80. */
  81. private Query query;
  82. /**
  83. * The body contains the message content sent by the client.
  84. */
  85. private Body body;
  86. /**
  87. * This is used to contain the values for this request.
  88. */
  89. private Map map;
  90. /**
  91. * This is the time at which the request is ready to be used.
  92. */
  93. private long time;
  94. /**
  95. * Constructor for the <code>RequestEntity</code> object. This is
  96. * used to create a request that contains all the parts sent by
  97. * the client, including the headers and the request body. Each of
  98. * the request elements are accessible through this object in a
  99. * convenient manner, all parts and parameters, as well as cookies
  100. * can be accessed and used without much effort.
  101. *
  102. * @param entity this is the entity that was sent by the client
  103. * @param observer this is the observer used to monitor events
  104. */
  105. public RequestEntity(Entity entity, Observer observer) {
  106. this.certificate = new RequestCertificate(entity, observer);
  107. this.builder = new QueryBuilder(this, entity);
  108. this.channel = entity.getChannel();
  109. this.header = entity.getHeader();
  110. this.body = entity.getBody();
  111. this.time = entity.getTime();
  112. }
  113. /**
  114. * This is used to determine if the request has been transferred
  115. * over a secure connection. If the protocol is HTTPS and the
  116. * content is delivered over SSL then the request is considered
  117. * to be secure. Also the associated response will be secure.
  118. *
  119. * @return true if the request is transferred securely
  120. */
  121. public boolean isSecure() {
  122. return channel.isSecure();
  123. }
  124. /**
  125. * This is a convenience method that is used to determine whether
  126. * or not this message has the Connection header with the close
  127. * token. If the close token is present then this stream is not a
  128. * keep-alive connection. However if this has no Connection header
  129. * then the keep alive status is determined by the HTTP version,
  130. * that is HTTP/1.1 is keep alive by default, HTTP/1.0 has the
  131. * connection close by default.
  132. *
  133. * @return returns true if this is keep alive connection
  134. */
  135. public boolean isKeepAlive(){
  136. String value = getValue(CONNECTION);
  137. if(value == null) {
  138. int major = getMajor();
  139. int minor = getMinor();
  140. if(major > 1) {
  141. return true;
  142. }
  143. if(major == 1) {
  144. return minor > 0;
  145. }
  146. return false;
  147. }
  148. return !value.equalsIgnoreCase(CLOSE);
  149. }
  150. /**
  151. * This is the time in milliseconds when the request was first
  152. * read from the underlying socket. The time represented here
  153. * represents the time collection of this request began. This
  154. * does not necessarily represent the time the bytes arrived on
  155. * the receive buffers as some data may have been buffered.
  156. *
  157. * @return this represents the time the request arrived at
  158. */
  159. public long getRequestTime() {
  160. return time;
  161. }
  162. /**
  163. * This provides the underlying channel for the request. It
  164. * contains the TCP socket channel and various other low level
  165. * components. Typically this will only ever be needed when
  166. * there is a need to switch protocols.
  167. *
  168. * @return the underlying channel for this request
  169. */
  170. public Channel getChannel() {
  171. return channel;
  172. }
  173. /**
  174. * This is used to acquire the SSL certificate used when the
  175. * server is using a HTTPS connection. For plain text connections
  176. * or connections that use a security mechanism other than SSL
  177. * this will be null. This is only available when the connection
  178. * makes specific use of an SSL engine to secure the connection.
  179. *
  180. * @return this returns the associated SSL certificate if any
  181. */
  182. public Certificate getClientCertificate() {
  183. if(channel.isSecure()) {
  184. return certificate;
  185. }
  186. return null;
  187. }
  188. /**
  189. * This is used to acquire the remote client address. This can
  190. * be used to acquire both the port and the I.P address for the
  191. * client. It allows the connected clients to be logged and if
  192. * require it can be used to perform course grained security.
  193. *
  194. * @return this returns the client address for this request
  195. */
  196. public InetSocketAddress getClientAddress() {
  197. SocketChannel socket = channel.getSocket();
  198. Socket client = socket.socket();
  199. return getClientAddress(client);
  200. }
  201. /**
  202. * This is used to acquire the remote client address. This can
  203. * be used to acquire both the port and the I.P address for the
  204. * client. It allows the connected clients to be logged and if
  205. * require it can be used to perform course grained security.
  206. *
  207. * @param socket this is the socket to get the address for
  208. *
  209. * @return this returns the client address for this request
  210. */
  211. private InetSocketAddress getClientAddress(Socket socket) {
  212. InetAddress address = socket.getInetAddress();
  213. int port = socket.getPort();
  214. return new InetSocketAddress(address, port);
  215. }
  216. /**
  217. * This is used to get the content body. This will essentially get
  218. * the content from the body and present it as a single string.
  219. * The encoding of the string is determined from the content type
  220. * charset value. If the charset is not supported this will throw
  221. * an exception. Typically only text values should be extracted
  222. * using this method if there is a need to parse that content.
  223. *
  224. * @return the body content containing the message body
  225. */
  226. public String getContent() throws IOException {
  227. ContentType type = getContentType();
  228. if(type == null) {
  229. return body.getContent("ISO-8859-1");
  230. }
  231. return getContent(type);
  232. }
  233. /**
  234. * This is used to get the content body. This will essentially get
  235. * the content from the body and present it as a single string.
  236. * The encoding of the string is determined from the content type
  237. * charset value. If the charset is not supported this will throw
  238. * an exception. Typically only text values should be extracted
  239. * using this method if there is a need to parse that content.
  240. *
  241. * @param type this is the content type used with the request
  242. *
  243. * @return the input stream containing the message body
  244. */
  245. public String getContent(ContentType type) throws IOException {
  246. String charset = type.getCharset();
  247. if(charset == null) {
  248. charset = "ISO-8859-1";
  249. }
  250. return body.getContent(charset);
  251. }
  252. /**
  253. * This is used to read the content body. The specifics of the data
  254. * that is read from this <code>InputStream</code> can be determined
  255. * by the <code>getContentLength</code> method. If the data sent by
  256. * the client is chunked then it is decoded, see RFC 2616 section
  257. * 3.6. Also multipart data is available as <code>Part</code> objects
  258. * however the raw content of the multipart body is still available.
  259. *
  260. * @return the input stream containing the message body
  261. */
  262. public InputStream getInputStream() throws IOException {
  263. return body.getInputStream();
  264. }
  265. /**
  266. * This is used to read the content body. The specifics of the data
  267. * that is read from this <code>ReadableByteChannel</code> can be
  268. * determined by the <code>getContentLength</code> method. If the
  269. * data sent by the client is chunked then it is decoded, see RFC
  270. * 2616 section 3.6. This stream will never provide empty reads as
  271. * the content is internally buffered, so this can do a full read.
  272. *
  273. * @return this returns the byte channel used to read the content
  274. */
  275. public ReadableByteChannel getByteChannel() throws IOException {
  276. InputStream source = getInputStream();
  277. if(source != null) {
  278. return Channels.newChannel(source);
  279. }
  280. return null;
  281. }
  282. /**
  283. * This can be used to retrieve the response attributes. These can
  284. * be used to keep state with the response when it is passed to
  285. * other systems for processing. Attributes act as a convenient
  286. * model for storing objects associated with the response. This
  287. * also inherits attributes associated with the client connection.
  288. *
  289. * @return the attributes that have been added to this request
  290. */
  291. public Map getAttributes() {
  292. Map common = channel.getAttributes();
  293. if(map == null) {
  294. map = new HashMap(common);
  295. }
  296. return map;
  297. }
  298. /**
  299. * This is used as a shortcut for acquiring attributes for the
  300. * response. This avoids acquiring the attribute <code>Map</code>
  301. * in order to retrieve the attribute directly from that object.
  302. * The attributes contain data specific to the response.
  303. *
  304. * @param key this is the key of the attribute to acquire
  305. *
  306. * @return this returns the attribute for the specified name
  307. */
  308. public Object getAttribute(Object key) {
  309. return getAttributes().get(key);
  310. }
  311. /**
  312. * This method is used to acquire the query part from the HTTP
  313. * request URI target and a form post if it exists. Both the
  314. * query and the form post are merge together in a single query.
  315. *
  316. * @return the query associated with the HTTP target URI
  317. */
  318. public Query getQuery() {
  319. if(query == null) {
  320. query = builder.build();
  321. }
  322. return query;
  323. }
  324. /**
  325. * This is used to provide quick access to the parameters. This
  326. * avoids having to acquire the request <code>Form</code> object.
  327. * This basically acquires the parameters object and invokes
  328. * the <code>getParameters</code> method with the given name.
  329. *
  330. * @param name this is the name of the parameter value
  331. */
  332. public String getParameter(String name) {
  333. return getQuery().get(name);
  334. }
  335. /**
  336. * This method is used to acquire a <code>Part</code> from the
  337. * HTTP request using a known name for the part. This is typically
  338. * used when there is a file upload with a multipart POST request.
  339. * All parts that are not files can be acquired as string values
  340. * from the attachment object.
  341. *
  342. * @param name this is the name of the part object to acquire
  343. *
  344. * @return the named part or null if the part does not exist
  345. */
  346. public Part getPart(String name) {
  347. return body.getPart(name);
  348. }
  349. /**
  350. * This method is used to get all <code>Part</code> objects that
  351. * are associated with the request. Each attachment contains the
  352. * body and headers associated with it. If the request is not a
  353. * multipart POST request then this will return an empty list.
  354. *
  355. * @return the list of parts associated with this request
  356. */
  357. public List<Part> getParts() {
  358. return body.getParts();
  359. }
  360. }