use of org.locationtech.geogig.rest.RestletException in project GeoGig by boundlessgeo.
the class SendObjectResource method post.
@Override
public void post(Representation entity) {
InputStream input = null;
Request request = getRequest();
try {
LOGGER.info("Receiving objects from {}", request.getClientInfo().getAddress());
Representation representation = request.getEntity();
input = representation.getStream();
final GeoGIG ggit = getGeogig(request).get();
final BinaryPackedObjects unpacker = new BinaryPackedObjects(ggit.getRepository().objectDatabase());
CountingInputStream countingStream = new CountingInputStream(input);
Stopwatch sw = Stopwatch.createStarted();
IngestResults ingestResults = unpacker.ingest(countingStream);
sw.stop();
LOGGER.info(String.format("SendObjectResource: Processed %,d objects.\nInserted: %,d.\nExisting: %,d.\nTime to process: %s.\nStream size: %,d bytes.\n", ingestResults.total(), ingestResults.getInserted(), ingestResults.getExisting(), sw, countingStream.getCount()));
} catch (IOException e) {
LOGGER.warn("Error processing incoming objects from {}", request.getClientInfo().getAddress(), e);
throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
} finally {
if (input != null)
Closeables.closeQuietly(input);
}
}
use of org.locationtech.geogig.rest.RestletException in project GeoGig by boundlessgeo.
the class CommandResource method resolveFormat.
private MediaType resolveFormat(Form options, Variant variant) {
MediaType retval = variant.getMediaType();
String requested = options.getFirstValue("output_format");
if (requested != null) {
if (requested.equalsIgnoreCase("xml")) {
retval = MediaType.APPLICATION_XML;
} else if (requested.equalsIgnoreCase("json")) {
retval = MediaType.APPLICATION_JSON;
} else if (requested.equalsIgnoreCase("csv")) {
retval = CSV_MEDIA_TYPE;
} else {
throw new RestletException("Invalid output_format '" + requested + "'", org.restlet.data.Status.CLIENT_ERROR_BAD_REQUEST);
}
}
return retval;
}
use of org.locationtech.geogig.rest.RestletException in project GeoGig by boundlessgeo.
the class MergeFeatureResource method post.
public void post(Representation entity) {
InputStream input = null;
try {
input = getRequest().getEntity().getStream();
final GeoGIG ggit = getGeogig(getRequest()).get();
final Reader body = new InputStreamReader(input);
final JsonParser parser = new JsonParser();
final JsonElement conflictJson = parser.parse(body);
if (conflictJson.isJsonObject()) {
final JsonObject conflict = conflictJson.getAsJsonObject();
String featureId = null;
RevFeature ourFeature = null;
RevFeatureType ourFeatureType = null;
RevFeature theirFeature = null;
RevFeatureType theirFeatureType = null;
JsonObject merges = null;
if (conflict.has("path") && conflict.get("path").isJsonPrimitive()) {
featureId = conflict.get("path").getAsJsonPrimitive().getAsString();
}
Preconditions.checkState(featureId != null);
if (conflict.has("ours") && conflict.get("ours").isJsonPrimitive()) {
String ourCommit = conflict.get("ours").getAsJsonPrimitive().getAsString();
Optional<NodeRef> ourNode = parseID(ObjectId.valueOf(ourCommit), featureId, ggit);
if (ourNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
ourFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(ourNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
ourFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("theirs") && conflict.get("theirs").isJsonPrimitive()) {
String theirCommit = conflict.get("theirs").getAsJsonPrimitive().getAsString();
Optional<NodeRef> theirNode = parseID(ObjectId.valueOf(theirCommit), featureId, ggit);
if (theirNode.isPresent()) {
Optional<RevObject> object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().objectId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeature);
theirFeature = (RevFeature) object.get();
object = ggit.command(RevObjectParse.class).setObjectId(theirNode.get().getMetadataId()).call();
Preconditions.checkState(object.isPresent() && object.get() instanceof RevFeatureType);
theirFeatureType = (RevFeatureType) object.get();
}
}
if (conflict.has("merges") && conflict.get("merges").isJsonObject()) {
merges = conflict.get("merges").getAsJsonObject();
}
Preconditions.checkState(merges != null);
Preconditions.checkState(ourFeatureType != null || theirFeatureType != null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) (ourFeatureType != null ? ourFeatureType.type() : theirFeatureType.type()));
ImmutableList<PropertyDescriptor> descriptors = (ourFeatureType == null ? theirFeatureType : ourFeatureType).sortedDescriptors();
for (Entry<String, JsonElement> entry : merges.entrySet()) {
int descriptorIndex = getDescriptorIndex(entry.getKey(), descriptors);
if (descriptorIndex != -1 && entry.getValue().isJsonObject()) {
PropertyDescriptor descriptor = descriptors.get(descriptorIndex);
JsonObject attributeObject = entry.getValue().getAsJsonObject();
if (attributeObject.has("ours") && attributeObject.get("ours").isJsonPrimitive() && attributeObject.get("ours").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), ourFeature == null ? null : ourFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("theirs") && attributeObject.get("theirs").isJsonPrimitive() && attributeObject.get("theirs").getAsBoolean()) {
featureBuilder.set(descriptor.getName(), theirFeature == null ? null : theirFeature.getValues().get(descriptorIndex).orNull());
} else if (attributeObject.has("value") && attributeObject.get("value").isJsonPrimitive()) {
JsonPrimitive primitive = attributeObject.get("value").getAsJsonPrimitive();
if (primitive.isString()) {
try {
Object object = valueFromString(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsString());
featureBuilder.set(descriptor.getName(), object);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isNumber()) {
try {
Object value = valueFromNumber(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsNumber());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isBoolean()) {
try {
Object value = valueFromBoolean(FieldType.forBinding(descriptor.getType().getBinding()), primitive.getAsBoolean());
featureBuilder.set(descriptor.getName(), value);
} catch (Exception e) {
throw new Exception("Unable to convert attribute (" + entry.getKey() + ") to required type: " + descriptor.getType().getBinding().toString());
}
} else if (primitive.isJsonNull()) {
featureBuilder.set(descriptor.getName(), null);
} else {
throw new Exception("Unsupported JSON type for attribute value (" + entry.getKey() + ")");
}
}
}
}
SimpleFeature feature = featureBuilder.buildFeature(NodeRef.nodeFromPath(featureId));
RevFeature revFeature = RevFeatureBuilder.build(feature);
ggit.getRepository().stagingDatabase().put(revFeature);
getResponse().setEntity(new StringRepresentation(revFeature.getId().toString(), MediaType.TEXT_PLAIN));
}
} catch (Exception e) {
throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
} finally {
if (input != null)
Closeables.closeQuietly(input);
}
}
Aggregations