-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bhaskarla
authored and
Bhaskarla
committed
Apr 16, 2021
1 parent
3b1b0f0
commit 53583bf
Showing
21 changed files
with
394 additions
and
167 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
asbestos-http/src/main/java/gov/nist/asbestos/http/operations/HttpMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
86 changes: 6 additions & 80 deletions
86
asbestos-http/src/main/java/gov/nist/asbestos/http/operations/HttpPost.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
asbestos-http/src/main/java/gov/nist/asbestos/http/operations/HttpPut.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
...roxy-support/src/main/java/gov/nist/asbestos/client/log/ChannelDoesNotExistException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.