Skip to content

Commit

Permalink
When sending multiple points, do it in a single job
Browse files Browse the repository at this point in the history
Issue #1073
  • Loading branch information
mendhak committed Mar 2, 2024
1 parent 77bd263 commit f9fbc98
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.slf4j.Logger;

import java.io.File;
import java.util.Map;

import javax.net.ssl.X509TrustManager;
Expand All @@ -49,13 +50,23 @@ public class CustomUrlJob extends Job {
private static final Logger LOG = Logs.of(CustomUrlJob.class);

private UploadEvents.BaseUploadEvent callbackEvent;
private CustomUrlRequest urlRequest;

private File csvFile;
private CustomUrlRequest[] urlRequests = new CustomUrlRequest[1];

public CustomUrlJob(CustomUrlRequest urlRequest, UploadEvents.BaseUploadEvent callbackEvent) {
super(new Params(1).requireNetwork().persist());

this.callbackEvent = callbackEvent;
this.urlRequest = urlRequest;
this.urlRequests[0] = urlRequest;
}

public CustomUrlJob(CustomUrlRequest[] urlRequests, File csvFile, UploadEvents.BaseUploadEvent callbackEvent) {
super(new Params(1).requireNetwork().persist());

this.callbackEvent = callbackEvent;
this.urlRequests = urlRequests;
this.csvFile = csvFile;
}


Expand All @@ -66,35 +77,54 @@ public void onAdded() {
@Override
public void onRun() throws Throwable {

LOG.info("HTTP Request - " + urlRequest.getLogURL());
boolean success = true;
Response errorResponse = null;

OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.sslSocketFactory(Networks.getSocketFactory(AppSettings.getInstance()),
(X509TrustManager) Networks.getTrustManager(AppSettings.getInstance()));
Request.Builder requestBuilder = new Request.Builder().url(urlRequest.getLogURL());
if(urlRequests != null && urlRequests.length > 0){

for(Map.Entry<String,String> header : urlRequest.getHttpHeaders().entrySet()){
requestBuilder.addHeader(header.getKey(), header.getValue());
}
for (CustomUrlRequest urlRequest : urlRequests) {
LOG.info("HTTP Request - " + urlRequest.getLogURL());

if ( ! urlRequest.getHttpMethod().equalsIgnoreCase("GET")) {
RequestBody body = RequestBody.create(null, urlRequest.getHttpBody());
requestBuilder = requestBuilder.method(urlRequest.getHttpMethod(),body);
}
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.sslSocketFactory(Networks.getSocketFactory(AppSettings.getInstance()),
(X509TrustManager) Networks.getTrustManager(AppSettings.getInstance()));
Request.Builder requestBuilder = new Request.Builder().url(urlRequest.getLogURL());

Request request = requestBuilder.build();
Response response = okBuilder.build().newCall(request).execute();
for(Map.Entry<String,String> header : urlRequest.getHttpHeaders().entrySet()){
requestBuilder.addHeader(header.getKey(), header.getValue());
}

if (response.isSuccessful()) {
LOG.debug("HTTP request complete with successful response code " + response);
if ( ! urlRequest.getHttpMethod().equalsIgnoreCase("GET")) {
RequestBody body = RequestBody.create(null, urlRequest.getHttpBody());
requestBuilder = requestBuilder.method(urlRequest.getHttpMethod(),body);
}

Request request = requestBuilder.build();
Response response = okBuilder.build().newCall(request).execute();

if (response.isSuccessful()) {
LOG.debug("HTTP request complete with successful response code " + response);
}
else {
LOG.error("HTTP request complete with unexpected response code " + response );
errorResponse = response;
success = false;
}

response.body().close();

if(!success){
break;
}
}
}

if(success){
EventBus.getDefault().post(callbackEvent.succeeded());
}
else {
LOG.error("HTTP request complete with unexpected response code " + response );
EventBus.getDefault().post(callbackEvent.failed("Unexpected code " + response,new Throwable(response.body().string())));
EventBus.getDefault().post(callbackEvent.failed("Unexpected code " + errorResponse,new Throwable(errorResponse.body().string())));
}

response.body().close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void uploadFile(List<File> files) {
foundFileToSend = true;
List<SerializableLocation> locations = getLocationsFromCSV(f);
LOG.debug(locations.size() + " points were read from " + f.getName());
sendLocations(locations.toArray(new SerializableLocation[locations.size()]));
sendLocations(locations.toArray(new SerializableLocation[locations.size()]), f);
}
}

Expand Down Expand Up @@ -144,29 +144,35 @@ private String unApplyDecimalComma(String recordValue) {
return recordValue.replace(",",".");
}

private void sendLocations(SerializableLocation[] locations){
private void sendLocations(SerializableLocation[] locations, File csvFile){
if(locations.length > 0){

ArrayList<CustomUrlRequest> requests = new ArrayList<>();
String customLoggingUrl = preferenceHelper.getCustomLoggingUrl();
String httpBody = preferenceHelper.getCustomLoggingHTTPBody();
String httpHeaders = preferenceHelper.getCustomLoggingHTTPHeaders();
String httpMethod = preferenceHelper.getCustomLoggingHTTPMethod();

for(SerializableLocation loc: locations){

try {
String finalUrl = getFormattedTextblock(customLoggingUrl, loc);
String finalBody = getFormattedTextblock(httpBody, loc);
String finalHeaders = getFormattedTextblock(httpHeaders, loc);

sendByHttp(finalUrl, httpMethod, finalBody, finalHeaders,
preferenceHelper.getCustomLoggingBasicAuthUsername(),
preferenceHelper.getCustomLoggingBasicAuthPassword());

requests.add(new CustomUrlRequest(finalUrl, httpMethod,
finalBody, finalHeaders, preferenceHelper.getCustomLoggingBasicAuthUsername(),
preferenceHelper.getCustomLoggingBasicAuthPassword()));
} catch (Exception e) {
LOG.error("Could not build the Custom URL to send", e);
}
}

JobManager jobManager = AppSettings.getJobManager();
jobManager.addJobInBackground(
new CustomUrlJob(
requests.toArray(new CustomUrlRequest[requests.size()]),
csvFile,
new UploadEvents.CustomUrl()));
}

}
Expand Down

0 comments on commit f9fbc98

Please sign in to comment.