Skip to content

Commit

Permalink
HTTP PUT: backend code for #141
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhaskarla authored and Bhaskarla committed Apr 16, 2021
1 parent 3b1b0f0 commit 53583bf
Show file tree
Hide file tree
Showing 21 changed files with 394 additions and 167 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package gov.nist.asbestos.http.operations;

import gov.nist.asbestos.http.headers.Header;
import gov.nist.asbestos.http.util.Gzip;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HttpMethod extends HttpBase {
private Header locationHeader = null;
protected String theHttpVerb;

public HttpMethod(String theHttpVerb) {
this.theHttpVerb = theHttpVerb;
}

// content is unzipped
protected void doVerb(URI uri, Map<String, String> headers, byte[] content) throws IOException {
//String stringContent = new String(content);
HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) uri.toURL().openConnection();
if (_requestHeaders != null)
addHeaders(connection, _requestHeaders);
if (headers != null)
addHeaders(connection, headers);
requestHeadersList = connection.getRequestProperties();
connection.setRequestMethod(theHttpVerb);
connection.setDoOutput(true);
connection.setDoInput(true);
// TODO use proper charset (from input)
if (content != null) {
if (isRequestGzipEncoded())
connection.getOutputStream().write(Gzip.compressGZIP(content));
else
connection.getOutputStream().write(content);
}
status = connection.getResponseCode();
try {
setResponseHeadersList(connection.getHeaderFields());
InputStream is;
if (status >= 400)
is = connection.getErrorStream();
else
is = connection.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
if (isResponseGzipEncoded())
setResponse(Gzip.decompressGZIP(bytes));
else
setResponse(bytes);
} catch (Throwable t) {
// ok - won't always be available
}
} finally {
if (connection != null)
connection.disconnect();
}
if (getResponseHeaders() != null)
locationHeader = getResponseHeaders().get("Location");
}

protected void submit() {
try {
doVerb(uri, getRequestHeaders().getAll(), getRequest());
} catch (IOException e) {
status = 400;
String msg = uri + "\n" + e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e);
setResponseText(msg);
setResponse(msg.getBytes());
}
}

protected void submit(URI uri, String json) throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");
doVerb(uri, headers, json == null ? null : json.getBytes());
}

public HttpMethod run() throws IOException {
Objects.requireNonNull(uri);
doVerb(uri, getRequestHeaders().getAll(), getRequest());
return this;
}

public String getVerb() {
return theHttpVerb;
}

public Header getLocationHeader() {
return locationHeader;
}

public HttpMethod setLocation(String location) {
this.locationHeader = new Header("Location", location);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,102 +1,28 @@
package gov.nist.asbestos.http.operations;

import gov.nist.asbestos.http.headers.Header;
import gov.nist.asbestos.http.util.Gzip;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HttpPost extends HttpBase {
private Header locationHeader = null;
public class HttpPost extends HttpMethod {

// content is unzipped
private void post(URI uri, Map<String, String> headers, byte[] content) throws IOException {
//String stringContent = new String(content);
HttpURLConnection connection = null;

try {
connection = (HttpURLConnection) uri.toURL().openConnection();
if (_requestHeaders != null)
addHeaders(connection, _requestHeaders);
if (headers != null)
addHeaders(connection, headers);
requestHeadersList = connection.getRequestProperties();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
// TODO use proper charset (from input)
if (content != null) {
if (isRequestGzipEncoded())
connection.getOutputStream().write(Gzip.compressGZIP(content));
else
connection.getOutputStream().write(content);
}
status = connection.getResponseCode();
try {
setResponseHeadersList(connection.getHeaderFields());
InputStream is;
if (status >= 400)
is = connection.getErrorStream();
else
is = connection.getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
if (isResponseGzipEncoded())
setResponse(Gzip.decompressGZIP(bytes));
else
setResponse(bytes);
} catch (Throwable t) {
// ok - won't always be available
}
} finally {
if (connection != null)
connection.disconnect();
}
if (getResponseHeaders() != null)
locationHeader = getResponseHeaders().get("Location");
public HttpPost() {
super("POST");
}

public HttpPost post() {
try {
post(uri, getRequestHeaders().getAll(), getRequest());
} catch (IOException e) {
status = 400;
String msg = uri + "\n" + e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e);
setResponseText(msg);
setResponse(msg.getBytes());
}
submit();
return this;
}

public HttpPost postJson(URI uri, String json) throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");
post(uri, headers, json == null ? null : json.getBytes());
submit(uri, json);
return this;
}

public HttpPost run() throws IOException {
Objects.requireNonNull(uri);
post(uri, getRequestHeaders().getAll(), getRequest());
super.run();
return this;
}

public String getVerb() {
return "POST";
}

public Header getLocationHeader() {
return locationHeader;
}

public HttpPost setLocation(String location) {
this.locationHeader = new Header("Location", location);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gov.nist.asbestos.http.operations;


import java.io.IOException;
import java.net.URI;

public class HttpPut extends HttpMethod {

public HttpPut() {
super("PUT");
}

public HttpPut put() {
submit();
return this;
}

public HttpPut putJson(URI uri, String json) throws IOException {
submit(uri, json);
return this;
}

public HttpPut run() throws IOException {
super.run();
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package gov.nist.asbestos.client.log;

public class ChannelDoesNotExistException extends RuntimeException {
public ChannelDoesNotExistException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public SimStore create(ChannelConfig channelConfig) {
public SimStore open() {
getStore(false);
if (!existsChannelDir())
throw new RuntimeException("Channel does not exist");
throw new ChannelDoesNotExistException("Channel does not exist");
File file = new File(getChannelDir(), CHANNEL_CONFIG_FILE);
channelConfig = ChannelConfigFactory.load(file);
channelId = getSimId(channelConfig);
Expand Down
Loading

0 comments on commit 53583bf

Please sign in to comment.