How to get MediaType from Request
Asked Answered
I

3

2

I'm building some kind of proxy server with Restlet, however I am having a problem that there's no automatic way to determine the MediaType based on the client request.

Here's my code:

Representation entity = null;
entity.setMediaType(processMediaType(path));

To process the media type:

private MediaType processMediaType(String path){
    MediaType type = MediaType.ALL;
    if(path.endsWith("html")){
        type = MediaType.TEXT_HTML;
    } else if (path.endsWith("css")) {
        type = MediaType.TEXT_CSS;
    } else if (path.endsWith("js")) {
        type = MediaType.TEXT_JAVASCRIPT;
    } else if (path.endsWith("txt")) {
        type = MediaType.TEXT_PLAIN;
    } else if (path.endsWith("jpg")){
        type = MediaType.IMAGE_JPEG;
    } else if (path.endsWith("png")){
        type = MediaType.IMAGE_PNG;
    }
    return type;
}

I was wondering if the MediaType can be constructed automatically by the framework (or by getting the MediaType from the request, which didn't worked for me) from the request such that I will not need to do these if-else statements which is very much limited in catching various media types.

Internationalist answered 13/2, 2016 at 18:34 Comment(0)
T
0

Restlet get the media type of the request based on the Content-Type header. To the value, you can use this:

MediaType mediaType = getRequest().getEntity().getMediaType();

The media type hints of the ClientInfo corresponds to what is provided within the Accept header:

getRequest().getClientInfo().getAcceptedMediaTypes();

To get the mapping of headers in the Restlet API, you could have a look at this link:

Translunar answered 23/2, 2016 at 10:1 Comment(0)
I
1

Why do you need to determine media type ? Normally when you construct a rest api in java you create individual methods for each media type allowed i.e.,

@Path("<your_path>")
@Consumes (MediaType.XML)
@Produces (MediaType.XML)
public Response processXMLRequest (...){
    //a more general method to process all request
    return processRequest (request, MediaType.XML);
}


@Path("<your_path>")
@Consumes (MediaType.JSON)
@Produces (MediaType.JSON)
public Response processXMLRequest (...){
    //a more general method to process all request
    return processRequest (request, MediaType.JSON);
}

etc ...

Immiscible answered 13/2, 2016 at 18:39 Comment(0)
E
1

If you need it this information is available in in the ClientInfo object within the request. Using the same mechanisms that Restlet uses to do content negotiation, which Em Ae's answer also automatically.

For example, within a ServerResource class function:

    List<MediaType> supported = null;
    MediaType type = getRequest().getClientInfo().getPreferredMediaType(supported);

Where you supply the list of supported MediaTypes in the most applicable way.

Eckard answered 15/2, 2016 at 10:49 Comment(2)
This is like getPreferredMediaType(null); ?Internationalist
@xybrek at the moment yes it is I leave supplying the List of MediaTypes that you support to you as you may wish to hard code it, obtain it from a static constant or some other means the simplest solution would be Arrays.asList(MediaType.TEXT_HTML, MediaType.TEXT_CSS, ..., etc); but I do not have enough context in your question to suggest this as being correct. basically replace the null above with your list of types you support.Eckard
T
0

Restlet get the media type of the request based on the Content-Type header. To the value, you can use this:

MediaType mediaType = getRequest().getEntity().getMediaType();

The media type hints of the ClientInfo corresponds to what is provided within the Accept header:

getRequest().getClientInfo().getAcceptedMediaTypes();

To get the mapping of headers in the Restlet API, you could have a look at this link:

Translunar answered 23/2, 2016 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.