Skip to content

Commit 5590557

Browse files
feat(response): possibility of placeholder vars in standard responses to improve error details
1 parent 58c7eb2 commit 5590557

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

src/main/java/net/hexonet/apiconnector/APIClient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ public Response request(Map<String, Object> cmd) {
468468
// request command to API
469469
String data = this.getPOSTData(newcmd);
470470
String secured = this.getPOSTData(newcmd, true);
471+
Map<String, String> cfg = new HashMap<String, String>();
472+
cfg.put("CONNECTION_URL", this.socketURL);
471473

472474
StringBuilder response;
473475
try {
474476
response = new StringBuilder("");
475-
URL myurl = new URL(this.socketURL);
477+
URL myurl = new URL(cfg.get("CONNECTION_URL"));
476478
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
477479
if (this.curlopts.containsKey("PROXY")) {
478480
URL proxyurl = new URL(this.curlopts.get("PROXY"));
@@ -515,7 +517,7 @@ public Response request(Map<String, Object> cmd) {
515517
System.err.println(e);
516518
}
517519
}
518-
Response r = new Response(response.toString(), newcmd);
520+
Response r = new Response(response.toString(), newcmd, cfg);
519521
if (this.debugMode) {
520522
System.out.println(secured);
521523
System.out.println(newcmd);

src/main/java/net/hexonet/apiconnector/Response.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.HashMap;
55
import java.util.Iterator;
66
import java.util.Map;
7+
import java.util.regex.Pattern;
8+
import java.util.regex.Matcher;
79

810
/**
911
* Response covers all functionality to wrap a Backend API Response like accessing data
@@ -41,9 +43,31 @@ public class Response extends ResponseTemplate {
4143
* @param raw API plain response
4244
* @param cmd API command used within this request
4345
*/
44-
@SuppressWarnings("unchecked") // not really a good way ...
4546
public Response(String raw, Map<String, String> cmd) {
47+
this(raw, cmd, Map.ofEntries());
48+
}
49+
50+
/**
51+
* Constructor
52+
*
53+
* @param raw API plain response
54+
* @param cmd API command used within this request
55+
* @param ph place holder variables replacements
56+
*/
57+
@SuppressWarnings("unchecked") // not really a good way ...
58+
public Response(String raw, Map<String, String> cmd, Map<String, String> ph) {
4659
super(raw);
60+
61+
String regex = "\\{[^}]+\\}";
62+
Pattern pattern = Pattern.compile(regex);
63+
Matcher matcher = pattern.matcher(this.raw);
64+
if (matcher.find()) {
65+
for (Map.Entry<String, String> entry : ph.entrySet()) {
66+
this.raw = this.raw.replace("{" + entry.getKey() + "}", entry.getValue());
67+
}
68+
super.init(this.raw.replaceAll(regex, ""));
69+
}
70+
4771
this.command = new HashMap<String, String>(cmd);
4872
if (this.command.containsKey("PASSWORD")) { // make password no longer accessible
4973
this.command.replace("PASSWORD", "***");

src/main/java/net/hexonet/apiconnector/ResponseTemplate.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ public ResponseTemplate(String raw) {
2525
if (raw.length() == 0) {
2626
raw = ResponseTemplateManager.getInstance().getTemplate("empty").getPlain();
2727
}
28+
this.init(raw);
29+
}
30+
31+
/**
32+
* init hash - workaround for not calling constructor in response class twice (TO DO)
33+
*
34+
* @param raw
35+
*/
36+
protected void init(String raw) {
2837
this.raw = raw;
2938
this.hash = ResponseParser.parse(raw);
3039
if (!this.hash.containsKey("CODE") || !this.hash.containsKey("DESCRIPTION")) {

src/main/java/net/hexonet/apiconnector/ResponseTemplateManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private ResponseTemplateManager() {
2525
this.templates.put("404", this.generateTemplate("421", "Page not found"));
2626
this.templates.put("500", this.generateTemplate("500", "Internal server error"));
2727
this.templates.put("empty", this.generateTemplate("423",
28-
"Empty API response. Probably unreachable API end point"));
28+
"Empty API response. Probably unreachable API end point {CONNECTION_URL}"));
2929
this.templates.put("error", this.generateTemplate("421",
3030
"Command failed due to server error. Client should try again"));
3131
this.templates.put("expired", this.generateTemplate("530", "SESSION NOT FOUND"));

src/test/java/net/hexonet/apiconnector/ResponseTemplateTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ResponseTemplateTest {
1515
public void constructorVars() {
1616
ResponseTemplate tpl = new ResponseTemplate("");
1717
assertEquals(423, tpl.getCode());
18-
assertEquals("Empty API response. Probably unreachable API end point",
18+
assertEquals("Empty API response. Probably unreachable API end point {CONNECTION_URL}",
1919
tpl.getDescription());
2020
}
2121

@@ -38,7 +38,7 @@ public void getHash() {
3838
ResponseTemplate tpl = new ResponseTemplate("");
3939
Map<String, Object> h = tpl.getHash();
4040
assertEquals("423", (String) h.get("CODE"));
41-
assertEquals("Empty API response. Probably unreachable API end point",
41+
assertEquals("Empty API response. Probably unreachable API end point {CONNECTION_URL}",
4242
(String) h.get("DESCRIPTION"));
4343
}
4444

src/test/java/net/hexonet/apiconnector/ResponseTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Arrays;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.util.regex.Pattern;
13+
import java.util.regex.Matcher;
1214
import org.junit.Test;
1315

1416
/**
@@ -44,6 +46,31 @@ public void getCommandPlainSecure() {
4446
assertEquals(str, r.getCommandPlain());
4547
}
4648

49+
/**
50+
* Test place holder vars replacement mechanism
51+
*/
52+
@Test
53+
public void placeHolderReplacements() {
54+
Map<String, String> cmd = new HashMap<String, String>();
55+
cmd.put("COMMAND", "StatusAccount");
56+
57+
// ensure no vars are returned in response, just in case no place holder replacements are
58+
// provided
59+
Response r = new Response("", cmd);
60+
String regex = "\\{[^}]+\\}";
61+
Pattern pattern = Pattern.compile(regex);
62+
Matcher matcher = pattern.matcher(r.getDescription());
63+
assertFalse(matcher.find());
64+
65+
// ensure variable replacements are correctly handled in case place holder replacements are
66+
// provided
67+
r = new Response("", cmd, Map.ofEntries(Map.entry("CONNECTION_URL", "123HXPHFOUND123")));
68+
regex = "123HXPHFOUND123";
69+
pattern = Pattern.compile(regex);
70+
matcher = pattern.matcher(r.getDescription());
71+
assertTrue(matcher.find());
72+
}
73+
4774
/**
4875
* Test getCurrentPageNumber method #1
4976
*/

0 commit comments

Comments
 (0)