Search in sources :

Example 11 with MediaType

use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.

the class ExtensionMediaTypeFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    List<String> segments = request.getResourceRef().getSegments();
    if (segments.get(segments.size() - 1).equals(""))
        return Filter.CONTINUE;
    String extensions = request.getResourceRef().getExtensions();
    if (extensions != null) {
        int idx = extensions.lastIndexOf(".");
        if (idx != -1) {
            extensions = extensions.substring(idx + 1);
        }
        MetadataService metadataService = getApplication().getMetadataService();
        Metadata metadata = metadataService.getMetadata(extensions);
        if (metadata != null && metadata instanceof MediaType) {
            request.getClientInfo().setAcceptedMediaTypes(Collections.singletonList(new Preference<MediaType>((MediaType) metadata)));
            String path = request.getResourceRef().getPath();
            path = path.substring(0, path.length() - extensions.length() - 1);
            request.getResourceRef().setPath(path);
        }
    }
    return Filter.CONTINUE;
}
Also used : Preference(org.restlet.data.Preference) Metadata(org.restlet.data.Metadata) MediaType(org.restlet.data.MediaType) MetadataService(org.restlet.service.MetadataService)

Example 12 with MediaType

use of org.restlet.data.MediaType in project qi4j-sdk by Qi4j.

the class JSONResponseWriter method writeResponse.

@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
    if (result instanceof JSONObject) {
        MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
        if (MediaType.APPLICATION_JSON.equals(type)) {
            JSONObject json = (JSONObject) result;
            StringRepresentation representation = new StringRepresentation(json.toString(), MediaType.APPLICATION_JSON);
            response.setEntity(representation);
            return true;
        } else if (MediaType.TEXT_HTML.equals(type)) {
            JSONObject json = (JSONObject) result;
            StringRepresentation representation = new StringRepresentation(json.toString(), MediaType.TEXT_HTML);
            response.setEntity(representation);
            return true;
        }
    }
    return false;
}
Also used : JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) MediaType(org.restlet.data.MediaType)

Example 13 with MediaType

use of org.restlet.data.MediaType in project camel by apache.

the class RestletConverter method toMediaTypes.

@Converter
public static MediaType[] toMediaTypes(final String name) {
    final String[] strings = name.split(",");
    final List<MediaType> answer = new ArrayList<>(strings.length);
    for (int i = 0; i < strings.length; i++) {
        final MediaType mediaType = toMediaType(strings[i]);
        if (mediaType != null) {
            answer.add(mediaType);
        }
    }
    return answer.toArray(new MediaType[answer.size()]);
}
Also used : ArrayList(java.util.ArrayList) MediaType(org.restlet.data.MediaType) Converter(org.apache.camel.Converter)

Example 14 with MediaType

use of org.restlet.data.MediaType in project camel by apache.

the class DefaultRestletBinding method populateExchangeFromRestletResponse.

public void populateExchangeFromRestletResponse(Exchange exchange, Response response) throws Exception {
    for (Map.Entry<String, Object> entry : response.getAttributes().entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (!headerFilterStrategy.applyFilterToExternalHeaders(key, value, exchange)) {
            exchange.getOut().setHeader(key, value);
            LOG.debug("Populate exchange from Restlet response header: {} value: {}", key, value);
        }
    }
    // set response code
    int responseCode = response.getStatus().getCode();
    exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
    // set restlet response as header so end user have access to it if needed
    exchange.getOut().setHeader(RestletConstants.RESTLET_RESPONSE, response);
    if (response.getEntity() != null) {
        // get content type
        MediaType mediaType = response.getEntity().getMediaType();
        if (mediaType != null) {
            LOG.debug("Setting the Content-Type to be {}", mediaType.toString());
            exchange.getOut().setHeader(Exchange.CONTENT_TYPE, mediaType.toString());
        }
        if (streamRepresentation && response.getEntity() instanceof StreamRepresentation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            InputStream is = representationDecoded.getStream();
            exchange.getOut().setBody(is);
            if (autoCloseStream) {
                // ensure the input stream is closed when we are done routing
                exchange.addOnCompletion(new RestletOnCompletion(is));
            }
        } else if (response.getEntity() instanceof Representation) {
            Representation representationDecoded = new DecodeRepresentation(response.getEntity());
            exchange.getOut().setBody(representationDecoded.getText());
        } else {
            // get content text by default
            String text = response.getEntity().getText();
            LOG.debug("Populate exchange from Restlet response: {}", text);
            exchange.getOut().setBody(text);
        }
    }
    // preserve headers from in by copying any non existing headers
    // to avoid overriding existing headers with old values
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), false);
}
Also used : DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) InputStream(java.io.InputStream) MediaType(org.restlet.data.MediaType) EmptyRepresentation(org.restlet.representation.EmptyRepresentation) StringRepresentation(org.restlet.representation.StringRepresentation) InputRepresentation(org.restlet.representation.InputRepresentation) ByteArrayRepresentation(org.restlet.representation.ByteArrayRepresentation) FileRepresentation(org.restlet.representation.FileRepresentation) StreamRepresentation(org.restlet.representation.StreamRepresentation) Representation(org.restlet.representation.Representation) DecodeRepresentation(org.restlet.engine.application.DecodeRepresentation) Map(java.util.Map) StreamRepresentation(org.restlet.representation.StreamRepresentation)

Example 15 with MediaType

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

the class CommandResource method runCommand.

private Representation runCommand(Variant variant, Request request) {
    final Optional<GeoGIG> geogig = getGeogig(request);
    Preconditions.checkState(geogig.isPresent());
    Representation rep = null;
    WebAPICommand command = null;
    Form options = getRequest().getResourceRef().getQueryAsForm();
    String commandName = (String) getRequest().getAttributes().get("command");
    MediaType format = resolveFormat(options, variant);
    try {
        ParameterSet params = new FormParams(options);
        command = CommandBuilder.build(commandName, params);
        assert command != null;
    } catch (CommandSpecException ex) {
        rep = formatException(ex, format);
    }
    try {
        if (command != null) {
            RestletContext ctx = new RestletContext(geogig.get());
            command.run(ctx);
            rep = ctx.getRepresentation(format, getJSONPCallback());
        }
    } catch (IllegalArgumentException ex) {
        rep = formatException(ex, format);
    } catch (Exception ex) {
        rep = formatUnexpectedException(ex, format);
    }
    return rep;
}
Also used : ParameterSet(org.locationtech.geogig.web.api.ParameterSet) WebAPICommand(org.locationtech.geogig.web.api.WebAPICommand) Form(org.restlet.data.Form) Representation(org.restlet.resource.Representation) WriterRepresentation(org.locationtech.geogig.rest.WriterRepresentation) RestletException(org.locationtech.geogig.rest.RestletException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) MediaType(org.restlet.data.MediaType) CommandSpecException(org.locationtech.geogig.web.api.CommandSpecException) GeoGIG(org.locationtech.geogig.api.GeoGIG)

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