Search in sources :

Example 21 with HTTPResult

use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.

the class FilesystemPackageCacheManager method fetchFromUrlSpecific.

private InputStream fetchFromUrlSpecific(String source, boolean optional) throws FHIRException {
    try {
        SimpleHTTPClient http = new SimpleHTTPClient();
        HTTPResult res = http.get(source);
        res.checkThrowException();
        return new ByteArrayInputStream(res.getContent());
    } catch (Exception e) {
        if (optional)
            return null;
        else
            throw new FHIRException("Unable to fetch: " + e.getMessage(), e);
    }
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) SimpleHTTPClient(org.hl7.fhir.utilities.SimpleHTTPClient) ByteArrayInputStream(java.io.ByteArrayInputStream) FHIRException(org.hl7.fhir.exceptions.FHIRException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 22 with HTTPResult

use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.

the class SimpleHTTPClient method post.

public HTTPResult post(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("POST");
    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 23 with HTTPResult

use of org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult in project org.hl7.fhir.core by hapifhir.

the class IgLoader method fetchFromUrlSpecific.

private byte[] fetchFromUrlSpecific(String source, String contentType, boolean optional, List<String> errors) throws FHIRException, IOException {
    try {
        SimpleHTTPClient http = new SimpleHTTPClient();
        try {
            // try with cache-busting option and then try withhout in case the server doesn't support that
            HTTPResult res = http.get(source + "?nocache=" + System.currentTimeMillis(), contentType);
            res.checkThrowException();
            return res.getContent();
        } catch (Exception e) {
            HTTPResult res = http.get(source, contentType);
            res.checkThrowException();
            return res.getContent();
        }
    } catch (IOException e) {
        if (errors != null) {
            errors.add("Error accessing " + source + ": " + e.getMessage());
        }
        if (optional)
            return null;
        else
            throw e;
    }
}
Also used : HTTPResult(org.hl7.fhir.utilities.SimpleHTTPClient.HTTPResult) SimpleHTTPClient(org.hl7.fhir.utilities.SimpleHTTPClient) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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