Limit path media type mappings in Jersey

后端 未结 1 449
轮回少年
轮回少年 2021-01-20 16:03

I have configured MEDIA_TYPE_MAPPINGS for my Jersey apps. Unfortunately, this causes some trouble with a generic upload service in my app.

@PUT
         


        
1条回答
  •  抹茶落季
    2021-01-20 16:47

    First of all, this is in no way a bug. It is the expected behavior. The purpose of media type mappings is not related to working with files, but instead an alternative form of content negotiation for cases where setting headers may not be available, for instance in a browser.

    Though not in the official spec, this feature was part of a draft prior to the specs final release. Most implementations decided to include it one way or another. Jersey happens to let you configure it. So can see here in the spec in 3.7.1 Request Preprocessing

    1. Set

      • M = {config.getMediaTypeMappings().keySet()}
      • L = {config.getLanguageMappings().keySet()}
      • m = null
      • l = null
      • Where config is an instance of the application-supplied subclass of ApplicationConfig.
    2. For each extension (a . character followed by one or more alphanumeric characters) e in the final path segment scanning from right to left:

      • (a) Remove the leading ‘.’ character from e
      • (b) If m is null and e is a member of M then remove the corresponding extension from the effective request URI and set m = e.
      • (c) Else if l is null and e is a member of L then remove the corresponding extension from the effective request URI and set l = e. Else go to step 4
    3. If m is not null then set the value of the Accept header to config.getExtensionMappings().get(m)

    3(b) is basically saying that the extension should be removed from the requested URI and 4 is stating that there should be some extension mapping that would map say json (the extension) to application/json and set that as the Accept header. You can see from different tests, this behavior

    @POST
    @Path("/files/{file}")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response doTest(@PathParam("file") String fileName, @Context HttpHeaders headers) {
        String accept = headers.getHeaderString(HttpHeaders.ACCEPT);
        return Response.ok(fileName + "; Accept: " + accept).build();
    }
    ...
    
    Map map = new HashMap<>();
    map.put("xml", MediaType.APPLICATION_XML_TYPE);
    resourceCnfig.property(ServerProperties.MEDIA_TYPE_MAPPINGS, map);
    

    curl -v http://localhost:8080/api/mapping/files/file.xml -X POST
    Result: file; Accept: application/xml

    If we comment out that configuration property, you will see that the Accept header hasn't been set.

    curl -v http://localhost:8080/api/mapping/files/file.xml -X POST
    Result: file.xml; Accept: */**

    That being said...

    When you configure the ServerProperties.MEDIA_TYPE_MAPPINGS, the org.glassfish.jersey.server.filter.UriConnegFilter is the filter used for this feature. You can see in the source code in line 204 and 211, where the filter is stripping the extension

    path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
    ...
    rc.setRequestUri(uriInfo.getRequestUriBuilder().replacePath(path).build(new Object[0]));
    

    So there's no way to configure this (at least as far as I can tell from looking at the source), so we would have to extend that class, override the filter method and take out, at minimum, that last line that actually does the replacing, then register the filter. Here's what I did to get it to work. I simply copy and pasted the code from the filter, and commented out the line where it replaces the extension

    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import javax.annotation.Priority;
    import javax.ws.rs.container.ContainerRequestContext;
    import javax.ws.rs.container.PreMatching;
    import javax.ws.rs.core.Configuration;
    import javax.ws.rs.core.Context;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.PathSegment;
    import javax.ws.rs.core.UriInfo;
    import org.glassfish.jersey.server.filter.UriConnegFilter;
    
    @PreMatching
    @Priority(3000)
    public class MyUriConnegFilter extends UriConnegFilter {
    
        public MyUriConnegFilter(@Context Configuration config) {
            super(config);
        }
    
        public MyUriConnegFilter(Map mediaTypeMappings, 
                                 Map languageMappings) {
            super(mediaTypeMappings, languageMappings);
        }
    
        @Override
        public void filter(ContainerRequestContext rc)
                throws IOException {
            UriInfo uriInfo = rc.getUriInfo();
    
            String path = uriInfo.getRequestUri().getRawPath();
            if (path.indexOf('.') == -1) {
                return;
            }
            List l = uriInfo.getPathSegments(false);
            if (l.isEmpty()) {
                return;
            }
            PathSegment segment = null;
            for (int i = l.size() - 1; i >= 0; i--) {
                segment = (PathSegment) l.get(i);
                if (segment.getPath().length() > 0) {
                    break;
                }
            }
            if (segment == null) {
                return;
            }
            int length = path.length();
    
            String[] suffixes = segment.getPath().split("\\.");
            for (int i = suffixes.length - 1; i >= 1; i--) {
                String suffix = suffixes[i];
                if (suffix.length() != 0) {
                    MediaType accept = (MediaType) this.mediaTypeMappings.get(suffix);
                    if (accept != null) {
                        rc.getHeaders().putSingle("Accept", accept.toString());
    
                        int index = path.lastIndexOf('.' + suffix);
                        path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
                        suffixes[i] = "";
                        break;
                    }
                }
            }
            for (int i = suffixes.length - 1; i >= 1; i--) {
                String suffix = suffixes[i];
                if (suffix.length() != 0) {
                    String acceptLanguage = (String) this.languageMappings.get(suffix);
                    if (acceptLanguage != null) {
                        rc.getHeaders().putSingle("Accept-Language", acceptLanguage);
    
                        int index = path.lastIndexOf('.' + suffix);
                        path = new StringBuilder(path).delete(index, index + suffix.length() + 1).toString();
                        suffixes[i] = "";
                        break;
                    }
                }
            }
            if (length != path.length()) {
                //rc.setRequestUri(uriInfo.getRequestUriBuilder().replacePath(path).build(new Object[0]));
            }
        }
    }
    

    Then configure it

    Map map = new HashMap<>();
    map.put("xml", MediaType.APPLICATION_XML_TYPE);
    map.put("json", MediaType.APPLICATION_JSON_TYPE);
    resourceConfig.register(new MyUriConnegFilter(map, null));
    

    curl -v http://localhost:8080/api/mapping/files/file.xml -X POST
    Result: file.xml; Accept: application/xml

    curl -v http://localhost:8080/api/mapping/files/file.json -X POST
    Result: file.json; Accept: application/json

    0 讨论(0)
提交回复
热议问题