How to get MediaType from Request

故事扮演 提交于 2019-12-24 01:26:02

问题


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.


回答1:


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:

  • Mapping HTTP Headers - https://restlet.com/technical-resources/restlet-framework/guide/2.2/core/http-headers-mapping



回答2:


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 ...




回答3:


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.



来源:https://stackoverflow.com/questions/35383763/how-to-get-mediatype-from-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!