Skip to content
Open
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
c422241
add jacoco plugin
YBushi Jun 8, 2024
9aa0841
Create .gitattributes
YBushi Jun 8, 2024
759e955
Normalize all line endings to LF
YBushi Jun 8, 2024
56f0a96
Normalize all line endings to LF
YBushi Jun 8, 2024
f389b08
Revert "Normalize all line endings to LF"
YBushi Jun 8, 2024
e84040a
Revert "Normalize all line endings to LF"
YBushi Jun 8, 2024
49d6ffe
.picked functions to cover
YBushi Jun 9, 2024
ac025a4
added readme which we should use for the report of our assignment
YBushi Jun 9, 2024
22ff531
Update README.md
YBushi Jun 9, 2024
30ddaf0
.added my 2 methods that I covered into the report
YBushi Jun 9, 2024
8385a7b
.disabled checkstyles so its easier to code without having to worry a…
YBushi Jun 9, 2024
99b6a14
Update README.md - Jayran 2 functions
jayran-d Jun 9, 2024
d865301
Update README.md
nikola20145 Jun 9, 2024
0983313
.disable programming mistake detector
YBushi Jun 10, 2024
c97456e
.added before/after coverage
YBushi Jun 10, 2024
8fa3da5
Update README.md
nikola20145 Jun 10, 2024
0976235
Update README.md
nikola20145 Jun 10, 2024
3884fad
Update README.md
nikola20145 Jun 10, 2024
f959b03
Update README.md
nikola20145 Jun 10, 2024
322bdc7
Update README.md
nikola20145 Jun 10, 2024
95bd2b6
Update README.md
nikola20145 Jun 10, 2024
e4efa5c
Update NingHttpClient.java
nikola20145 Jun 10, 2024
3015c50
Update pom.xml
nikola20145 Jun 10, 2024
31761ff
Update settings.json
nikola20145 Jun 10, 2024
de3bbe5
Update ArmeriaHttpClient.java
nikola20145 Jun 10, 2024
b7de5fe
Last updates for Assignment 1
nikola20145 Jun 18, 2024
d040a99
Changing Armeria file to the same as it is in master
nikola20145 Jun 20, 2024
292d26f
Update NingHttpClient.java
nikola20145 Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update NingHttpClient.java
  • Loading branch information
nikola20145 committed Jun 10, 2024
commit e4efa5cc6438362d6f084efd80029e01971c3900
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,37 @@
import com.github.scribejava.core.model.Verb;
import com.ning.http.client.AsyncHttpClient;


import java.util.Map;
import java.util.concurrent.Future;

import com.ning.http.client.AsyncHttpClientConfig;
import java.io.File;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

public class NingHttpClient extends AbstractAsyncOnlyHttpClient {

private final AsyncHttpClient client;
private static final ConcurrentHashMap<String, AtomicBoolean> branchCoverage = new ConcurrentHashMap<>();
// data structure for info about the branches
static {
branchCoverage.put("branch_1", new AtomicBoolean(false)); // GET
branchCoverage.put("branch_2", new AtomicBoolean(false)); // POST
branchCoverage.put("branch_3", new AtomicBoolean(false)); // PUT
branchCoverage.put("branch_4", new AtomicBoolean(false)); // DELETE
branchCoverage.put("branch_5", new AtomicBoolean(false)); // DEFAULT

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Branch coverage results:");
for (Map.Entry<String, AtomicBoolean> entry : branchCoverage.entrySet()) {
System.out.println(entry.getKey() + ": " + (entry.getValue().get() ? "Taken" : "Not taken"));
}
}
}));
}

public NingHttpClient() {
this(NingHttpClientConfig.defaultConfig());
Expand Down Expand Up @@ -79,45 +101,59 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers,
converter);
}

// branch coverage: Nikola
private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb,
String completeUrl, BodySetter bodySetter, Object bodyContents, OAuthAsyncRequestCallback<T> callback,
OAuthRequest.ResponseConverter<T> converter) {
final AsyncHttpClient.BoundRequestBuilder boundRequestBuilder;
switch (httpVerb) {
case GET:
boundRequestBuilder = client.prepareGet(completeUrl);
break;
case POST:
boundRequestBuilder = client.preparePost(completeUrl);
break;
case PUT:
boundRequestBuilder = client.preparePut(completeUrl);
break;
case DELETE:
boundRequestBuilder = client.prepareDelete(completeUrl);
break;
default:
throw new IllegalArgumentException("message build error: unknown verb type");
}

if (httpVerb.isPermitBody()) {
if (!headers.containsKey(CONTENT_TYPE)) {
boundRequestBuilder.addHeader(CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
}
bodySetter.setBody(boundRequestBuilder, bodyContents);
}
String completeUrl, BodySetter bodySetter, Object bodyContents, OAuthAsyncRequestCallback<T> callback,
OAuthRequest.ResponseConverter<T> converter) {
final AsyncHttpClient.BoundRequestBuilder boundRequestBuilder;
switch (httpVerb) {
// ID: branch_1
case GET:
branchCoverage.get("branch_1").set(true); // Update branch coverage for GET
boundRequestBuilder = client.prepareGet(completeUrl);
break;
// ID: branch_2
case POST:
branchCoverage.get("branch_2").set(true); // Update branch coverage for POST
boundRequestBuilder = client.preparePost(completeUrl);
break;
// ID: branch_3
case PUT:
branchCoverage.get("branch_3").set(true); // Update branch coverage for PUT
boundRequestBuilder = client.preparePut(completeUrl);
break;
// ID: branch_4
case DELETE:
branchCoverage.get("branch_4").set(true); // Update branch coverage for DELETE
boundRequestBuilder = client.prepareDelete(completeUrl);
break;
// ID: branch_5
default:
branchCoverage.get("branch_5").set(true); // Update branch coverage for DEFAULT case
throw new IllegalArgumentException("message build error: unknown verb type");
}

for (Map.Entry<String, String> header : headers.entrySet()) {
boundRequestBuilder.addHeader(header.getKey(), header.getValue());
if (httpVerb.isPermitBody()) {
// Check if the Content-Type header is already present
if (!headers.containsKey("Content-Type")) {
// Set the Content-Type header if not present
boundRequestBuilder.addHeader("Content-Type", "application/x-www-form-urlencoded");
}
bodySetter.setBody(boundRequestBuilder, bodyContents);
}

if (userAgent != null) {
boundRequestBuilder.setHeader(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}
for (Map.Entry<String, String> header : headers.entrySet()) {
boundRequestBuilder.addHeader(header.getKey(), header.getValue());
}

return boundRequestBuilder.execute(new OAuthAsyncCompletionHandler<>(callback, converter));
if (userAgent != null) {
boundRequestBuilder.setHeader(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
}

return boundRequestBuilder.execute(new OAuthAsyncCompletionHandler<>(callback, converter));
}


private enum BodySetter {
BYTE_ARRAY {
@Override
Expand Down