Skip to content

Commit

Permalink
Use HTTPClient that's included in the JDK
Browse files Browse the repository at this point in the history
  • Loading branch information
slacmshankar committed Apr 16, 2024
1 parent 3485539 commit b55092e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
# Ignore Gradle project-specific cache directory
.gradle


# Ignore Gradle build output directory
build
lib/build
.idea
lib/bin
.idea

.DS_Store
10 changes: 3 additions & 7 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
id("maven-publish")
}

version = '0.1.1'
version = '0.0.11'

java {
toolchain {
Expand Down Expand Up @@ -48,12 +48,7 @@ publishing {

dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
api 'com.google.protobuf:protobuf-java:3.23.0'
api 'org.apache.httpcomponents.client5:httpclient5-fluent:5.2.1'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:31.1-jre'
}

protobuf {
Expand Down Expand Up @@ -102,10 +97,11 @@ spotless {
}


tasks.named('jar') {
jar {
manifest {
attributes('Implementation-Title': project.name,
'Implementation-Version': project.version)
}
archiveBaseName = 'pbrawclient'
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
package org.epics.archiverappliance.retrieval.client;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand All @@ -17,7 +19,11 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hc.client5.http.fluent.Request;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpResponse.BodyHandlers;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
* Client side class for retrieving data from the appliance archiver using the PB over HTTP protocol.
Expand All @@ -27,9 +33,13 @@
public class RawDataRetrieval extends DataRetrieval {
private static final Logger logger = Logger.getLogger(RawDataRetrieval.class.getName());
private final String accessURL;
private HttpClient theClient;

public RawDataRetrieval(String accessURL) {
this.accessURL = accessURL;
this.theClient = HttpClient.newBuilder()
.followRedirects(Redirect.NORMAL)
.build();
}

private static String convertToUTC(Timestamp time) {
Expand Down Expand Up @@ -69,15 +79,15 @@ public final GenMsgIterator getDataForPVs(
});
}
String getURL = buf.toString();
logger.info("URL to fetch data is " + getURL);
logger.info("URL to fetch data is " + getURL);
try {
URL url = new URL(getURL);
var data = Request.get(url.toURI()).execute().returnContent();

BufferedInputStream is = new BufferedInputStream(data.asStream());

if (is.available() <= 0) return null;
return new InputStreamBackedGenMsg(is);
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(getURL)).build();
HttpResponse<InputStream> response = this.theClient.send(request, BodyHandlers.ofInputStream());
if(response.statusCode() != 200) {
logger.warning("Invalid status code from server " + response.statusCode() + " when fetching data from " + getURL);
return null;
}
return new InputStreamBackedGenMsg(response.body());

} catch (Exception ex) {
logger.log(Level.SEVERE, "Exception fetching data from URL " + getURL, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CheckArchApplPing {
* @param args
*/
public static void main(String[] args) throws Exception {
String serverURL = "http://cdlx27.slac.stanford.edu:17665/retrieval";
String serverURL = "http://localhost:17665/retrieval";
if (args.length > 1) {
serverURL = args[0];
}
Expand Down

0 comments on commit b55092e

Please sign in to comment.