ServiceFramework icon indicating copy to clipboard operation
ServiceFramework copied to clipboard

restResponse对象中如何增加header头的设置

Open xinqiyang opened this issue 12 years ago • 3 comments

allwefantasy大大

现在有个需求,做断点上传,在 restResponse 对象中设置下 http头里面加个 offset的字段。

貌似对http头的设置 restResponse 中还没有方法,这个需要咋个做。

对于这块的处理还不咋个懂,遂请教。。。。。

xinqiyang avatar Oct 30 '12 04:10 xinqiyang

这个扩展你可以直接做,并且是非常容易的。不要害怕修改源码。如果合适,我还可以合并你的源码到master分支去。 我现在暂时不先提交对offset 的支持。我下面会详细说如何修改支持该head.

RestResponse 接口有两个实现:

class DefaultResponse implements RestResponse {

                private String content;
                private byte[] contentByte;
                private int status = HttpStatus.HttpStatusOK;
                private String content_type = "application/json; charset=UTF-8";

还有一个是为了做测试用的:

public class MockRestResponse implements RestResponse {
    private String content;
    private Object object;
    private byte[] contentByte;
    private int status = HttpStatus.HttpStatusOK;
    private String content_type = "application/json; charset=UTF-8";

如果你需要添加一个新的 offset http header.很简单,三步搞定:

第一,为 DefaultResponse 添加一个属性。

class DefaultResponse implements RestResponse {

                private int offset = -1;//这里是添加的。
                private String content;
                private byte[] contentByte;
                private int status = HttpStatus.HttpStatusOK;
                private String content_type = "application/json; charset=UTF-8";

接着在 下面两个方法添加:

 public void output(String msg) throws IOException {
                    httpServletResponse.setContentType(content_type);
                    httpServletResponse.setStatus(status);
                   // 我不确认这个你是否需要的就是offset 这个名称的头部。
                    httpServletResponse.setHeader("offset",offset);
                    //httpServletResponse.setContentLength(msg.length());
                    PrintWriter printWriter = httpServletResponse.getWriter();
                    printWriter.write(msg);
                    printWriter.flush();
                    printWriter.close();
                }

                public void outputAsByte(byte[] msg) throws IOException {
                    //httpServletResponse.setContentType("application/json; charset=UTF-8");
                    // 我不确认这个你是否需要的就是offset 这个名称的头部。
                    httpServletResponse.setHeader("offset",offset);
                    httpServletResponse.setStatus(status);
                    ServletOutputStream outputStream = httpServletResponse.getOutputStream();
                    outputStream.write(msg);
                    outputStream.flush();
                    outputStream.close();
                }

当然 如果你不希望每次都带那个offset 的header 你可以加个if判断。

不知道上面的能不能帮到你。有疑问随时问我。

allwefantasy avatar Oct 30 '12 06:10 allwefantasy

恩下午查了下httpServletResponse 看到setHeader方法,试了下还是可以用的,也是你这样修改的。 就是在output里面多加了一个判断 if (offset >0 ) { setHeader } 。。。。

对于这些对象,看来得多看源码。。

谢谢 ^_^ 。。。。。

xinqiyang avatar Oct 30 '12 07:10 xinqiyang

HttpServletResponse 和 HttpServletRequest 都是jetty 提供的标准servlet 方法。但是在框架层次隐藏掉它了。因为它真的不好用。哈哈

allwefantasy avatar Oct 30 '12 07:10 allwefantasy