Skip to content

Commit

Permalink
Merge pull request #12 from slacmshankar/master
Browse files Browse the repository at this point in the history
Use HTTPClient that's included in the JDK
  • Loading branch information
jacomago authored Apr 25, 2024
2 parents 3485539 + a911c1f commit e7adcce
Show file tree
Hide file tree
Showing 4 changed files with 32 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.2.0'

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 @@ -7,17 +7,21 @@
*******************************************************************************/
package org.epics.archiverappliance.retrieval.client;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.hc.client5.http.fluent.Request;

/**
* Client side class for retrieving data from the appliance archiver using the PB over HTTP protocol.
Expand All @@ -27,9 +31,12 @@
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 @@ -71,12 +78,19 @@ public final GenMsgIterator getDataForPVs(
String getURL = buf.toString();
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;
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;
}
InputStream is = response.body();
if (is.available() <= 0) {
logger.warning("Empty response from server when fetching data from " + getURL);
return null;
}
return new InputStreamBackedGenMsg(is);

} catch (Exception 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 e7adcce

Please sign in to comment.