Search in sources :

Example 1 with Request

use of org.restlet.data.Request 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);
    }
}
Also used : CountingInputStream(com.google.common.io.CountingInputStream) InputStream(java.io.InputStream) RestletException(org.locationtech.geogig.rest.RestletException) Request(org.restlet.data.Request) CountingInputStream(com.google.common.io.CountingInputStream) Stopwatch(com.google.common.base.Stopwatch) Representation(org.restlet.resource.Representation) IngestResults(org.locationtech.geogig.remote.BinaryPackedObjects.IngestResults) IOException(java.io.IOException) GeoGIG(org.locationtech.geogig.api.GeoGIG) BinaryPackedObjects(org.locationtech.geogig.remote.BinaryPackedObjects)

Example 2 with Request

use of org.restlet.data.Request in project GeoGig by boundlessgeo.

the class TaskStatusResource method getRepresentation.

@Override
public Representation getRepresentation(Variant variant) {
    final Request request = getRequest();
    final String taskId = getStringAttribute(request, "taskId");
    final boolean prune = Boolean.valueOf(getRequest().getResourceRef().getQueryAsForm().getFirstValue("prune"));
    final boolean cancel = Boolean.valueOf(getRequest().getResourceRef().getQueryAsForm().getFirstValue("cancel"));
    final AsyncContext asyncContext = AsyncContext.get();
    MediaType mediaType = variant.getMediaType();
    final String rootPath = request.getRootRef().toString();
    if (Strings.isNullOrEmpty(taskId)) {
        Iterable<AsyncCommand<? extends Object>> all = asyncContext.getAll();
        return new TaskListResource(mediaType, rootPath, all);
    }
    Optional<AsyncCommand<?>> cmd;
    if (prune) {
        cmd = asyncContext.getAndPruneIfFinished(taskId);
    } else {
        cmd = asyncContext.get(taskId);
    }
    if (!cmd.isPresent()) {
        throw new RestletException("Task not found: " + taskId, Status.CLIENT_ERROR_NOT_FOUND);
    }
    AsyncCommand<?> command = cmd.get();
    if (cancel) {
        command.tryCancel();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        // ignore
        }
        if (prune) {
            asyncContext.getAndPruneIfFinished(taskId);
        }
    }
    return Representations.newRepresentation(command, mediaType, rootPath);
}
Also used : AsyncCommand(org.locationtech.geogig.rest.AsyncContext.AsyncCommand) Request(org.restlet.data.Request) MediaType(org.restlet.data.MediaType)

Example 3 with Request

use of org.restlet.data.Request in project GeoGig by boundlessgeo.

the class Main method createRoot.

@Override
public Restlet createRoot() {
    Router router = new Router() {

        @Override
        protected synchronized void init(Request request, Response response) {
            super.init(request, response);
            if (!isStarted()) {
                return;
            }
            request.getAttributes().put(RepositoryProvider.KEY, repoProvider);
        }
    };
    Router repo = new RepositoryRouter();
    Router osm = new OSMRouter();
    router.attach("/tasks", TaskStatusResource.class);
    router.attach("/tasks/{taskId}.{extension}", TaskStatusResource.class);
    router.attach("/tasks/{taskId}", TaskStatusResource.class);
    router.attach("/osm", osm);
    router.attach("/repo", repo);
    router.attach("/{command}.{extension}", CommandResource.class);
    router.attach("/{command}", CommandResource.class);
    org.restlet.Context context = getContext();
    // enable support for compressing responses if the client supports it.
    // NOTE: restlet 1.0.8 leaves a dangling thread on each request (see
    // EncodeRepresentation.getStream()
    // This problem is fixed in latest versions (2.x) of restlet. See the javadocs for
    // FixedEncoder for further detail
    // Encoder responseEncoder = new com.noelios.restlet.application.Encoder(context);
    FixedEncoder encoder = new FixedEncoder(context);
    encoder.setEncodeRequest(false);
    encoder.setEncodeResponse(true);
    encoder.setNext(router);
    Decoder decoder = new Decoder(context);
    decoder.setDecodeRequest(true);
    decoder.setDecodeResponse(false);
    decoder.setNext(encoder);
    return decoder;
}
Also used : Response(org.restlet.data.Response) OSMRouter(org.locationtech.geogig.rest.osm.OSMRouter) FixedEncoder(org.locationtech.geogig.rest.repository.FixedEncoder) Request(org.restlet.data.Request) OSMRouter(org.locationtech.geogig.rest.osm.OSMRouter) RepositoryRouter(org.locationtech.geogig.rest.repository.RepositoryRouter) Router(org.restlet.Router) RepositoryRouter(org.locationtech.geogig.rest.repository.RepositoryRouter) Decoder(com.noelios.restlet.application.Decoder)

Example 4 with Request

use of org.restlet.data.Request in project GeoGig by boundlessgeo.

the class OsmDownloadWebOp method getRepresentation.

@Override
public Representation getRepresentation(final Variant variant) {
    final Request request = getRequest();
    Context context = getContext(request);
    Form options = getRequest().getResourceRef().getQueryAsForm();
    final String filterFileArg = options.getFirstValue("filter");
    final String bboxArg = options.getFirstValue("bbox");
    final String messageArg = options.getFirstValue("message");
    final boolean update = Boolean.valueOf(options.getFirstValue("update"));
    final boolean rebase = Boolean.valueOf(options.getFirstValue("rebase"));
    final String mappingFileArg = options.getFirstValue("mapping");
    checkArgSpec(filterFileArg != null ^ bboxArg != null || update, "You must specify a filter file or a bounding box");
    checkArgSpec((filterFileArg != null || bboxArg != null) ^ update, "Filters cannot be used when updating");
    checkArgSpec(context.index().isClean() && context.workingTree().isClean(), "Working tree and index are not clean");
    checkArgSpec(!rebase || update, "rebase switch can only be used when updating");
    final File filterFile = parseFile(filterFileArg);
    final File mappingFile = parseFile(mappingFileArg);
    final List<String> bbox = parseBbox(bboxArg);
    checkArgSpec(filterFile == null || filterFile.exists(), "The specified filter file does not exist");
    checkArgSpec(mappingFile == null || mappingFile.exists(), "The specified mapping file does not exist");
    AbstractGeoGigOp<Optional<OSMReport>> command;
    if (update) {
        command = context.command(OSMUpdateOp.class).setRebase(rebase).setMessage(messageArg).setAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT);
    } else {
        command = context.command(OSMDownloadOp.class).setBbox(bbox).setFilterFile(filterFile).setMessage(messageArg).setMappingFile(mappingFile).setOsmAPIUrl(OSMUtils.DEFAULT_API_ENDPOINT);
    }
    AsyncCommand<Optional<OSMReport>> asyncCommand;
    URL repo = context.repository().getLocation();
    String description = String.format("osm download filter: %s, bbox: %s, mapping: %s, update: %s, rebase: %s, repository: %s", filterFileArg, bboxArg, mappingFileArg, update, rebase, repo);
    asyncCommand = AsyncContext.get().run(command, description);
    final String rootPath = request.getRootRef().toString();
    MediaType mediaType = variant.getMediaType();
    Representation rep = new OSMReportRepresentation(mediaType, asyncCommand, rootPath);
    return rep;
}
Also used : Context(org.locationtech.geogig.api.Context) AsyncContext(org.locationtech.geogig.rest.AsyncContext) Optional(com.google.common.base.Optional) Form(org.restlet.data.Form) Request(org.restlet.data.Request) Representation(org.restlet.resource.Representation) URL(java.net.URL) MediaType(org.restlet.data.MediaType) File(java.io.File)

Example 5 with Request

use of org.restlet.data.Request in project GeoGig by boundlessgeo.

the class OsmImportWebOp method getRepresentation.

@Override
public Representation getRepresentation(final Variant variant) {
    final Request request = getRequest();
    final Context context = super.getContext(request);
    Form options = getRequest().getResourceRef().getQueryAsForm();
    final String urlOrFilepath = options.getFirstValue("uri");
    final boolean add = Boolean.valueOf(options.getFirstValue("add"));
    final String mappingFile = options.getFirstValue("mapping");
    Mapping mapping = null;
    if (mappingFile != null) {
        mapping = Mapping.fromFile(mappingFile);
    }
    final boolean noRaw = Boolean.valueOf(options.getFirstValue("noRaw"));
    final String message = options.getFirstValue("message");
    if (urlOrFilepath == null) {
        String msg = "Missing parameter: uri\n" + "Usage: GET <repo context>/osm/import?uri=<osm file URI>[&<arg>=<value>]+\n" + "Arguments:\n" + " * uri: Mandatory. URL or path to OSM data file in the server filesystem\n" + " * add: Optional. true|false. Default: false. If true, do not remove previous data before importing.\n" + " * mapping: Optional. Location of mapping file in the server filesystem\n" + " * noRaw: Optional. true|false. Default: false. If true, do not import raw data when using a mapping\n" + " * message: Optional. Message for the commit to create.";
        throw new CommandSpecException(msg);
    }
    OSMImportOp command = context.command(OSMImportOp.class);
    command.setAdd(add);
    command.setDataSource(urlOrFilepath);
    command.setMapping(mapping);
    command.setMessage(message);
    command.setNoRaw(noRaw);
    AsyncCommand<Optional<OSMReport>> asyncCommand;
    URL repo = context.repository().getLocation();
    String description = String.format("osm import %s, repository: %s", urlOrFilepath, repo);
    asyncCommand = AsyncContext.get().run(command, description);
    final String rootPath = request.getRootRef().toString();
    MediaType mediaType = variant.getMediaType();
    Representation rep = new OSMReportRepresentation(mediaType, asyncCommand, rootPath);
    return rep;
}
Also used : Context(org.locationtech.geogig.api.Context) AsyncContext(org.locationtech.geogig.rest.AsyncContext) Optional(com.google.common.base.Optional) Form(org.restlet.data.Form) Request(org.restlet.data.Request) Mapping(org.locationtech.geogig.osm.internal.Mapping) OSMImportOp(org.locationtech.geogig.osm.internal.OSMImportOp) Representation(org.restlet.resource.Representation) URL(java.net.URL) MediaType(org.restlet.data.MediaType) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException)

Aggregations

Request (org.restlet.data.Request)7 Representation (org.restlet.resource.Representation)4 MediaType (org.restlet.data.MediaType)3 Optional (com.google.common.base.Optional)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Context (org.locationtech.geogig.api.Context)2 GeoGIG (org.locationtech.geogig.api.GeoGIG)2 AsyncContext (org.locationtech.geogig.rest.AsyncContext)2 Form (org.restlet.data.Form)2 Stopwatch (com.google.common.base.Stopwatch)1 CountingInputStream (com.google.common.io.CountingInputStream)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 Decoder (com.noelios.restlet.application.Decoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 Mapping (org.locationtech.geogig.osm.internal.Mapping)1