5

为什么以下示例会在请求中吞下我的“Content-Encoding”的 HTTP-Header。我正在编写一个需要解码自定义编码格式的应用程序。但是,我永远无法从请求中获取“Content-Encoding”标头。既不在实际资源中,也不在 ReaderInterceptor 中。在响应中,这个编码头没有被吞掉。

在以下(可运行的)示例中可以很容易地观察到这种行为:

public class Demo extends JerseyTest {
  @Override
  protected Application configure() {
    enable(TestProperties.DUMP_ENTITY);
    enable(TestProperties.LOG_TRAFFIC);
    return new ResourceConfig(MyResource.class, MyInterceptor.class);
  }

  public static final String PATH = "path";
  public static final String ENCODING = "my-encoding";
  public static final String CUSTOM_HEADER = "X-Content-Encoding";
  public static final String QUESTION = "question", ANSWER = "answer";

  @Path(PATH)
  public static class MyResource {
    @POST
    public Response handle(String value, @Context HttpHeaders httpHeaders) {
      assertEquals(ENCODING, httpHeaders.getHeaderString(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header mysteriously disappeared.
      assertEquals(ENCODING, httpHeaders.getHeaderString(HttpHeaders.CONTENT_ENCODING));
      return Response
          .ok(ANSWER)
          .header(CUSTOM_HEADER, ENCODING)
          .header(HttpHeaders.CONTENT_ENCODING, ENCODING)
          .build();
    }
  }

  public static class MyInterceptor implements ReaderInterceptor, WriterInterceptor {
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext context) 
        throws IOException, WebApplicationException {
      assertEquals(ENCODING, context.getHeaders().getFirst(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header mysteriously disappeared.
      assertEquals(ENCODING, context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
      return context.proceed();
    }

    @Override
    public void aroundWriteTo(WriterInterceptorContext context) 
        throws IOException, WebApplicationException {
      assertEquals(ENCODING, context.getHeaders().getFirst(CUSTOM_HEADER));
      // Here, the "Content-Encoding" header can be found.
      assertEquals(ENCODING, context.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
      context.proceed();
    }
  }

  @Test
  public void test() throws Exception {
    Response response = target(PATH)
        .request()
        .header(CUSTOM_HEADER, ENCODING)
        .header(HttpHeaders.CONTENT_ENCODING, ENCODING)
        .post(Entity.text(QUESTION));
    assertEquals(200, response.getStatus());
    assertEquals(ENCODING, response.getHeaders().getFirst(CUSTOM_HEADER));
    // Here, the "Content-Encoding" header can be found.
    assertEquals(ENCODING, response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
  }
}

泽西试图修复我的内容编码的幕后是否发生了一些神奇的事情?(它不能因为它是一个封闭源编码,事实上我必须通过查询网络中另一台服务器上的另一个应用程序来解决它。)我什至无法在请求转储中发现“Content-Encoding”标头,这就是为什么我怀疑泽西根本不发送标题。

我当然可以使用一些“X-Content-Encoding”标头,如示例中所示。但是这个解决方案很愚蠢。我已经搜索了各种CommonProperties, ServerProperties,ClientProperties常量池,但没有找到配置选项。

4

1 回答 1

8

您看到的问题是因为您有效地覆盖了 Content-Encoding 标头

.post(Entity.text(QUESTION));

称呼。该Entity.text(...)方法生成内容数据Variant字段设置为的实体:

media type = "text/plain";
content language = null;
content encoding = null;

这些null内容编码和语言的值反过来会删除任何先前设置的Content-EncodingContent-Language标题。要解决此问题,您需要将内容编码指定为实体的一部分:

    Response response = target(PATH)
            .request()
            .post(Entity.entity(QUESTION, 
                  new Variant(MediaType.TEXT_PLAIN_TYPE, (String) null, "my-encoding")));

(我同意这种行为有点令人困惑,因为它并不明显。也许我们应该修复 Jersey 以不覆盖带有null变量字段值的标题(如果设置...)

于 2013-11-05T19:08:52.213 回答