Permalink
Browse files

Various improvements and bugfixes in http_request

  • Loading branch information...
LadyCailin committed Dec 20, 2018
1 parent bdbe7d9 commit f35e4da93374449151dee95a49b456502cb69111
@@ -144,21 +144,44 @@ public static void write(String data, File file, int mode, boolean create) throw
}

/**
* Writes out a String to the given file, either appending or overwriting, depending on the selected mode. If create
* Writes out byte data to the given file, either appending or overwriting, depending on the selected mode. If create
* is true, will attempt to create the file and parent directories if need be.
*
* @param data The string to write to the file
* @param file The File to write to
* @param mode The mode in which to write the file
* @param create If true, will create the parent directories
* @throws IOException If the File cannot be written to
*/
public static void write(byte[] data, File file, FileWriteMode mode, boolean create) throws IOException {
if(mode == FileWriteMode.SAFE_WRITE) {
if(file.exists()) {
throw new IOException("Cannot create file, SAFE_WRITE set, and file already exists [" + file + "]");
}
mode = FileWriteMode.OVERWRITE;
}
if(mode == FileWriteMode.OVERWRITE) {
write(data, file, OVERWRITE, create);
} else if(mode == FileWriteMode.APPEND) {
write(data, file, APPEND, create);
} else {
throw new Error("Unaccounted for FileWriteMode");
}
}

/**
* Writes out byte data to the given file, either appending or overwriting, depending on the selected mode. If create
* is true, will attempt to create the file and parent directories if need be.
*
* @param data The string to write to the file
* @param file The File to write to
* @param mode Either OVERWRITE or APPEND
* @param create If true, will create the parent directories
* @throws IOException If the File f cannot be written to
*/
public static void write(byte[] data, File file, int mode, boolean create) throws IOException {
boolean append;
if(mode == OVERWRITE) {
append = false;
} else {
append = true;
}
append = mode != OVERWRITE;
if(create && !file.exists()) {
if(file.getAbsoluteFile().getParentFile() != null) {
file.getAbsoluteFile().getParentFile().mkdirs();
@@ -0,0 +1,19 @@
package com.laytonsmith.PureUtilities.Common;

/**
*
*/
public enum FileWriteMode {
/**
* Replaces any existing file with the new contents (or simply creates a new file if it doesn't already exist).
*/
OVERWRITE,
/**
* Appends to the existing file, or if one does not exist, simply creates the file with the new content.
*/
APPEND,
/**
* Only writes the file if it doesn't already exist.
*/
SAFE_WRITE
}
@@ -7,6 +7,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
@@ -85,14 +86,15 @@ public static String GetString(InputStream in, String encoding) throws Unsupport

/**
* Fully reads in a stream, as efficiently as possible, and returns a byte array.
* The input stream is not closed afterwards.
*
* @param in
* @return
* @throws IOException
*/
public static byte[] GetBytes(InputStream in) throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
List<Byte> bytes = new ArrayList<Byte>();
List<Byte> bytes = new ArrayList<>();
int i;
while((i = bis.read()) != -1) {
bytes.add(((byte) i));
@@ -1,22 +1,25 @@
package com.laytonsmith.PureUtilities.Web;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* This class wraps all the data that an HTTP response contains.
*/
public final class HTTPResponse {

private String rawResponse = null;
private final List<HTTPHeader> headers = new LinkedList<HTTPHeader>();
private final List<HTTPHeader> headers = new LinkedList<>();
private final String responseText;
private final int responseCode;
private final String content;
private final byte[] content;
private final String httpVersion;

/**
@@ -30,7 +33,7 @@
* @param httpVersion The HTTP version that the server is using, for instance "1.0"
*/
public HTTPResponse(String responseText, int responseCode, Map<String, List<String>> headers,
String response, String httpVersion) {
byte[] response, String httpVersion) {
this.responseText = responseText;
this.responseCode = responseCode;
for(String key : headers.keySet()) {
@@ -47,10 +50,32 @@ public HTTPResponse(String responseText, int responseCode, Map<String, List<Stri
*
* @return
*/
public String getContent() {
public byte[] getContent() {
return this.content;
}

/**
* Returns the content as UTF-8 text.
* @return
*/
public String getContentAsString() {
try {
return getContentAsString("UTF-8");
} catch(UnsupportedEncodingException ex) {
throw new Error(ex);
}
}

/**
* Returns the content as text with the specified encoding.
* @param encoding
* @return
* @throws UnsupportedEncodingException
*/
public String getContentAsString(String encoding) throws UnsupportedEncodingException {
return new String(getContent(), encoding);
}

/**
* Returns the HTTP version that the server is using. This string will be in the format HTTP/1.0 for instance.
*
@@ -81,7 +106,7 @@ public String getFirstHeader(String key) {
* @return
*/
public Set<String> getHeaderNames() {
Set<String> set = new HashSet<String>();
Set<String> set = new HashSet<>();
for(HTTPHeader h : headers) {
if(h.getHeader() != null) {
set.add(h.getHeader());
@@ -97,7 +122,7 @@ public String getFirstHeader(String key) {
* @return
*/
public List<String> getHeaders(String key) {
List<String> list = new ArrayList<String>();
List<String> list = new ArrayList<>();
for(HTTPHeader header : headers) {
if((header.getHeader() == null && key == null) || (header.getHeader() != null && header.getHeader().equalsIgnoreCase(key))) {
list.add(header.getValue());
@@ -134,7 +159,7 @@ public String toString() {
}
rawResponse += h.getHeader() + ": " + h.getValue() + "\n";
}
rawResponse += "\n" + content;
rawResponse += "\n" + "<bytes of length " + content.length + ">";
}
return rawResponse;
}
@@ -1,5 +1,6 @@
package com.laytonsmith.PureUtilities.Web;

import com.laytonsmith.PureUtilities.Common.FileWriteMode;
import java.io.File;
import java.net.Proxy;
import java.util.Arrays;
@@ -25,6 +26,7 @@
private Proxy proxy = null;
private byte[] rawParameter;
private File downloadTo;
private FileWriteMode downloadStrategy = FileWriteMode.SAFE_WRITE;
private boolean blocking = false;
private boolean disableCertChecking = false;
private boolean useDefaultTrustStore = true;
@@ -242,6 +244,16 @@ public RequestSettings setDownloadTo(File downloadTo) {
return this;
}

/**
* Sets the download strategy.
* @param downloadStrategy
* @return
*/
public RequestSettings setDownloadStrategy(FileWriteMode downloadStrategy) {
this.downloadStrategy = downloadStrategy;
return this;
}

/**
*
* @return The file location to download to, or null if this shouldn't save the request as a file.
@@ -250,6 +262,14 @@ public File getDownloadTo() {
return this.downloadTo;
}

/**
* Returns the configured download strategy. The default is {@link FileWriteMode#SAFE_WRITE}
* @return
*/
public FileWriteMode getDownloadStrategy() {
return this.downloadStrategy;
}

/**
* Sets the logger to use. If null, disables logging.
*
Oops, something went wrong.

0 comments on commit f35e4da

Please sign in to comment.