Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/net/httpserver/HttpExchange.java
38922 views
1
/*
2
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.net.httpserver;
27
28
import java.io.*;
29
import java.nio.*;
30
import java.nio.channels.*;
31
import java.net.*;
32
import javax.net.ssl.*;
33
import java.util.*;
34
import sun.net.www.MessageHeader;
35
36
/**
37
* This class encapsulates a HTTP request received and a
38
* response to be generated in one exchange. It provides methods
39
* for examining the request from the client, and for building and
40
* sending the response.
41
* <p>
42
* The typical life-cycle of a HttpExchange is shown in the sequence
43
* below.
44
* <ol><li>{@link #getRequestMethod()} to determine the command
45
* <li>{@link #getRequestHeaders()} to examine the request headers (if needed)
46
* <li>{@link #getRequestBody()} returns a {@link java.io.InputStream} for reading the request body.
47
* After reading the request body, the stream is close.
48
* <li>{@link #getResponseHeaders()} to set any response headers, except content-length
49
* <li>{@link #sendResponseHeaders(int,long)} to send the response headers. Must be called before
50
* next step.
51
* <li>{@link #getResponseBody()} to get a {@link java.io.OutputStream} to send the response body.
52
* When the response body has been written, the stream must be closed to terminate the exchange.
53
* </ol>
54
* <b>Terminating exchanges</b>
55
* <br>
56
* Exchanges are terminated when both the request InputStream and response OutputStream are closed.
57
* Closing the OutputStream, implicitly closes the InputStream (if it is not already closed).
58
* However, it is recommended
59
* to consume all the data from the InputStream before closing it.
60
* The convenience method {@link #close()} does all of these tasks.
61
* Closing an exchange without consuming all of the request body is not an error
62
* but may make the underlying TCP connection unusable for following exchanges.
63
* The effect of failing to terminate an exchange is undefined, but will typically
64
* result in resources failing to be freed/reused.
65
* @since 1.6
66
*/
67
68
@jdk.Exported
69
public abstract class HttpExchange {
70
71
protected HttpExchange () {
72
}
73
74
/**
75
* Returns an immutable Map containing the HTTP headers that were
76
* included with this request. The keys in this Map will be the header
77
* names, while the values will be a List of Strings containing each value
78
* that was included (either for a header that was listed several times,
79
* or one that accepts a comma-delimited list of values on a single line).
80
* In either of these cases, the values for the header name will be
81
* presented in the order that they were included in the request.
82
* <p>
83
* The keys in Map are case-insensitive.
84
* @return a read-only Map which can be used to access request headers
85
*/
86
public abstract Headers getRequestHeaders () ;
87
88
/**
89
* Returns a mutable Map into which the HTTP response headers can be stored
90
* and which will be transmitted as part of this response. The keys in the
91
* Map will be the header names, while the values must be a List of Strings
92
* containing each value that should be included multiple times
93
* (in the order that they should be included).
94
* <p>
95
* The keys in Map are case-insensitive.
96
* @return a writable Map which can be used to set response headers.
97
*/
98
public abstract Headers getResponseHeaders () ;
99
100
/**
101
* Get the request URI
102
*
103
* @return the request URI
104
*/
105
public abstract URI getRequestURI () ;
106
107
/**
108
* Get the request method
109
* @return the request method
110
*/
111
public abstract String getRequestMethod ();
112
113
/**
114
* Get the HttpContext for this exchange
115
* @return the HttpContext
116
*/
117
public abstract HttpContext getHttpContext ();
118
119
/**
120
* Ends this exchange by doing the following in sequence:<p><ol>
121
* <li>close the request InputStream, if not already closed<p></li>
122
* <li>close the response OutputStream, if not already closed. </li>
123
* </ol>
124
*/
125
public abstract void close () ;
126
127
/**
128
* returns a stream from which the request body can be read.
129
* Multiple calls to this method will return the same stream.
130
* It is recommended that applications should consume (read) all of the
131
* data from this stream before closing it. If a stream is closed
132
* before all data has been read, then the close() call will
133
* read and discard remaining data (up to an implementation specific
134
* number of bytes).
135
* @return the stream from which the request body can be read.
136
*/
137
public abstract InputStream getRequestBody () ;
138
139
/**
140
* returns a stream to which the response body must be
141
* written. {@link #sendResponseHeaders(int,long)}) must be called prior to calling
142
* this method. Multiple calls to this method (for the same exchange)
143
* will return the same stream. In order to correctly terminate
144
* each exchange, the output stream must be closed, even if no
145
* response body is being sent.
146
* <p>
147
* Closing this stream implicitly
148
* closes the InputStream returned from {@link #getRequestBody()}
149
* (if it is not already closed).
150
* <P>
151
* If the call to sendResponseHeaders() specified a fixed response
152
* body length, then the exact number of bytes specified in that
153
* call must be written to this stream. If too many bytes are written,
154
* then write() will throw an IOException. If too few bytes are written
155
* then the stream close() will throw an IOException. In both cases,
156
* the exchange is aborted and the underlying TCP connection closed.
157
* @return the stream to which the response body is written
158
*/
159
public abstract OutputStream getResponseBody () ;
160
161
162
/**
163
* Starts sending the response back to the client using the current set of response headers
164
* and the numeric response code as specified in this method. The response body length is also specified
165
* as follows. If the response length parameter is greater than zero, this specifies an exact
166
* number of bytes to send and the application must send that exact amount of data.
167
* If the response length parameter is <code>zero</code>, then chunked transfer encoding is
168
* used and an arbitrary amount of data may be sent. The application terminates the
169
* response body by closing the OutputStream. If response length has the value <code>-1</code>
170
* then no response body is being sent.
171
* <p>
172
* If the content-length response header has not already been set then
173
* this is set to the appropriate value depending on the response length parameter.
174
* <p>
175
* This method must be called prior to calling {@link #getResponseBody()}.
176
* @param rCode the response code to send
177
* @param responseLength if > 0, specifies a fixed response body length
178
* and that exact number of bytes must be written
179
* to the stream acquired from getResponseBody(), or else
180
* if equal to 0, then chunked encoding is used,
181
* and an arbitrary number of bytes may be written.
182
* if <= -1, then no response body length is specified and
183
* no response body may be written.
184
* @see HttpExchange#getResponseBody()
185
*/
186
public abstract void sendResponseHeaders (int rCode, long responseLength) throws IOException ;
187
188
/**
189
* Returns the address of the remote entity invoking this request
190
* @return the InetSocketAddress of the caller
191
*/
192
public abstract InetSocketAddress getRemoteAddress ();
193
194
/**
195
* Returns the response code, if it has already been set
196
* @return the response code, if available. <code>-1</code> if not available yet.
197
*/
198
public abstract int getResponseCode ();
199
200
/**
201
* Returns the local address on which the request was received
202
* @return the InetSocketAddress of the local interface
203
*/
204
public abstract InetSocketAddress getLocalAddress ();
205
206
/**
207
* Returns the protocol string from the request in the form
208
* <i>protocol/majorVersion.minorVersion</i>. For example,
209
* "HTTP/1.1"
210
* @return the protocol string from the request
211
*/
212
public abstract String getProtocol ();
213
214
/**
215
* Filter modules may store arbitrary objects with HttpExchange
216
* instances as an out-of-band communication mechanism. Other Filters
217
* or the exchange handler may then access these objects.
218
* <p>
219
* Each Filter class will document the attributes which they make
220
* available.
221
* @param name the name of the attribute to retrieve
222
* @return the attribute object, or null if it does not exist
223
* @throws NullPointerException if name is <code>null</code>
224
*/
225
public abstract Object getAttribute (String name) ;
226
227
/**
228
* Filter modules may store arbitrary objects with HttpExchange
229
* instances as an out-of-band communication mechanism. Other Filters
230
* or the exchange handler may then access these objects.
231
* <p>
232
* Each Filter class will document the attributes which they make
233
* available.
234
* @param name the name to associate with the attribute value
235
* @param value the object to store as the attribute value. <code>null</code>
236
* value is permitted.
237
* @throws NullPointerException if name is <code>null</code>
238
*/
239
public abstract void setAttribute (String name, Object value) ;
240
241
/**
242
* Used by Filters to wrap either (or both) of this exchange's InputStream
243
* and OutputStream, with the given filtered streams so
244
* that subsequent calls to {@link #getRequestBody()} will
245
* return the given {@link java.io.InputStream}, and calls to
246
* {@link #getResponseBody()} will return the given
247
* {@link java.io.OutputStream}. The streams provided to this
248
* call must wrap the original streams, and may be (but are not
249
* required to be) sub-classes of {@link java.io.FilterInputStream}
250
* and {@link java.io.FilterOutputStream}.
251
* @param i the filtered input stream to set as this object's inputstream,
252
* or <code>null</code> if no change.
253
* @param o the filtered output stream to set as this object's outputstream,
254
* or <code>null</code> if no change.
255
*/
256
public abstract void setStreams (InputStream i, OutputStream o);
257
258
259
/**
260
* If an authenticator is set on the HttpContext that owns this exchange,
261
* then this method will return the {@link HttpPrincipal} that represents
262
* the authenticated user for this HttpExchange.
263
* @return the HttpPrincipal, or <code>null</code> if no authenticator is set.
264
*/
265
public abstract HttpPrincipal getPrincipal ();
266
}
267
268