当前位置: 首页>>代码示例>>Java>>正文


Java ContainerResponseContext.setEntityStream方法代码示例

本文整理汇总了Java中javax.ws.rs.container.ContainerResponseContext.setEntityStream方法的典型用法代码示例。如果您正苦于以下问题:Java ContainerResponseContext.setEntityStream方法的具体用法?Java ContainerResponseContext.setEntityStream怎么用?Java ContainerResponseContext.setEntityStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.ws.rs.container.ContainerResponseContext的用法示例。


在下文中一共展示了ContainerResponseContext.setEntityStream方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: filter

import javax.ws.rs.container.ContainerResponseContext; //导入方法依赖的package包/类
/**
 * Intercept the Container request/response and log.
 * <p>
 * If output is streamed, also hook the output stream.
 *
 * @param request  Request to intercept
 * @param response  Response to intercept
 *
 * @throws IOException if there's a problem processing the request or response
 */
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response)
    throws IOException {
    appendRequestId(request.getHeaders().getFirst(X_REQUEST_ID_HEADER));
    RequestLog.startTiming(this);
    StringBuilder debugMsgBuilder = new StringBuilder();

    debugMsgBuilder.append("\tRequest: ").append(request.getMethod());
    debugMsgBuilder.append("\tlength=").append(request.getProperty(PROPERTY_REQ_LEN)).append("\t");
    debugMsgBuilder.append(renderUri(request.getUriInfo().getRequestUri())).append("\t");

    debugMsgBuilder.append("Response: ").append(response.getStatus()).append("\t");
    debugMsgBuilder.append(response.getStatusInfo()).append("\t");

    Long requestStartTime = (Long) request.getProperty(PROPERTY_NANOS);
    if (requestStartTime != null) {
        debugMsgBuilder.append((System.nanoTime() - requestStartTime) / MILLISECONDS_PER_NANOSECOND);
    }
    debugMsgBuilder.append(" ms\t");

    if (request.getSecurityContext().getUserPrincipal() != null) {
        String user = request.getSecurityContext().getUserPrincipal().getName();
        debugMsgBuilder.append("User=").append(user).append("\t");
    }

    appendStringHeaders(debugMsgBuilder, "> ", request.getHeaders().entrySet());
    appendObjectHeaders(debugMsgBuilder, "< ", response.getHeaders().entrySet());

    Response.StatusType status = response.getStatusInfo();
    String msg = "Successful request";
    if (status.getFamily() != SUCCESSFUL) {
        msg = response.hasEntity() ? response.getEntity().toString() : "Request without entity failed";
    }
    CacheLastObserver<Long> responseLengthObserver = new CacheLastObserver<>();
    RequestLog.record(new Epilogue(msg, status, responseLengthObserver));

    // if response is not yet finalized, we must intercept the output stream
    if (response.getLength() == -1 && response.hasEntity()) {
        //Pass the length of the response stream to the epilogue as soon as we know what that length is.
        Subject<Long, Long> lengthBroadcaster = ReplaySubject.create();
        lengthBroadcaster.subscribe(responseLengthObserver);
        OutputStream stream = new LengthOfOutputStream(response.getEntityStream(), lengthBroadcaster);

        response.setEntityStream(stream);
        request.setProperty(PROPERTY_OUTPUT_STREAM, stream);
    } else {
        debugMsgBuilder.append("length=").append(response.getLength()).append("\t");
        Observable.just((long) response.getLength()).subscribe(responseLengthObserver);
        LOG.debug(debugMsgBuilder.toString());
        RequestLog.stopTiming(this);
        RequestLog.stopTiming(TOTAL_TIMER);
        RequestLog.log();
    }
}
 
开发者ID:yahoo,项目名称:fili,代码行数:65,代码来源:BardLoggingFilter.java


注:本文中的javax.ws.rs.container.ContainerResponseContext.setEntityStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。