• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java ContentCachingResponseWrapper类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.springframework.web.util.ContentCachingResponseWrapper的典型用法代码示例。如果您正苦于以下问题:Java ContentCachingResponseWrapper类的具体用法?Java ContentCachingResponseWrapper怎么用?Java ContentCachingResponseWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ContentCachingResponseWrapper类属于org.springframework.web.util包,在下文中一共展示了ContentCachingResponseWrapper类的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: doFilterInternal

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws ServletException, IOException {
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

    try {
        filterChain.doFilter(requestWrapper, responseWrapper);
    } finally {
        String requestBody = new String(requestWrapper.getContentAsByteArray());
        log.info("Request body: {}", requestBody);

        String responseBody = new String(responseWrapper.getContentAsByteArray());
        log.info("Response body: {}", responseBody);

        // Do not forget this line after reading response content or actual response will be empty!
        responseWrapper.copyBodyToResponse();
    }
}
 
开发者ID:amvnetworks,项目名称:amv-access-api-poc,代码行数:20,代码来源:PayloadLoggingFilter.java


示例2: extractResponseResult

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
/**
 * 提取请求结果
 * @param request
 * @param loggingContext
 * @return
 */
protected Object extractResponseResult(HttpServletResponse response, LoggingContext loggingContext) throws Exception {
	MediaType contentType = loggingContext.getHttpAccessLog().getResponseContentType();
	if(contentType != null){
		if(isContentCachingResponse(response)){
			String charset = response.getCharacterEncoding();
			if(StringUtils.isEmpty(charset)){
				charset = StringUtils.defaultIfEmpty(contentType.getCharset().name(), GlobalConstants.SYSTEM_DEFAULT_CHARSET);
			}
			ContentCachingResponseWrapper responseToUse = (ContentCachingResponseWrapper) response;
			byte[] bytes = responseToUse.getContentAsByteArray();
			if(bytes != null){
				return IOUtils.toString(bytes, charset);
			}
		}
	}
	return null;
}
 
开发者ID:penggle,项目名称:xproject,代码行数:24,代码来源:AbstractHttpAccessLoggingInterceptor.java


示例3: updateResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
private void updateResponse(String requestURI, ContentCachingResponseWrapper responseWrapper) throws IOException {
	try {
		HttpServletResponse rawResponse = (HttpServletResponse) responseWrapper.getResponse();
		byte[] body = responseWrapper.getContentAsByteArray();
		ServletOutputStream outputStream = rawResponse.getOutputStream();
		if (rawResponse.isCommitted()) {
			if (body.length > 0) {
				StreamUtils.copy(body, outputStream);
			}
		} else {
			if (body.length > 0) {
				rawResponse.setContentLength(body.length);
				StreamUtils.copy(body, rawResponse.getOutputStream());
			}
		}
		finishResponse(outputStream, body);
	} catch (Exception ex) {
		logger.error("请求地址为" + requestURI + "的连接返回报文失败,原因是{}", ex.getMessage());
	}
}
 
开发者ID:wengwh,项目名称:plumdo-stock,代码行数:21,代码来源:RequestLogFilter.java


示例4: doFilterInternal

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
	long begintime = DateUtils.currentTimeMillis();
	JsonContentCachingRequestWrapper requestWrapper = new JsonContentCachingRequestWrapper(request);
	ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
	filterChain.doFilter(requestWrapper, responseWrapper);
	long timecost = DateUtils.getTimeMillisConsume(begintime);
	String requestURI = request.getRequestURI();
	String ip = request.getRemoteAddr();
	String requestParam = convertString(requestWrapper.getContentAsByteArray());
	updateResponse(requestURI, responseWrapper);
	if (isNotJsonContentType(responseWrapper.getContentType())) {
		logger.debug("ip:{} 调用接口,请求地址为:{}, 请求参数为:{},[{}]ms", ip, requestURI, requestParam, timecost);
	} else {
		String result = convertString(responseWrapper.getContentAsByteArray());
		logger.debug("ip:{} 调用接口,请求地址为:{}, 请求参数为:{},返回值是{},[{}]ms", ip, requestURI, requestParam, result, timecost);
	}
}
 
开发者ID:wengwh,项目名称:plumdo-stock,代码行数:19,代码来源:RequestLogFilter.java


示例5: getResponseContent

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
private static String getResponseContent(HttpServletResponse response) {
    if (response instanceof ContentCachingResponseWrapper) {
        return new String(((ContentCachingResponseWrapper) response).getContentAsByteArray());
    }
    if (response instanceof HttpServletResponseWrapper
        && ((HttpServletResponseWrapper) response).getResponse() instanceof ContentCachingResponseWrapper) {
        return new String(((ContentCachingResponseWrapper) ((HttpServletResponseWrapper) response)
            .getResponse()).getContentAsByteArray());
    }
    log.warn("Empty response content because of unsupported response class {}", response.getClass());
    return "";
}
 
开发者ID:xm-online,项目名称:xm-commons,代码行数:13,代码来源:TimelineEventProducer.java


示例6: wrapResponseIfNeeded

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
/**
 * wrap the {@link HttpServletResponse} in a {@link ContentCachingResponseWrapper} if it is not already one.
 * 
 * @param httpServletResponse
 * @return
 */
protected ContentCachingResponseWrapper wrapResponseIfNeeded(HttpServletResponse httpServletResponse) {
	if (!(httpServletResponse instanceof ContentCachingResponseWrapper)) {
		return new ContentCachingResponseWrapper(httpServletResponse);
	} else {
		return (ContentCachingResponseWrapper) httpServletResponse;
	}
}
 
开发者ID:alecalanis,项目名称:session-to-cookie,代码行数:14,代码来源:SessionToCookieFilter.java


示例7: isContentCachingResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
protected boolean isContentCachingResponse(ServletResponse response) {
	if(response instanceof ContentCachingResponseWrapper){
		return true;
	}else if(response instanceof HttpServletResponseWrapper) {
		HttpServletResponseWrapper responseToUse = (HttpServletResponseWrapper) response;
		return isContentCachingResponse(responseToUse.getResponse());
	}
	return false;
}
 
开发者ID:penggle,项目名称:xproject,代码行数:10,代码来源:AbstractHttpAccessLoggingInterceptor.java


示例8: getContentCachingResponseWrapper

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
/**
 * 获取ContentCachingResponseWrapper
 * (考虑到Wrapper是可以重复的,故需要递归获取)
 * @param response
 * @return
 */
protected ContentCachingResponseWrapper getContentCachingResponseWrapper(ServletResponse response) {
	if(response instanceof ContentCachingResponseWrapper){
		return (ContentCachingResponseWrapper) response;
	}else if(response instanceof HttpServletResponseWrapper) {
		HttpServletResponseWrapper responseToUse = (HttpServletResponseWrapper) response;
		return getContentCachingResponseWrapper(responseToUse.getResponse());
	}
	return null;
}
 
开发者ID:penggle,项目名称:xproject,代码行数:16,代码来源:HttpAccessLoggingServletStreamFilter.java


示例9: doFilterInternal

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request,
                                HttpServletResponse response,
                                FilterChain filterChain) throws ServletException, IOException {
    ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
    ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
    try {
        filterChain.doFilter(requestWrapper, responseWrapper);
    } finally {
        responseWrapper.copyBodyToResponse();
    }
}
 
开发者ID:xm-online,项目名称:xm-ms-dashboard,代码行数:13,代码来源:ContentCachingWrappingFilter.java


示例10: doFilterInternal_NoCacheRequestAndResponse_CallWithCacheRequestAndResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void doFilterInternal_NoCacheRequestAndResponse_CallWithCacheRequestAndResponse() throws Exception {
	HttpServletRequest request = new MockHttpServletRequest();
	HttpServletResponse response = new MockHttpServletResponse();
	FilterChain filterChain = spy(new MockFilterChain());

	FILTER.doFilterInternal(request, response, filterChain);

	Mockito.verify(filterChain).doFilter(isA(ContentCachingRequestWrapper.class), isA
			(ContentCachingResponseWrapper.class));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:12,代码来源:BaseLoggingFilterTest.java


示例11: doFilterInternal_CacheRequestAndNoCacheResponse_CallWithCacheRequestAndResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void doFilterInternal_CacheRequestAndNoCacheResponse_CallWithCacheRequestAndResponse() throws Exception {
	HttpServletRequest cacheRequest = new ContentCachingRequestWrapper(new MockHttpServletRequest());
	HttpServletResponse response = new MockHttpServletResponse();
	FilterChain filterChain = spy(new MockFilterChain());

	ArgumentCaptor<HttpServletRequest> requestCaptor = ArgumentCaptor.forClass(HttpServletRequest.class);

	FILTER.doFilterInternal(cacheRequest, response, filterChain);

	Mockito.verify(filterChain).doFilter(requestCaptor.capture(), isA(ContentCachingResponseWrapper.class));
	assertThat(requestCaptor.getValue(), is(cacheRequest));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:14,代码来源:BaseLoggingFilterTest.java


示例12: doFilterInternal_NoCacheRequestAndCacheResponse_CallWithCacheRequestAndResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void doFilterInternal_NoCacheRequestAndCacheResponse_CallWithCacheRequestAndResponse() throws Exception {
	HttpServletRequest request = new MockHttpServletRequest();
	HttpServletResponse cacheResponse = new ContentCachingResponseWrapper(new MockHttpServletResponse());
	FilterChain filterChain = spy(new MockFilterChain());

	ArgumentCaptor<HttpServletResponse> responseCaptor = ArgumentCaptor.forClass(HttpServletResponse.class);

	FILTER.doFilterInternal(request, cacheResponse, filterChain);

	Mockito.verify(filterChain).doFilter(isA(ContentCachingRequestWrapper.class), responseCaptor.capture());
	assertThat(responseCaptor.getValue(), is(cacheResponse));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:14,代码来源:BaseLoggingFilterTest.java


示例13: buildResponseLog_WithNoBody_ReturnStringResponseWithUriAndEmptyBody

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void buildResponseLog_WithNoBody_ReturnStringResponseWithUriAndEmptyBody() {
	MockHttpServletResponse response = new MockHttpServletResponse();

	String log = FILTER.buildResponseLog(new ContentCachingResponseWrapper(response));

	assertThat(log, is("Server responded with a response\n > 200\n[empty]"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:9,代码来源:BaseLoggingFilterTest.java


示例14: buildResponseLog_WithCorrectBody_ReturnStringResponseWithUriAndBody

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void buildResponseLog_WithCorrectBody_ReturnStringResponseWithUriAndBody() throws IOException {
	MockHttpServletResponse response = new MockHttpServletResponse();

	ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
	responseWrapper.getWriter().print("test");

	String log = FILTER.buildResponseLog(responseWrapper);

	assertThat(log, is("Server responded with a response\n > 200\ntest"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:12,代码来源:BaseLoggingFilterTest.java


示例15: buildResponseLog_WithUnreadableBody_ReturnStringResponseWithUriAndUnknownBody

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Test
public void buildResponseLog_WithUnreadableBody_ReturnStringResponseWithUriAndUnknownBody() throws IOException {
	MockHttpServletResponse response = new MockHttpServletResponse();

	ContentCachingResponseWrapper responseWrapper = spy(new ContentCachingResponseWrapper(response));
	responseWrapper.getWriter().print("test");
	//noinspection InjectedReferences
	responseWrapper.setCharacterEncoding("notExistThisEncoding");

	String log = FILTER.buildResponseLog(responseWrapper);

	assertThat(log, is("Server responded with a response\n > 200\n[unknown]"));
}
 
开发者ID:cmateosl,项目名称:role-api,代码行数:14,代码来源:BaseLoggingFilterTest.java


示例16: appendResponseCommonMessage

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
void appendResponseCommonMessage(ContentCachingResponseWrapper response, long cost) {
  normalMsg.append(RESPONSE_PREFIX);
  StringBuilder msg = new StringBuilder();
  msg.append("status=").append(response.getStatusCode());
  msg.append(";size=").append(response.getContentSize());
  msg.append(";cost=").append(cost);
  normalMsg.append(msg.toString());
}
 
开发者ID:lord-of-code,项目名称:loc-framework,代码行数:9,代码来源:LocAccessLogger.java


示例17: doFilterInternal

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
		throws ServletException, IOException {

	HttpServletResponse responseToUse = response;
	if (!isAsyncDispatch(request) && !(response instanceof ContentCachingResponseWrapper)) {
		responseToUse = new HttpStreamingAwareContentCachingResponseWrapper(response, request);
	}

	filterChain.doFilter(request, responseToUse);

	if (!isAsyncStarted(request) && !isContentCachingDisabled(request)) {
		updateResponse(request, responseToUse);
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:16,代码来源:ShallowEtagHeaderFilter.java


示例18: updateResponse

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
private void updateResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
	ContentCachingResponseWrapper responseWrapper =
			WebUtils.getNativeResponse(response, ContentCachingResponseWrapper.class);
	Assert.notNull(responseWrapper, "ContentCachingResponseWrapper not found");
	HttpServletResponse rawResponse = (HttpServletResponse) responseWrapper.getResponse();
	int statusCode = responseWrapper.getStatusCode();

	if (rawResponse.isCommitted()) {
		responseWrapper.copyBodyToResponse();
	}
	else if (isEligibleForEtag(request, responseWrapper, statusCode, responseWrapper.getContentInputStream())) {
		String responseETag = generateETagHeaderValue(responseWrapper.getContentInputStream());
		rawResponse.setHeader(HEADER_ETAG, responseETag);
		String requestETag = request.getHeader(HEADER_IF_NONE_MATCH);
		if (responseETag.equals(requestETag)) {
			if (logger.isTraceEnabled()) {
				logger.trace("ETag [" + responseETag + "] equal to If-None-Match, sending 304");
			}
			rawResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
		}
		else {
			if (logger.isTraceEnabled()) {
				logger.trace("ETag [" + responseETag + "] not equal to If-None-Match [" + requestETag +
						"], sending normal response");
			}
			responseWrapper.copyBodyToResponse();
		}
	}
	else {
		if (logger.isTraceEnabled()) {
			logger.trace("Response with status code [" + statusCode + "] not eligible for ETag");
		}
		responseWrapper.copyBodyToResponse();
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:36,代码来源:ShallowEtagHeaderFilter.java


示例19: doFilter

import org.springframework.web.util.ContentCachingResponseWrapper; //导入依赖的package包/类
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException {
    if (!(request instanceof HttpServletRequest)) {
        return;
    }
    ContentCachingRequestWrapper wrapperRequest = new ContentCachingRequestWrapper((HttpServletRequest) request);
    ContentCachingResponseWrapper wrapperResponse = new ContentCachingResponseWrapper((HttpServletResponse)
            response);
    String ua = wrapperRequest.getHeader("User-Agent");
    ua = null == ua ? "" : ua;
    if (ua.equals("KeepAliveClient") || HttpMethod.HEAD.matches(wrapperRequest.getMethod())) {
        chain.doFilter(wrapperRequest, wrapperResponse);
        wrapperResponse.copyBodyToResponse();
    } else {
        ApplicationContext context = ACU.ctx();
        logFilterInterceptorList.forEach(LogFilterInterceptor::prepare);
        StringBuilder accessLogBuilder = new StringBuilder();
        long startTime = Instant.now().toEpochMilli();
        addLogKV(accessLogBuilder, "start", String.valueOf(startTime));
        chain.doFilter(wrapperRequest, wrapperResponse);
        addLogKV(accessLogBuilder, "status", String.valueOf(wrapperResponse.getStatusCode()));
        RequestAttributes requestAttributes = (RequestAttributes) context.getBean("requestAttributes");
        if (null != requestAttributes.getDeviceToken()) {
            addLogKV(accessLogBuilder, "vd", (String) requestAttributes.getDeviceToken());
        }
        if (null != requestAttributes.getLoginUser()) {
            addLogKV(accessLogBuilder, "user", String.valueOf(requestAttributes.getLoginUser().getId()));
            addLogKV(accessLogBuilder, "token", requestAttributes.getLoginUser().getToken());
            addLogKV(accessLogBuilder, "tokenExpireTime", requestAttributes.getLoginUser().getTokenExpireTime().toString
                    ());

        }
        addLogKV(accessLogBuilder, "locale", RequestContextUtils.getLocale(wrapperRequest).toLanguageTag());
        String remoteIp = requestAttributes.getRemoteIp();
        addLogKV(accessLogBuilder, "prevIp", wrapperRequest.getRemoteAddr());
        addLogKV(accessLogBuilder, "remoteIp", remoteIp);
        addLogKV(accessLogBuilder, "request", wrapperRequest.getRequestURI());
        addLogKV(accessLogBuilder, "method", wrapperRequest.getMethod());
        addLogKV(accessLogBuilder, "params", parseParams(wrapperRequest.getParameterMap()));
        addLogKV(accessLogBuilder, "ua", wrapperRequest.getHeader("User-Agent"));
        if ("POST".equalsIgnoreCase(wrapperRequest.getMethod())) {
            byte[] content = wrapperRequest.getContentAsByteArray();
            if (content.length < 1) {
                content = StreamUtils.copyToByteArray(wrapperRequest.getInputStream());
            }
            addLogKV(accessLogBuilder, "body", IOUtils.toString(content, "UTF-8").replaceAll("\\r\\n", "")
                    .replaceAll
                            ("\\n", ""));
        }
        if (null != wrapperResponse.getContentType() && (wrapperResponse.getContentType().equals(MediaType
                .APPLICATION_JSON_UTF8_VALUE) || wrapperResponse.getContentType().equals(MediaType
                .APPLICATION_JSON_VALUE))) {
            addLogKV(accessLogBuilder, "response", IOUtils.toString(wrapperResponse.getContentAsByteArray(),
                    "UTF-8"));
        }
        logFilterInterceptorList.forEach(logFilterInterceptor -> addLogKV(accessLogBuilder, logFilterInterceptor
                .getKey(), logFilterInterceptor.getOutput()));
        long endTime = Instant.now().toEpochMilli();
        addLogKV(accessLogBuilder, "duration", String.valueOf(endTime - startTime) + "ms");
        accessLogger.info(accessLogBuilder.toString());
        wrapperResponse.copyBodyToResponse();
    }
}
 
开发者ID:superkoh,项目名称:k-framework,代码行数:65,代码来源:LogFilter.java



注:本文中的org.springframework.web.util.ContentCachingResponseWrapper类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java LuaStateFactory类代码示例发布时间:2022-05-22
下一篇:
Java BpmnError类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap