Search in sources :

Example 1 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class HTTPConnector method httpPost.

// public HTTPResponse postAsMultipart(String url, File file)
// {
// try {
// if (!isInitialized())
// {
// initialize();
// }
// 
// HttpPost post = new HttpPost(this.getServerUrl() + url);
// 
// post.setRequestHeader("Content-Type", "multipart/form-data");
// 
// FilePart filePart;
// 
// filePart = new FilePart("form_def_file", file, "application/xml", "UTF-8");
// 
// 
// Part[] parts = { filePart };
// MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, post.getParams());
// 
// post.setRequestEntity(multipartRequestEntity);
// 
// HTTPResponse response = this.httpRequest(post);
// 
// if (response.getStatusCode() == 401)
// {
// throw new InvalidLoginException("Unable to log in to " + this.getServerUrl());
// }
// 
// return response;
// } catch (FileNotFoundException e) {
// throw new RuntimeException(e);
// }
// }
public DHIS2Response httpPost(String url, List<NameValuePair> params, HttpEntity body) throws InvalidLoginException, HTTPException, BadServerUriException {
    try {
        if (!isInitialized()) {
            initialize();
        }
        HttpPost post = new HttpPost(this.buildUri(url, params));
        post.addHeader("Content-Type", "application/json");
        post.setEntity(body);
        try (CloseableHttpResponse response = client.execute(post, this.getContext())) {
            return this.convertResponse(response);
        }
    } catch (IOException | URISyntaxException e) {
        throw new HTTPException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HTTPException(net.geoprism.dhis2.dhis2adapter.exception.HTTPException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException)

Example 2 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class HTTPConnector method httpDelete.

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

Example 3 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class HTTPConnector method httpPut.

public DHIS2Response httpPut(String url, List<NameValuePair> params, HttpEntity body) throws InvalidLoginException, HTTPException, BadServerUriException {
    try {
        if (!isInitialized()) {
            initialize();
        }
        HttpPut post = new HttpPut(this.buildUri(url, params));
        post.addHeader("Content-Type", "application/json");
        post.setEntity(body);
        try (CloseableHttpResponse response = client.execute(post, 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) HttpPut(org.apache.http.client.methods.HttpPut)

Example 4 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response in project geoprism-registry by terraframe.

the class FakeIdConnector method httpGetSubclass.

@Override
public DHIS2Response httpGetSubclass(String string, List<NameValuePair> params) throws InvalidLoginException, HTTPException {
    Integer limit = null;
    for (int i = 0; i < params.size(); ++i) {
        NameValuePair param = params.get(i);
        if (param.getName().equals("limit")) {
            limit = Integer.valueOf(param.getValue());
        }
    }
    JsonArray codes = new JsonArray();
    for (int i = 0; i < limit; ++i) {
        codes.add(UUID.randomUUID().toString().substring(0, 11));
    }
    JsonObject resp = new JsonObject();
    resp.add("codes", codes);
    return new DHIS2Response(resp.toString(), 200);
}
Also used : JsonArray(com.google.gson.JsonArray) NameValuePair(org.apache.http.NameValuePair) JsonObject(com.google.gson.JsonObject) DHIS2Response(net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)

Example 5 with DHIS2Response

use of net.geoprism.dhis2.dhis2adapter.response.DHIS2Response 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)

Aggregations

HTTPException (net.geoprism.dhis2.dhis2adapter.exception.HTTPException)7 DHIS2Response (net.geoprism.dhis2.dhis2adapter.response.DHIS2Response)6 JsonObject (com.google.gson.JsonObject)5 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)5 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 NameValuePair (org.apache.http.NameValuePair)4 JsonArray (com.google.gson.JsonArray)3 ArrayList (java.util.ArrayList)3 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)3 UnexpectedResponseException (net.geoprism.dhis2.dhis2adapter.exception.UnexpectedResponseException)2 Test (org.junit.Test)2 GsonBuilder (com.google.gson.GsonBuilder)1 Transaction (com.runwaysdk.dataaccess.transaction.Transaction)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 InvalidLoginException (net.geoprism.dhis2.dhis2adapter.exception.InvalidLoginException)1 DHIS2ImportResponse (net.geoprism.dhis2.dhis2adapter.response.DHIS2ImportResponse)1 ImportStrategy (net.geoprism.dhis2.dhis2adapter.response.model.ImportStrategy)1 OrganisationUnit (net.geoprism.dhis2.dhis2adapter.response.model.OrganisationUnit)1