Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTTPClient that's included in the JDK #12

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
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'
jacomago marked this conversation as resolved.
Show resolved Hide resolved

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,13 +78,15 @@ 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;
jacomago marked this conversation as resolved.
Show resolved Hide resolved
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
Loading