Search in sources :

Example 1 with GetResource

use of org.openstreetmap.atlas.streaming.resource.http.GetResource in project atlas-checks by osmlab.

the class MapRouletteConnection method isAbleToConnectToMapRoulette.

private boolean isAbleToConnectToMapRoulette(final MapRouletteConfiguration configuration) {
    return new Retry(DEFAULT_CONNECTION_RETRIES, Duration.seconds(DEFAULT_CONNECTION_WAIT)).run(() -> {
        final String serverConnection = "http://" + configuration.getServer() + ":" + configuration.getPort();
        final GetResource homepage = new GetResource(serverConnection);
        final int statusCode = homepage.getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            logger.debug("Failed to connect to Map Roulette server [{}]. StatusCode: {}", serverConnection, statusCode);
            return false;
        } else {
            return true;
        }
    });
}
Also used : Retry(org.openstreetmap.atlas.utilities.runtime.Retry) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource)

Example 2 with GetResource

use of org.openstreetmap.atlas.streaming.resource.http.GetResource in project atlas-checks by osmlab.

the class MapRouletteConnection method create.

/**
 * Will create a challenge if it has not already been created
 *
 * @param getURI
 *            The URI used to retrieve the object
 * @param postURI
 *            The URI used to create the object
 * @param putURI
 *            The URI used to update the object
 * @param data
 *            The data of the object to create/update
 * @param logSuccessMessage
 *            The message to display on successful creation/update of object
 * @return The id of the created object
 * @throws UnsupportedEncodingException
 *             if cannot encode string for post/put to map roulette
 * @throws URISyntaxException
 *             if URI supplied is invalid and cannot be built
 */
public long create(final String getURI, final String postURI, final String putURI, final JsonObject data, final String logSuccessMessage) throws UnsupportedEncodingException, URISyntaxException {
    HttpResource createUpdate = null;
    final GetResource challengeGet = new GetResource(this.uriBuilder.build().resolve(getURI));
    this.setAuth(challengeGet);
    try {
        final int statusCode = challengeGet.getStatusCode();
        if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_NO_CONTENT) {
            final URIBuilder baseUrl = this.uriBuilder.setPath(postURI);
            // generate the Challenge through the API
            createUpdate = new PostResource(baseUrl.build().toString());
            ((PostResource) createUpdate).setStringBody(data.toString(), ContentType.APPLICATION_JSON);
        } else // just make sure it is up to date in this case
        {
            // get the ID directly from the response
            final long responseId = new Gson().fromJson(challengeGet.getRequestBodyAsString(), JsonObject.class).get(KEY_ID).getAsLong();
            final URIBuilder baseUrl = this.uriBuilder.setPath(String.format(putURI, responseId));
            createUpdate = new PutResource(baseUrl.build().toString());
            data.add(KEY_ID, new JsonPrimitive(responseId));
            ((PutResource) createUpdate).setStringBody(data.toString(), ContentType.APPLICATION_JSON);
        }
        this.setAuth(createUpdate);
        final int createUpdateStatus = createUpdate.getStatusCode();
        switch(createUpdateStatus) {
            case HttpStatus.SC_CREATED:
            case HttpStatus.SC_OK:
                final long responseID = new Gson().fromJson(createUpdate.getRequestBodyAsString(), JsonObject.class).get("id").getAsLong();
                logger.debug(logSuccessMessage, responseID);
                return responseID;
            default:
                logger.debug("{} - {}", createUpdate.getStatusCode(), createUpdate.getRequestBodyAsString());
                return -1;
        }
    } finally {
        challengeGet.close();
        if (createUpdate != null) {
            createUpdate.close();
        }
    }
}
Also used : HttpResource(org.openstreetmap.atlas.streaming.resource.http.HttpResource) JsonPrimitive(com.google.gson.JsonPrimitive) Gson(com.google.gson.Gson) GetResource(org.openstreetmap.atlas.streaming.resource.http.GetResource) PutResource(org.openstreetmap.atlas.streaming.resource.http.PutResource) PostResource(org.openstreetmap.atlas.streaming.resource.http.PostResource) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

GetResource (org.openstreetmap.atlas.streaming.resource.http.GetResource)2 Gson (com.google.gson.Gson)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 HttpResource (org.openstreetmap.atlas.streaming.resource.http.HttpResource)1 PostResource (org.openstreetmap.atlas.streaming.resource.http.PostResource)1 PutResource (org.openstreetmap.atlas.streaming.resource.http.PutResource)1 Retry (org.openstreetmap.atlas.utilities.runtime.Retry)1