use of org.openstreetmap.atlas.streaming.resource.http.HttpResource 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();
}
}
}
use of org.openstreetmap.atlas.streaming.resource.http.HttpResource in project atlas-checks by osmlab.
the class MapRouletteConnection method uploadTask.
/**
* Will upload a batch of tasks (or a single task) to a map roulette server
*
* @param parentChallengeId
* The ID of the challenge that is the parent of the list of tasks
* @param tasks
* The list of tasks to upload
* @param post
* Our modified version of maproulette changes the actions of POST and PUT on the
* batch upload process. Basically if you POST a batch, it will create any new tasks
* and ignore already created tasks, for a PUT it will create new tasks and update
* already created new tasks
* @return the JSON payload used to create the tasks
* @throws UnsupportedEncodingException
* @throws URISyntaxException
* if the URI is build incorrectly
*/
private boolean uploadTask(final long parentChallengeId, final List<Task> tasks, final boolean post) throws UnsupportedEncodingException, URISyntaxException {
if (tasks.isEmpty()) {
logger.debug("No tasks supplied in list to upload");
return false;
}
boolean uploaded = false;
final JsonArray taskArray = new JsonArray();
tasks.forEach(element -> taskArray.add(element.generateTask(parentChallengeId)));
final HttpResource taskCreateUpdate;
final URIBuilder builder = this.uriBuilder.setPath("/api/v2/tasks");
if (post) {
taskCreateUpdate = new PostResource(builder.build().toString());
((PostResource) taskCreateUpdate).setStringBody(taskArray.toString(), ContentType.APPLICATION_JSON);
} else {
taskCreateUpdate = new PutResource(builder.build().toString());
((PutResource) taskCreateUpdate).setStringBody(taskArray.toString(), ContentType.APPLICATION_JSON);
}
this.setAuth(taskCreateUpdate);
final int createStatus = taskCreateUpdate.getStatusCode();
switch(createStatus) {
case HttpStatus.SC_CREATED:
logger.debug("Created {} task(s) for challenge {}", tasks.size(), tasks.get(0).getChallengeName());
uploaded = true;
break;
case HttpStatus.SC_OK:
logger.debug("Updated {} task(s) for challenge {}", tasks.size(), tasks.get(0).getChallengeName());
uploaded = true;
break;
default:
logger.debug("{} - {}", taskCreateUpdate.getStatusCode(), taskCreateUpdate.getRequestBodyAsString());
}
return uploaded;
}
Aggregations