use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.
the class SimpleHTTPClient method put.
public HTTPResult put(String url, String contentType, byte[] content, String accept) throws IOException {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setDoOutput(true);
c.setDoInput(true);
c.setRequestMethod("PUT");
c.setRequestProperty("Content-type", contentType);
if (accept != null) {
c.setRequestProperty("Accept", accept);
}
setHeaders(c);
c.getOutputStream().write(content);
c.getOutputStream().close();
return new HTTPResult(url, c.getResponseCode(), c.getResponseMessage(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getResponseCode() >= 400 ? c.getErrorStream() : c.getInputStream()));
}
use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.
the class SimpleHTTPClient method get.
public HTTPResult get(String url, String accept) throws IOException {
URL u = new URL(url);
// boolean isSSL = url.startsWith("https://");
// handling redirects - setInstanceFollowRedirects(true) doesn't handle crossing http to https
Map<String, Integer> visited = new HashMap<>();
HttpURLConnection c = null;
boolean done = false;
while (!done) {
int times = visited.compute(url, (key, count) -> count == null ? 1 : count + 1);
if (times > MAX_REDIRECTS)
throw new IOException("Stuck in redirect loop");
u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Accept", accept);
setHeaders(c);
c.setInstanceFollowRedirects(false);
if (trustAll && url.startsWith("https://")) {
((javax.net.ssl.HttpsURLConnection) c).setHostnameVerifier(SSLCertTruster.DO_NOT_VERIFY);
}
switch(c.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
String location = c.getHeaderField("Location");
location = URLDecoder.decode(location, "UTF-8");
URL base = new URL(url);
// Deal with relative URLs
URL next = new URL(base, location);
url = next.toExternalForm();
continue;
default:
done = true;
}
}
return new HTTPResult(url, c.getResponseCode(), c.getResponseMessage(), c.getRequestProperty("Content-Type"), TextFile.streamToBytes(c.getResponseCode() >= 400 ? c.getErrorStream() : c.getInputStream()));
}
use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.
the class ProfileComparer method cachedFetch.
private String cachedFetch(String id, String source) throws IOException {
String tmpDir = System.getProperty("java.io.tmpdir");
String local = Utilities.path(tmpDir, id);
File f = new File(local);
if (f.exists())
return TextFile.fileToString(f);
SimpleHTTPClient http = new SimpleHTTPClient();
HTTPResult res = http.get(source);
res.checkThrowException();
String result = TextFile.bytesToString(res.getContent());
TextFile.stringToFile(result, f);
return result;
}
use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.
the class TerminologyCacheManager method commit.
public void commit(String token) throws IOException {
// create a zip of all the files
ByteArrayOutputStream bs = new ByteArrayOutputStream();
zipDirectory(bs);
// post it to
String url = "https://tx.fhir.org/post/tx-cache/" + ghOrg + "/" + ghRepo + "/" + ghBranch + ".zip";
System.out.println("Sending tx-cache to " + url + " (" + Utilities.describeSize(bs.toByteArray().length) + ")");
SimpleHTTPClient http = new SimpleHTTPClient();
http.setUsername(token.substring(0, token.indexOf(':')));
http.setPassword(token.substring(token.indexOf(':') + 1));
// accept doesn't matter
HTTPResult res = http.put(url, "application/zip", bs.toByteArray(), null);
if (res.getCode() >= 300) {
System.out.println("sending cache failed: " + res.getCode());
} else {
System.out.println("Sent cache");
}
}
use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.
the class FilesystemPackageCacheManager method loadFromBuildServer.
private void loadFromBuildServer() throws IOException {
SimpleHTTPClient http = new SimpleHTTPClient();
http.trustAllhosts();
HTTPResult res = http.get("https://build.fhir.org/ig/qas.json?nocache=" + System.currentTimeMillis());
res.checkThrowException();
buildInfo = (JsonArray) new com.google.gson.JsonParser().parse(TextFile.bytesToString(res.getContent()));
List<BuildRecord> builds = new ArrayList<>();
for (JsonElement n : buildInfo) {
JsonObject o = (JsonObject) n;
if (o.has("url") && o.has("package-id") && o.get("package-id").getAsString().contains(".")) {
String u = o.get("url").getAsString();
if (u.contains("/ImplementationGuide/"))
u = u.substring(0, u.indexOf("/ImplementationGuide/"));
builds.add(new BuildRecord(u, o.get("package-id").getAsString(), getRepo(o.get("repo").getAsString()), readDate(o.get("date").getAsString())));
}
}
Collections.sort(builds, new BuildRecordSorter());
for (BuildRecord bld : builds) {
if (!ciList.containsKey(bld.getPackageId())) {
ciList.put(bld.getPackageId(), "https://build.fhir.org/ig/" + bld.getRepo());
}
}
// whether it succeeds or not
buildLoaded = true;
}
Aggregations