Search in sources :

Example 6 with HTTPException

use of net.geoprism.dhis2.dhis2adapter.exception.HTTPException in project geoprism-registry by terraframe.

the class DHIS2SynchronizationManager method init.

// https://play.dhis2.org/2.35.1/api/metadata.xml?organisationUnits=true&filter=level:eq:1
private void init() {
    if (this.ouLevel1 != null) {
        return;
    }
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("filter", "level:eq:1"));
    try {
        MetadataGetResponse<OrganisationUnit> resp = this.dhis2.metadataGet(OrganisationUnit.class, params);
        this.service.validateDhis2Response(resp);
        List<OrganisationUnit> orgUnits = resp.getObjects();
        this.ouLevel1 = orgUnits;
    } catch (InvalidLoginException e) {
        LoginException cgrlogin = new LoginException(e);
        throw cgrlogin;
    } catch (HTTPException | BadServerUriException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) OrganisationUnit(net.geoprism.dhis2.dhis2adapter.response.model.OrganisationUnit) HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) ArrayList(java.util.ArrayList) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) LoginException(net.geoprism.registry.etl.export.LoginException) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) HttpError(net.geoprism.registry.etl.export.HttpError)

Example 7 with HTTPException

use of net.geoprism.dhis2.dhis2adapter.exception.HTTPException in project geoprism-registry by terraframe.

the class DHIS2OptionCache method init.

private void init() {
    try {
        MetadataGetResponse<OptionSet> resp = dhis2.<OptionSet>metadataGet(OptionSet.class);
        if (!resp.isSuccess()) {
            // if (resp.hasMessage())
            // {
            // ExportRemoteException ere = new ExportRemoteException();
            // ere.setRemoteError(resp.getMessage());
            // throw ere;
            // }
            // else
            // {
            UnexpectedRemoteResponse re = new UnexpectedRemoteResponse();
            throw re;
        // }
        }
        List<OptionSet> objects = resp.getObjects();
        this.optionSets = new HashMap<String, IntegratedOptionSet>(objects.size());
        for (OptionSet os : objects) {
            this.optionSets.put(os.getId(), new IntegratedOptionSet(os));
        }
        if (this.optionSets.size() > 0) {
            MetadataGetResponse<Option> resp2 = dhis2.<Option>metadataGet(Option.class);
            if (!resp2.isSuccess()) {
                // if (resp.hasMessage())
                // {
                // ExportRemoteException ere = new ExportRemoteException();
                // ere.setRemoteError(resp.getMessage());
                // throw ere;
                // }
                // else
                // {
                UnexpectedRemoteResponse re = new UnexpectedRemoteResponse();
                throw re;
            // }
            }
            List<Option> options = resp2.getObjects();
            for (Option option : options) {
                if (option.getOptionSetId() != null && this.optionSets.containsKey(option.getOptionSetId())) {
                    this.optionSets.get(option.getOptionSetId()).addOption(option);
                }
            }
        }
    } catch (InvalidLoginException e) {
        LoginException cgrlogin = new LoginException(e);
        throw cgrlogin;
    } catch (HTTPException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    } catch (BadServerUriException e) {
        HttpError cgrhttp = new HttpError(e);
        throw cgrhttp;
    }
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) UnexpectedRemoteResponse(net.geoprism.registry.etl.export.UnexpectedRemoteResponse) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) LoginException(net.geoprism.registry.etl.export.LoginException) InvalidLoginException(net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException) Option(net.geoprism.dhis2.dhis2adapter.response.model.Option) HttpError(net.geoprism.registry.etl.export.HttpError) OptionSet(net.geoprism.dhis2.dhis2adapter.response.model.OptionSet)

Example 8 with HTTPException

use of net.geoprism.dhis2.dhis2adapter.exception.HTTPException in project geoprism-registry by terraframe.

the class DHIS2Bridge method fetchVersionRemoteServer.

private void fetchVersionRemoteServer() throws UnexpectedResponseException, InvalidLoginException, HTTPException, BadServerUriException {
    DHIS2Response resp = this.systemInfo();
    if (!resp.isSuccess()) {
        throw new UnexpectedResponseException(resp);
    }
    Matcher m = null;
    try {
        JsonObject jo = resp.getJsonObject();
        this.versionRemoteServer = jo.get("version").getAsString();
        Pattern p = Pattern.compile("\\d+\\.(\\d+)\\.\\d+(-.+)?");
        m = p.matcher(this.versionRemoteServer);
    } catch (Throwable t) {
        throw new UnexpectedResponseException(t, resp);
    }
    if (m.matches()) {
        this.versionRemoteServerApi = Integer.parseInt(m.group(1));
    } else {
        throw new UnexpectedResponseException(resp);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) JsonObject(com.google.gson.JsonObject) UnexpectedResponseException(net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)

Example 9 with HTTPException

use of net.geoprism.dhis2.dhis2adapter.exception.HTTPException in project geoprism-registry by terraframe.

the class Dhis2IdCache method fetchIds.

/**
 * Fetches more ids from DHIS2 and adds them to our internal cache.
 *
 * @throws HTTPException
 * @throws InvalidLoginException
 * @throws UnexpectedResponseException
 * @throws BadServerUriException
 */
public DHIS2Response fetchIds() throws HTTPException, InvalidLoginException, UnexpectedResponseException, BadServerUriException {
    List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    nvp.add(new BasicNameValuePair("limit", String.valueOf(FETCH_NUM)));
    DHIS2Response response = dhis2.apiGet("system/id.json", nvp);
    if (response.getStatusCode() != 200) {
        throw new HTTPException("Unable to get new ids from DHIS2. " + response.getResponse());
    }
    JsonObject json = response.getJsonObject();
    if (json.has("codes")) {
        JsonArray codes = json.get("codes").getAsJsonArray();
        for (int i = 0; i < codes.size(); ++i) {
            String id = codes.get(i).getAsString();
            cache.push(id);
        }
    } else {
        throw new UnexpectedResponseException(response);
    }
    return response;
}
Also used : JsonArray(com.google.gson.JsonArray) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) UnexpectedResponseException(net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)

Example 10 with HTTPException

use of net.geoprism.dhis2.dhis2adapter.exception.HTTPException in project geoprism-registry by terraframe.

the class HTTPConnector method httpPatch.

public DHIS2Response httpPatch(String url, List<NameValuePair> params, HttpEntity body) throws InvalidLoginException, HTTPException, BadServerUriException {
    try {
        if (!isInitialized()) {
            initialize();
        }
        HttpPatch patch = new HttpPatch(this.buildUri(url, params));
        patch.addHeader("Content-Type", "application/json");
        patch.setEntity(body);
        try (CloseableHttpResponse response = client.execute(patch, this.getContext())) {
            return this.convertResponse(response);
        }
    } catch (IOException | URISyntaxException e) {
        throw new HTTPException(e);
    }
}
Also used : HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) HttpPatch(org.apache.http.client.methods.HttpPatch)

Aggregations

HTTPException (net.geoprism.dhis2.dhis2adapter.exception.HTTPException)13 JsonObject (com.google.gson.JsonObject)8 JsonArray (com.google.gson.JsonArray)7 InvalidLoginException (net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException)7 HttpError (net.geoprism.registry.etl.export.HttpError)7 LoginException (net.geoprism.registry.etl.export.LoginException)7 IOException (java.io.IOException)6 BadServerUriException (net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException)6 NameValuePair (org.apache.http.NameValuePair)6 URISyntaxException (java.net.URISyntaxException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)5 ArrayList (java.util.ArrayList)4 UnexpectedResponseException (net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException)4 DHIS2Response (net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)4 ProgrammingErrorException (com.runwaysdk.dataaccess.ProgrammingErrorException)3 GsonBuilder (com.google.gson.GsonBuilder)2 Request (com.runwaysdk.session.Request)2 MetadataGetResponse (net.geoprism.dhis2.dhis2adapter.response.MetadataGetResponse)2 Attribute (net.geoprism.dhis2.dhis2adapter.response.model.Attribute)2