Skip to content

Commit

Permalink
Upgrade HttpEasy
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Sumner committed Oct 30, 2016
1 parent e2faf51 commit 4d27fda
Show file tree
Hide file tree
Showing 14 changed files with 998 additions and 289 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/inedo/http/DataWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;

import org.slf4j.Logger;

/**
* Http request data writer interface.
*
Expand All @@ -12,8 +14,10 @@ interface DataWriter {
/**
* Add data to Http request.
*
* @param logger Logger to write details to
*
* @throws IOException
*/
public void write() throws IOException;
public void write(Logger logger) throws IOException;

}
55 changes: 55 additions & 0 deletions src/main/java/com/inedo/http/Family.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.inedo.http;

/**
* An enumeration representing the class of an http response status code.
*/
public enum Family {

/**
* {@code 1xx} HTTP status codes.
*/
INFORMATIONAL,
/**
* {@code 2xx} HTTP status codes.
*/
SUCCESSFUL,
/**
* {@code 3xx} HTTP status codes.
*/
REDIRECTION,
/**
* {@code 4xx} HTTP status codes.
*/
CLIENT_ERROR,
/**
* {@code 5xx} HTTP status codes.
*/
SERVER_ERROR,
/**
* Other, unrecognized HTTP status codes.
*/
OTHER;

/**
* Get the response status family for the status code.
*
* @param statusCode response status code to get the family for.
* @return family of the response status code.
*/
public static Family familyOf(final int statusCode) {
switch (statusCode / 100) {
case 1:
return Family.INFORMATIONAL;
case 2:
return Family.SUCCESSFUL;
case 3:
return Family.REDIRECTION;
case 4:
return Family.CLIENT_ERROR;
case 5:
return Family.SERVER_ERROR;
default:
return Family.OTHER;
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/inedo/http/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ class Field {
final MediaType type;
final String name;
final Object value;
final String fileName;

/**
* Create a new form field.
*
* @param name Field name
* @param value Field contents
* @param type Field media type
* @param fileName Field file name for attachments
*/
Field(String name, Object value, MediaType type) {
Field(String name, Object value, MediaType type, String fileName) {
this.name = name;
this.value = value;
this.type = type;
this.fileName = fileName;
}
}
117 changes: 92 additions & 25 deletions src/main/java/com/inedo/http/FormDataWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
Expand All @@ -12,6 +13,8 @@
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.slf4j.Logger;

import com.google.common.net.MediaType;

/**
Expand All @@ -22,10 +25,11 @@
class FormDataWriter implements DataWriter {
private final HttpURLConnection connection;
private final List<Field> fields;
private final String boundary = "---FormBoundary" + System.currentTimeMillis();
private final String boundary = "FormBoundary" + System.currentTimeMillis();
private OutputStream outputStream;
private PrintWriter writer = null;
private static final String LINE_FEED = "\r\n";
private StringBuilder logBuffer = null;

/**
* Constructor.
Expand All @@ -44,38 +48,80 @@ public FormDataWriter(HttpURLConnection connection, String query, List<Field> fi
}

@Override
public void write() throws IOException {
public void write(Logger logger) throws IOException {
outputStream = connection.getOutputStream();

if (logger != null) {
logBuffer = new StringBuilder();
}

try {
writer = new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8), true);

for (Field field : fields) {
if (field.value instanceof File) {
addFilePart(field.name, (File)field.value, field.type);
} else if (field.value instanceof InputStream) {
addFilePart(field.name, (InputStream) field.value, field.type, field.fileName);
} else {
addFormField(field.name, field.value);
}
}

writeFinalBoundary();
} finally {
if (logger != null) {
logger.trace("With Content:{}\t{}", LINE_FEED, logBuffer.toString().replace(LINE_FEED, LINE_FEED + "\t"));
}

if (writer != null) {
writer.close();
}
}
}

private void writeFieldBoundary() {
StringBuilder buf = new StringBuilder();
buf.append("--").append(boundary).append(LINE_FEED);

if (logBuffer != null) {
logBuffer.append(buf);
}

writer.append(buf);
}

private void writeFinalBoundary() {
StringBuilder buf = new StringBuilder();
buf.append("--").append(boundary).append("--").append(LINE_FEED);

if (logBuffer != null) {
logBuffer.append(buf);
}

writer.append(buf);
}

/**
* Adds a form field to the request.
*
* @param name field name
* @param value field value
*/
public void addFormField(String name, Object value) {
writer.append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
writer.append("Content-Type: text/plain; charset=utf-8").append(LINE_FEED);
writer.append(LINE_FEED);
writer.append(String.valueOf(value)).append(LINE_FEED);
private void addFormField(String name, Object value) {
StringBuilder buf = new StringBuilder();

writeFieldBoundary();
buf.append("Content-Disposition: form-data; name=\"" + name + "\"").append(LINE_FEED);
// buf.append("Content-Type: text/plain; charset=utf-8").append(LINE_FEED);
buf.append(LINE_FEED);
buf.append(String.valueOf(value)).append(LINE_FEED);

if (logBuffer != null) {
logBuffer.append(buf);
}

writer.append(buf);
writer.flush();
}

Expand All @@ -87,30 +133,51 @@ public void addFormField(String name, Object value) {
* @param type MediaType of the file
* @throws IOException
*/
public void addFilePart(String fieldName, File uploadFile, MediaType type) throws IOException {
String fileName = uploadFile.getName();
private void addFilePart(String fieldName, File uploadFile, MediaType type) throws IOException {
try (FileInputStream inputStream = new FileInputStream(uploadFile)) {
addFilePart(fieldName, inputStream, type, uploadFile.getName());
}
}

/**
* Adds a upload stream section to the request.
*
* @param fieldName name attribute in <input type="file" name="..." />
* @param inputStream a File to be uploaded
* @param type MediaType of the file
* @param fileName name of file to be uploaded
* @throws IOException
*/
private void addFilePart(String fieldName, InputStream inputStream, MediaType type, String fileName) throws IOException {
StringBuilder buf = new StringBuilder();

writer.append(boundary).append(LINE_FEED);
writer.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE_FEED);
writeFieldBoundary();
buf.append("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\"" + fileName + "\"").append(LINE_FEED);
if (type == null) {
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
buf.append("Content-Type: " + URLConnection.guessContentTypeFromName(fileName)).append(LINE_FEED);
} else {
writer.append("Content-Type: " + type.toString()).append(LINE_FEED);
buf.append("Content-Type: " + type.toString()).append(LINE_FEED);
}
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
writer.append(LINE_FEED);

// buf.append("Content-Transfer-Encoding: binary").append(LINE_FEED);
buf.append(LINE_FEED);

if (logBuffer != null) {
logBuffer.append(buf);
logBuffer.append("... Content of file ").append(fileName).append(" ...").append(LINE_FEED);
}

writer.append(buf);
writer.flush();

try (FileInputStream inputStream = new FileInputStream(uploadFile)) {
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();

writer.append(LINE_FEED);
writer.flush();
writer.flush();
}
}
8 changes: 7 additions & 1 deletion src/main/java/com/inedo/http/FormUrlEncodedDataWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.slf4j.Logger;

/**
* Attach an "application/x-www-form-urlencoded" form to an http request.
*
Expand Down Expand Up @@ -52,7 +54,11 @@ public FormUrlEncodedDataWriter(HttpURLConnection connection, String query, List
}

@Override
public void write() throws IOException {
public void write(Logger logger) throws IOException {
if (logger != null) {
logger.trace("With Content:{}\t{}", System.lineSeparator(), new String(postEndcoded, "UTF-8"));
}

try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(postEndcoded);
}
Expand Down

0 comments on commit 4d27fda

Please sign in to comment.