Search in sources :

Example 1 with BadServerUriException

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

the class DHIS2Bridge method metadataGet.

public <T> MetadataGetResponse<T> metadataGet(Class<?> dhis2Type, List<NameValuePair> params) throws InvalidLoginException, HTTPException, BadServerUriException {
    String objectNamePlural = DHIS2Objects.getPluralObjectNameFromClass(dhis2Type);
    if (params == null) {
        params = new ArrayList<NameValuePair>();
    }
    boolean hasObjectName = false;
    for (NameValuePair param : params) {
        if (param.getName().equals(objectNamePlural)) {
            hasObjectName = true;
        }
    }
    if (!hasObjectName) {
        params.add(new BasicNameValuePair(objectNamePlural, "true"));
    }
    return new MetadataGetResponse<T>(this.apiGet("metadata", params), objectNamePlural, dhis2Type);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) MetadataGetResponse(net.geoprism.dhis2.dhis2adapter.response.MetadataGetResponse)

Example 2 with BadServerUriException

use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException 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 3 with BadServerUriException

use of net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException 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 4 with BadServerUriException

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

the class HTTPConnector method getContext.

private HttpClientContext getContext() throws BadServerUriException {
    try {
        URIBuilder uri = new URIBuilder(this.getServerUrl());
        final HttpHost targetHost;
        try {
            targetHost = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        } catch (IllegalArgumentException e) {
            throw new BadServerUriException(e, this.getServerUrl());
        }
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(username, password));
        // This authcache code is used to enable "Preemptive authentication".
        // http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/authentication.html#d5e613
        // TODO : We shouldn't need to be using preemtive authentication (because it's a potential
        // security risk), however I'm not sure how else to get this working.
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(credsProvider);
        context.setAuthCache(authCache);
        return context;
    } catch (URISyntaxException e) {
        throw new BadServerUriException(e, this.getServerUrl());
    }
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) URISyntaxException(java.net.URISyntaxException) BadServerUriException(net.geoprism.dhis2.dhis2adapter.exception.BadServerUriException) URIBuilder(org.apache.http.client.utils.URIBuilder) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope)

Example 5 with BadServerUriException

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

Aggregations

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