📄 httpconnectionhelper.java
字号:
* current uploadPolicy, and upload history), send the request, and create
* the InputStream to read the server response.
*
* @throws JUploadIOException
*/
public void sendRequest() throws JUploadIOException {
// This method expects that the connection is writing data to the
// server.
if (this.connectionStatus != STATUS_BEFORE_SERVER_CONNECTION) {
throw new JUploadIOException(
"Bad status of the connectionHelper in initRequest: "
+ getStatusLabel());
}
try {
// We've finished with the current encoder.
if (!this.byteArrayEncoder.isClosed()) {
this.byteArrayEncoder.close();
}
// Let's clear any field that could have been read in a previous
// step:
this.httpInputStreamReader = null;
// Only connect, if sock is null!!
// ... or if we don't persist HTTP connections (patch for IIS, based
// on Marc Reidy's patch)
if (this.socket == null
|| !this.uploadPolicy.getAllowHttpPersistent()) {
this.socket = new HttpConnect(this.uploadPolicy).Connect(
this.url, this.proxy);
this.outputStream = new DataOutputStream(
new BufferedOutputStream(this.socket.getOutputStream()));
this.inputStream = new PushbackInputStream(this.socket
.getInputStream(), 1);
}
// Send http request to server
this.outputStream
.write(this.byteArrayEncoder.getEncodedByteArray());
// The request has been sent. The current ByteArrayEncoder is now
// useless. A new one is to be created for the next request.
this.connectionStatus = STATUS_WRITING_REQUEST;
} catch (IOException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (KeyManagementException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (UnrecoverableKeyException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (NoSuchAlgorithmException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (KeyStoreException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (CertificateException e) {
throw new JUploadIOException("Unable to open socket", e);
} catch (IllegalArgumentException e) {
throw new JUploadIOException("Unable to open socket", e);
}
}
/**
* Releases all reserved resources.
*
* @throws JUploadIOException
*/
public void dispose() throws JUploadIOException {
try {
// Throws java.io.IOException
this.outputStream.close();
} catch (NullPointerException e) {
// httpDataOut is already null ...
} catch (IOException e) {
throw new JUploadIOException(e);
} finally {
this.outputStream = null;
}
try {
// Throws java.io.IOException
this.inputStream.close();
} catch (NullPointerException e) {
// httpDataIn is already null ...
} catch (IOException e) {
throw new JUploadIOException(e);
} finally {
this.inputStream = null;
}
try {
// Throws java.io.IOException
this.socket.close();
} catch (NullPointerException e) {
// sock is already null ...
} catch (IOException e) {
throw new JUploadIOException(e);
} finally {
this.socket = null;
}
}
/**
* Return the current socket. If the byteArrayEncoder is not closed: close
* it, and send the request to the server.
*
* @return public Socket getSocket() { }
*/
/**
* get the output stream, where HTTP data can be written.
*
* @return The current output stream to the server, where things can be
* written, event after the socket is open, if the byteArrayEncoder
* did not contain the full request.
*/
public OutputStream getOutputStream() {
return this;
}
/**
* get the input stream, where HTTP server response can be read.
*
* @return The current input stream of the socket.
*/
public PushbackInputStream getInputStream() {
return this.inputStream;
}
/**
* Get the HTTP method (HEAD, POST, GET...)
*
* @return The HTTP method
*/
public String getMethod() {
return this.method;
}
/**
* Get the last response body.
*
* @return The full response body, that is: the HTTP body of the server
* response.
*/
public String getResponseBody() {
return this.httpInputStreamReader.getResponseBody();
}
/**
* Get the headers of the HTTP response.
*
* @return The HTTP headers.
*/
public String getResponseHeaders() {
return this.httpInputStreamReader.getResponseHeaders();
}
/**
* Get the last response message.
*
* @return the response message, like "200 OK"
*/
public String getResponseMsg() {
return this.httpInputStreamReader.getResponseMsg();
}
/**
* Get the label describing the current state of this connection helper.
*
* @return A text describing briefly the current connection status.
*/
public String getStatusLabel() {
switch (this.connectionStatus) {
case STATUS_NOT_INITIALIZED:
return "Not initialized";
case STATUS_BEFORE_SERVER_CONNECTION:
return "Before server connection";
case STATUS_WRITING_REQUEST:
return "Writing request to the network";
case STATUS_READING_RESPONSE:
return "Reading server response";
case STATUS_CONNECTION_CLOSED:
return "Connection closed";
}
return "Unknown status in HTTPConnectionHelper.getStatusLabel()";
}
/**
* Get the current socket.
*
* @return return the current Socket, opened toward the server.
*/
Socket getSocket() {
return this.socket;
}
/**
* Return true is the upload is stopped.
*
* @return Current value of the stop attribute.
*/
public boolean gotStopped() {
return this.stop;
}
/**
* Append bytes to the current query. The bytes will be written to the
* current ByteArrayEncoder if the the connection to the server is not open,
* or directly to the server if the connection is opened.
*
* @param b The byte to send to the server.
* @return Returns the current ConnectionHelper, to allow coding like
* StringBuffers: a.append(b).append(c);
* @throws JUploadIOException
*/
public HTTPConnectionHelper append(int b) throws JUploadIOException {
if (this.connectionStatus == STATUS_BEFORE_SERVER_CONNECTION) {
this.byteArrayEncoder.append(b);
} else if (this.connectionStatus == STATUS_WRITING_REQUEST) {
try {
this.outputStream.write(b);
} catch (IOException e) {
throw new JUploadIOException(e.getClass().getName()
+ " while writing to httpDataOut", e);
}
} else {
throw new JUploadIOException(
"Wrong status in HTTPConnectionHelper.write() ["
+ getStatusLabel() + "]");
}
return this;
}
/**
* Append bytes to the current query. The bytes will be written to the
* current ByteArrayEncoder if the the connection to the server is not open,
* or directly to the server if the connection is opened.
*
* @param bytes The bytes to send to the server.
* @return Returns the current ConnectionHelper, to allow coding like
* StringBuffers: a.append(b).append(c);
* @throws JUploadIOException
*/
public HTTPConnectionHelper append(byte[] bytes) throws JUploadIOException {
if (this.connectionStatus == STATUS_BEFORE_SERVER_CONNECTION) {
this.byteArrayEncoder.append(bytes);
} else if (this.connectionStatus == STATUS_WRITING_REQUEST) {
try {
this.outputStream.write(bytes);
} catch (IOException e) {
throw new JUploadIOException(e.getClass().getName()
+ " while writing to httpDataOut", e);
}
} else {
throw new JUploadIOException(
"Wrong status in HTTPConnectionHelper.write() ["
+ getStatusLabel() + "]");
}
if (this.uploadPolicy.getDebugLevel() > 100) {
this.uploadPolicy
.displayDebug(
"[HTTPConnectionHelper append] ("
+ bytes.length
+ " bytes appended to "
+ (this.connectionStatus == STATUS_BEFORE_SERVER_CONNECTION ? " current ByteArrayEncoder"
: " socket") + ")", 70);
}
return this;
}
/**
* Append bytes to the current query. The bytes will be written to the
* current ByteArrayEncoder if the the connection to the server is not open,
* or directly to the server if the connection is opened.
*
* @param bytes The bytes to send to the server.
* @param off The first byte to send
* @param len Number of bytes to send.
* @return Returns the current ConnectionHelper, to allow coding like
* StringBuffers: a.append(b).append(c);
* @throws JUploadIOException
*/
public HTTPConnectionHelper append(byte[] bytes, int off, int len)
throws JUploadIOException {
if (this.connectionStatus == STATUS_BEFORE_SERVER_CONNECTION) {
this.byteArrayEncoder.append(bytes);
} else if (this.connectionStatus == STATUS_WRITING_REQUEST) {
try {
this.outputStream.write(bytes, off, len);
} catch (IOException e) {
throw new JUploadIOException(e.getClass().getName()
+ " while writing to httpDataOut", e);
}
} else {
throw new JUploadIOException(
"Wrong status in HTTPConnectionHelper.write() ["
+ getStatusLabel() + "]");
}
if (this.uploadPolicy.getDebugLevel() > 100) {
this.uploadPolicy
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -