Skip to content

Commit

Permalink
integrate basic api for api-handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Conor Flynn committed Feb 6, 2023
1 parent 1fff7d0 commit 5b89139
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.util.HashMap;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.stream.external.request.core.RequestFramework;
import org.stream.external.request.core.RequestManager;

import jakarta.annotation.PostConstruct;
Expand All @@ -31,7 +34,45 @@ public void initialize() {
@PostMapping
@CrossOrigin
@RequestMapping(path = {"/request"})
public ResponseEntity<String> handleRequest(@RequestParam String name) {
return null;
public ResponseEntity<String> handleRequest(
@RequestParam(name="name", required=true) String name,
@RequestParam(name="properties", required=false) String properties,
@RequestParam(name="headers", required=false) String headers) {

// assert that request exists
if(!RequestManager.hasRequestFormat(name))
return new ResponseEntity<String>(String.format("Request name <%s> does not exist.", name), HttpStatus.UNPROCESSABLE_ENTITY);

// retrieve request framework
RequestFramework request = RequestManager.getRequestFormat(name);

// parse properties and headers
HashMap<String, String> properties_map = new HashMap<String, String>();
HashMap<String, String> headers_map = new HashMap<String, String>();

if(properties != null) {
String[] arr = properties.split(",");
if(arr.length % 2 != 0)
return new ResponseEntity<String>("Properties must be in the format of <key, value> pairs.", HttpStatus.PRECONDITION_FAILED);

for(int i = 0; i < arr.length; i+=2)
properties_map.put(arr[i], arr[i + 1]);
}

if(headers != null) {
String[] arr = headers.split(",");
if(arr.length % 2 != 0)
return new ResponseEntity<String>("Headers must be in the format of <key, value> pairs.", HttpStatus.PRECONDITION_FAILED);

for(int i = 0; i < arr.length; i+=2)
headers_map.put(arr[i], arr[i + 1]);
}

// submit request
String response = request.request(properties_map, headers_map);
if(response != null)
return new ResponseEntity<String>(response, HttpStatus.SERVICE_UNAVAILABLE);

return new ResponseEntity<String>("", HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,49 +151,66 @@ public final String[] getPath() {
return path;
}

@SuppressWarnings("unchecked")
public final void request(HashMap<String, String> properties, HashMap<String, String> headers) {
// clone maps and push to process
HashMap<String, String> cloned_properties = (HashMap<String, String>) properties.clone();
HashMap<String, String> cloned_headers = (HashMap<String, String>) headers.clone();
public final synchronized String request(HashMap<String, String> properties, HashMap<String, String> headers) {
// create new maps and add all properties and headers
HashMap<String, String> properties_combined = new HashMap<String, String>();
HashMap<String, String> headers_combined = new HashMap<String, String>();

process(url, cloned_properties, cloned_headers);
// add all default properties
for(String key : this.properties.keySet())
properties_combined.put(key, this.properties.get(key));

for(String key : this.headers.keySet())
headers_combined.put(key, this.headers.get(key));

// add all defined properties
for(String key : properties.keySet())
properties_combined.put(key, properties.get(key));

for(String key : headers.keySet())
headers_combined.put(key, headers.get(key));

return process(url, properties_combined, headers_combined);
}

protected void process(String url, HashMap<String, String> properties, HashMap<String, String> headers) {
protected String process(String url, HashMap<String, String> properties, HashMap<String, String> headers) {
OkHttpClient client = new OkHttpClient();
Request request = getRequest(url, properties, headers);
if(request == null) {
System.err.println("Malformed request, killing process.");
return;
return "Malformed request, killing process.";
}

Response response = null;
String body = null;
try {
response = client.newCall(request).execute();
body = response.body().string().toString();
if(response.code() != 200) {
System.err.println(String.format("Request Failure code <%d> url <%s>\nmsg <%s>", response.code(), request.url().toString(), response.message()));
System.exit(1);
System.err.println(String.format("Request Failure code <%d> url <%s>\nbody:\n%s", response.code(), request.url().toString(), body));
return String.format("Request Failure code <%d> url <%s>\nbody:\n%s", response.code(), request.url().toString(), body);
}
} catch (IOException e) {
e.printStackTrace();
}

if(response == null) {
if(body == null) {
System.err.println("Response had fatal issue, killing process.");
return;
return "Response had fatal issue, killing process.";
}

// send to specific request handler
try {
String body = response.body().string().toString();
handle(body, properties, headers);
} catch(Exception e) {
e.printStackTrace();
return e.toString();
}

return null;
}

protected abstract void handle(String json, HashMap<String, String> properties, HashMap<String, String> headers);
protected abstract String handle(String json, HashMap<String, String> properties, HashMap<String, String> headers);

public abstract String getType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ public static void initialize() throws InstantiationException, IllegalAccessExce
}
}

public static final boolean hasRequestFormat(String name) {
return requests.containsKey(name);
}

public static final RequestFramework getRequestFormat(String name) {
return requests.get(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ public String getType() {
return "parameterized";
}

public void handle(String json, HashMap<String, String> properties, HashMap<String, String> headers) {
public String handle(String json, HashMap<String, String> properties, HashMap<String, String> headers) {
// parse json formatting
JSONObject obj = new JSONObject(json);

// validate all required parameters are present:
// check for required tag -l
if(!hasTag("-l")) {
System.err.println(String.format("Missing required recursive parameter <-l>"));
return;
return "Missing required recursive parameter <-l>";
}

// check for required tag -t
if(!hasTag("-t")) {
System.err.println(String.format("Missing required recursive parameter <-t>"));
return;
return "Missing required recursive parameter <-t>";
}

// check for -l being an integer
Expand All @@ -47,7 +47,7 @@ public void handle(String json, HashMap<String, String> properties, HashMap<Stri
} catch(Exception e) {
e.printStackTrace();
System.err.println(String.format("Value following <-l> must be an integer."));
return;
return "Value following <-l> must be an integer.";
}

// validate that recursion location is valid
Expand All @@ -62,7 +62,7 @@ public void handle(String json, HashMap<String, String> properties, HashMap<Stri
base = base.getJSONObject(location[i]);
} else {
System.err.println("Response does not contain proper recursive parameter location at: " + location[i]);
return;
return "Response does not contain proper recursive parameter location at: " + location[i];
}
}

Expand All @@ -72,7 +72,7 @@ public void handle(String json, HashMap<String, String> properties, HashMap<Stri
else if(getTag("-t").equals("incremental")) {
if(!properties.containsKey(getRecursiveLocation()[0])) {
System.err.println("Properties do not contain incremental parameter listed in: " + getRecursiveLocation()[0]);
return;
return "Properties do not contain incremental parameter listed in: " + getRecursiveLocation()[0];
}

recursive_parameter = properties.get(getRecursiveLocation()[0]);
Expand All @@ -81,7 +81,7 @@ else if(getTag("-t").equals("incremental")) {
// validate that recursive parameter has been set
if(recursive_parameter == null) {
System.err.println("Fatal error. Recursive parameter is null after successful initialization.");
return;
return "Fatal error. Recursive parameter is null after successful initialization.";
}


Expand All @@ -95,7 +95,7 @@ else if(getTag("-t").equals("incremental")) {
data = obj.getJSONArray(path[i]);
} catch(Exception e) {
System.err.println("obj path type is not of type <JSONArray>. Cannot parse.");
return;
return "obj path type is not of type <JSONArray>. Cannot parse.";
}
}
}
Expand All @@ -105,15 +105,15 @@ else if(obj.has(path[i])) {
obj = obj.getJSONObject(path[i]);
} catch(Exception e) {
System.err.println("obj path type step is not of type <JSONObject>. Cannot parse.");
return;
return "obj path type step is not of type <JSONObject>. Cannot parse.";
}
}
}

// validate that data is non-empty
if(data == null) {
System.err.println("Data array retrieval had fatal error, killing process.");
return;
return "Data array retrieval had fatal error, killing process.";
}

// extract and print data
Expand All @@ -125,7 +125,7 @@ else if(obj.has(path[i])) {
// initiate recursive call
// if under limit requested, terminate call
if(data.length() < limit)
return;
return null;

// extract recursive parameter and apply to next call
String rec_type = getTag("-t");
Expand All @@ -150,13 +150,13 @@ else if(obj.has(path[i])) {
param = Integer.parseInt(recursive_parameter);
} catch(Exception e) {
e.printStackTrace();
System.err.println(String.format("Recursive parameter <%s> is not of type integer.", getTag("-rp")));
return;
System.err.println(String.format("Recursive parameter <%s> is not of type integer.", recursive_parameter));
return String.format("Recursive parameter <%s> is not of type integer.", recursive_parameter);
}

if(param == -1) {
System.err.println("Fatal parsing error occured.");
return;
return "Fatal parsing error occured.";
}

param += 1;
Expand All @@ -176,6 +176,6 @@ else if(obj.has(path[i])) {
}

// increment param, replace, and execute
process(url, properties, headers);
return process(url, properties, headers);
}
}

0 comments on commit 5b89139

Please sign in to comment.