Search in sources :

Example 16 with MediaType

use of org.restlet.data.MediaType 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 17 with MediaType

use of org.restlet.data.MediaType 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)

Example 18 with MediaType

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

the class JettisonRepresentation method createWriter.

private XMLStreamWriter createWriter(Writer writer) {
    final MediaType mediaType = getMediaType();
    XMLStreamWriter xml;
    if (mediaType.getSubType().equalsIgnoreCase("xml")) {
        try {
            xml = XMLOutputFactory.newFactory().createXMLStreamWriter(writer);
        } catch (XMLStreamException ex) {
            throw new RuntimeException(ex);
        }
    } else if (mediaType == MediaType.APPLICATION_JSON) {
        xml = new MappedXMLStreamWriter(new MappedNamespaceConvention(), writer);
    } else {
        throw new RuntimeException("mediatype not handled " + mediaType);
    }
    return xml;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) MediaType(org.restlet.data.MediaType) MappedXMLStreamWriter(org.codehaus.jettison.mapped.MappedXMLStreamWriter) MappedNamespaceConvention(org.codehaus.jettison.mapped.MappedNamespaceConvention)

Example 19 with MediaType

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

the class ConsoleResourceResource method handleGet.

@Override
public void handleGet() {
    final String resourceName;
    {
        String res = RESTUtils.getStringAttribute(getRequest(), "resource");
        if (null == res) {
            resourceName = "terminal.html";
        } else {
            resourceName = res;
        }
    }
    MediaType mediaType = guessMediaType(resourceName);
    getResponse().setEntity(new StreamRepresentation(mediaType) {

        @Override
        public void write(OutputStream outputStream) throws IOException {
            // System.out.println("returning " + resourceName);
            ByteStreams.copy(getStream(), outputStream);
        }

        @Override
        public InputStream getStream() throws IOException {
            InputStream inputStream = ConsoleResourceResource.class.getResourceAsStream(resourceName);
            return inputStream;
        }
    });
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileBackedOutputStream(com.google.common.io.FileBackedOutputStream) MediaType(org.restlet.data.MediaType) IOException(java.io.IOException) StreamRepresentation(org.restlet.resource.StreamRepresentation)

Example 20 with MediaType

use of org.restlet.data.MediaType in project lobcder by skoulouzis.

the class ResponseAdapter method setTargetEntity.

// Grab the Milton entity, wrap it in a Restlet entity, set it on target
public void setTargetEntity() {
    final Entity entity;
    if ((entity = getEntity()) != null) {
        MediaType contentType = MediaType.valueOf(getContentTypeHeader());
        getTarget().setEntity(new OutputRepresentation(contentType) {

            @Override
            public void write(OutputStream outputStream) throws IOException {
                try {
                    entity.write(ResponseAdapter.this, outputStream);
                } catch (IOException ex) {
                    throw ex;
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}
Also used : Entity(io.milton.http.Response.Entity) OutputRepresentation(org.restlet.representation.OutputRepresentation) OutputStream(java.io.OutputStream) MediaType(org.restlet.data.MediaType) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

MediaType (org.restlet.data.MediaType)29 StringRepresentation (org.restlet.representation.StringRepresentation)11 Representation (org.restlet.representation.Representation)10 IOException (java.io.IOException)9 Map (java.util.Map)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)7 Writer (java.io.Writer)6 TemplateException (freemarker.template.TemplateException)5 HashMap (java.util.HashMap)5 Form (org.restlet.data.Form)5 ArrayList (java.util.ArrayList)4 Preference (org.restlet.data.Preference)4 EmptyRepresentation (org.restlet.representation.EmptyRepresentation)4 ResourceException (org.restlet.resource.ResourceException)4 Template (freemarker.template.Template)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ParseException (java.text.ParseException)3 InputRepresentation (org.restlet.representation.InputRepresentation)3 Representation (org.restlet.resource.Representation)3