Search in sources :

Example 6 with HTTPResult

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()));
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL)

Example 7 with HTTPResult

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()));
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) IOException(java.io.IOException) URL(java.net.URL)

Example 8 with HTTPResult

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;
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) SimpleHTTPClient(org.hl7.fhir.utilities.SimpleHTTPClient) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 9 with HTTPResult

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");
    }
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) SimpleHTTPClient(org.hl7.fhir.utilities.SimpleHTTPClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with HTTPResult

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;
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) SimpleHTTPClient(org.hl7.fhir.utilities.SimpleHTTPClient) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Aggregations

HTTPResult (org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult)23 SimpleHTTPClient (org.hl7.fhir.utilities.SimpleHTTPClient)17 ByteArrayInputStream (java.io.ByteArrayInputStream)6 IOException (java.io.IOException)6 FHIRException (org.hl7.fhir.exceptions.FHIRException)4 HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TextFile (org.hl7.fhir.utilities.TextFile)1