Improved logging and added abstract URL and protocol logic
This commit is contained in:
parent
a0d0eddea3
commit
37692613bf
3 changed files with 87 additions and 36 deletions
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.net.http;
|
||||
|
||||
import zutil.converter.Converter;
|
||||
import zutil.net.http.HttpPrintStream.HttpMessageType;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
|
|
@ -44,7 +45,9 @@ public class HttpClient implements AutoCloseable {
|
|||
|
||||
// Request variables
|
||||
|
||||
private String protocol = "HTTP";
|
||||
private HttpURL url;
|
||||
private boolean absoluteURL;
|
||||
private String type;
|
||||
private HashMap<String, String> headers;
|
||||
private HashMap<String, String> cookies;
|
||||
|
|
@ -71,6 +74,13 @@ public class HttpClient implements AutoCloseable {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the protocol that should be provided by the request. Default is HTTP.
|
||||
*/
|
||||
public void setProtocol(String protocol) {
|
||||
this.protocol = protocol;
|
||||
}
|
||||
|
||||
public void setURL(URL url) {
|
||||
setURL(new HttpURL(url));
|
||||
}
|
||||
|
|
@ -79,6 +89,13 @@ public class HttpClient implements AutoCloseable {
|
|||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* If set to true the the request will contain the full URL instead of only the path to the page.
|
||||
*/
|
||||
public void setAbsoluteURL(boolean absoluteURL) {
|
||||
this.absoluteURL = absoluteURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a parameter to the request
|
||||
*/
|
||||
|
|
@ -120,8 +137,9 @@ public class HttpClient implements AutoCloseable {
|
|||
// ---------------------------------
|
||||
|
||||
HttpPrintStream request = new HttpPrintStream(conn.getOutputStream(), HttpMessageType.REQUEST);
|
||||
request.setProtocol(protocol);
|
||||
request.setRequestType(type);
|
||||
request.setRequestURL(url.getHttpURL());
|
||||
request.setRequestURL(absoluteURL ? url.getURL() : url.getHttpURL());
|
||||
request.setHeaders(headers);
|
||||
request.setCookies(cookies);
|
||||
|
||||
|
|
@ -161,4 +179,15 @@ public class HttpClient implements AutoCloseable {
|
|||
responseStream = null;
|
||||
responseHeader = null;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
tmp.append("{Type: ").append(type);
|
||||
tmp.append(", URL: \"").append(url).append('\"');
|
||||
tmp.append(", Headers: ").append(Converter.toString(headers));
|
||||
tmp.append(", Cookies: ").append(Converter.toString(cookies));
|
||||
tmp.append('}');
|
||||
return tmp.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,6 +40,7 @@ public class HttpHeader {
|
|||
public static final String HEADER_COOKIE = "Cookie";
|
||||
public static final String HEADER_SET_COOKIE = "Set-Cookie";
|
||||
public static final String HEADER_SERVER = "Server";
|
||||
public static final String HEADER_USER_AGENT = "User-Agent";
|
||||
|
||||
// Variables
|
||||
|
||||
|
|
@ -146,7 +147,7 @@ public class HttpHeader {
|
|||
}
|
||||
|
||||
public void setRequestURL(String url) {
|
||||
this.requestUrl = url.trim().replaceAll("//", "/");
|
||||
this.requestUrl = url;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -273,10 +274,19 @@ public class HttpHeader {
|
|||
|
||||
public String toString() {
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
tmp.append("{Type: ").append(requestType);
|
||||
tmp.append(", HTTP_version: ").append(protocol).append("/").append(protocolVersion);
|
||||
tmp.append(", URL: \"").append((String) requestUrl).append('\"');
|
||||
tmp.append(", URL_attr: ").append(toStringAttributes());
|
||||
tmp.append("{");
|
||||
|
||||
if (isRequest) {
|
||||
tmp.append("Type: ").append(requestType);
|
||||
tmp.append(", HTTP_version: ").append(protocol).append("/").append(protocolVersion);
|
||||
tmp.append(", URL: \"").append(requestUrl).append('\"');
|
||||
tmp.append(", URL_attr: ").append(toStringAttributes());
|
||||
} else {
|
||||
tmp.append("HTTP_version: ").append(protocol).append("/").append(protocolVersion);
|
||||
tmp.append(", Status_code: ").append(responseStatusCode);
|
||||
tmp.append(", Status_msg: \"").append(getResponseStatusString()).append('\"');
|
||||
}
|
||||
|
||||
tmp.append(", Headers: ").append(toStringHeaders());
|
||||
tmp.append(", Cookies: ").append(toStringCookies());
|
||||
tmp.append('}');
|
||||
|
|
|
|||
|
|
@ -98,6 +98,13 @@ public class HttpPrintStream extends OutputStream {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the protocol name that should be provided in the HTTP header.
|
||||
*/
|
||||
public void setProtocol(String protocol) {
|
||||
header.setProtocol(protocol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the http version that will be used in the http header
|
||||
*/
|
||||
|
|
@ -105,32 +112,6 @@ public class HttpPrintStream extends OutputStream {
|
|||
header.setProtocolVersion(version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cookie that will be sent to the client
|
||||
*
|
||||
* @param key is the name of the cookie
|
||||
* @param value is the value of the cookie
|
||||
* @throws IllegalStateException if the header has already been sent
|
||||
*/
|
||||
public void setCookie(String key, String value) {
|
||||
headerSentCheck();
|
||||
|
||||
header.setCookie(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an header value
|
||||
*
|
||||
* @param key is the header name
|
||||
* @param value is the value of the header
|
||||
* @throws IllegalStateException if the header has already been sent
|
||||
*/
|
||||
public void setHeader(String key, String value) {
|
||||
headerSentCheck();
|
||||
|
||||
header.setHeader(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the status code of the message, ONLY available in HTTP RESPONSE
|
||||
*
|
||||
|
|
@ -173,6 +154,32 @@ public class HttpPrintStream extends OutputStream {
|
|||
header.setRequestURL(req_url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a cookie that will be sent to the client
|
||||
*
|
||||
* @param key is the name of the cookie
|
||||
* @param value is the value of the cookie
|
||||
* @throws IllegalStateException if the header has already been sent
|
||||
*/
|
||||
public void setCookie(String key, String value) {
|
||||
headerSentCheck();
|
||||
|
||||
header.setCookie(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an header value
|
||||
*
|
||||
* @param key is the header name
|
||||
* @param value is the value of the header
|
||||
* @throws IllegalStateException if the header has already been sent
|
||||
*/
|
||||
public void setHeader(String key, String value) {
|
||||
headerSentCheck();
|
||||
|
||||
header.setHeader(key, value);
|
||||
}
|
||||
|
||||
protected void setHeaders(HashMap<String, String> map) {
|
||||
header.setHeaders(map);
|
||||
}
|
||||
|
|
@ -219,10 +226,15 @@ public class HttpPrintStream extends OutputStream {
|
|||
*/
|
||||
private void printForced(String s) {
|
||||
if (header != null) {
|
||||
if (header.isRequest())
|
||||
out.print(header.getRequestType() + " " + header.getRequestURL() + " " + header.getProtocol() + "/" + header.getProtocolVersion());
|
||||
else
|
||||
out.print(header.getProtocol() + "/" + header.getProtocolVersion() + " " + header.getResponseStatusCode() + " " + header.getResponseStatusString());
|
||||
if (header.isRequest()) {
|
||||
out.print(header.getRequestType() + " " +
|
||||
header.getRequestURL() + " " +
|
||||
header.getProtocol() + "/" + header.getProtocolVersion());
|
||||
} else {
|
||||
out.print(header.getProtocol() + "/" + header.getProtocolVersion() + " " +
|
||||
header.getResponseStatusCode() + " " +
|
||||
header.getResponseStatusString());
|
||||
}
|
||||
out.println();
|
||||
|
||||
// Send headers
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue