當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。