本文整理汇总了Java中org.springframework.web.util.ContentCachingRequestWrapper类的典型用法代码示例。如果您正苦于以下问题:Java ContentCachingRequestWrapper类的具体用法?Java ContentCachingRequestWrapper怎么用?Java ContentCachingRequestWrapper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ContentCachingRequestWrapper类属于org.springframework.web.util包,在下文中一共展示了ContentCachingRequestWrapper类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: doFilterInternal
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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: extractRequestPayload
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
private String extractRequestPayload(HttpServletRequest request) {
ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper == null) {
return UNKNOWN_PAYLOAD;
}
if (wrapper.getContentLength() > 0) {
try {
return request.getReader().lines().collect(Collectors.joining(StringUtils.LF));
} catch (IOException e) {
return UNKNOWN_PAYLOAD;
}
}
return EMPTY_PAYLOAD;
}
开发者ID:cmateosl,项目名称:role-api,代码行数:17,代码来源:BaseLoggingFilter.java
示例3: resolveArgument
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
@Override
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Object handler = webRequest.getAttribute(WebRequestConstants.HANDLER,
NativeWebRequest.SCOPE_REQUEST);
Class<?> type = inspector.getInputType(handler);
if (type == null) {
type = Object.class;
}
boolean message = inspector.isMessage(handler);
List<Object> body;
ContentCachingRequestWrapper nativeRequest = new ContentCachingRequestWrapper(
webRequest.getNativeRequest(HttpServletRequest.class));
if (logger.isDebugEnabled()) {
logger.debug("Resolving request body into type: " + type);
}
if (isPlainText(webRequest) && CharSequence.class.isAssignableFrom(type)) {
body = Arrays.asList(StreamUtils.copyToString(nativeRequest.getInputStream(),
Charset.forName("UTF-8")));
}
else {
try {
body = mapper.readValue(nativeRequest.getInputStream(),
mapper.getTypeFactory()
.constructCollectionLikeType(ArrayList.class, type));
}
catch (JsonMappingException e) {
nativeRequest.setAttribute(WebRequestConstants.INPUT_SINGLE, true);
body = Arrays.asList(
mapper.readValue(nativeRequest.getContentAsByteArray(), type));
}
}
if (message) {
List<Object> messages = new ArrayList<>();
for (Object payload : body) {
messages.add(MessageBuilder.withPayload(payload)
.copyHeaders(HeaderUtils.fromHttp(new ServletServerHttpRequest(
webRequest.getNativeRequest(HttpServletRequest.class))
.getHeaders()))
.build());
}
body = messages;
}
return new FluxRequest<Object>(body);
}
开发者ID:spring-cloud,项目名称:spring-cloud-function,代码行数:47,代码来源:FluxHandlerMethodArgumentResolver.java
示例4: getRequestContent
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
private static String getRequestContent(HttpServletRequest request) {
if (request instanceof ContentCachingRequestWrapper) {
return new String(((ContentCachingRequestWrapper) request).getContentAsByteArray());
}
if (request instanceof HttpServletRequestWrapper
&& ((HttpServletRequestWrapper) request).getRequest() instanceof ContentCachingRequestWrapper) {
return new String(((ContentCachingRequestWrapper) ((HttpServletRequestWrapper) request)
.getRequest()).getContentAsByteArray());
}
log.warn("Empty request content because of unsupported request class {}", request.getClass());
return "";
}
开发者ID:xm-online,项目名称:xm-commons,代码行数:13,代码来源:TimelineEventProducer.java
示例5: extractRequestParameter
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
/**
* 从HttpServletRequest中提取请求参数(包括请求体中的参数)
* @param request
* @param loggingContext
* @return
* @throws Exception
*/
protected HttpRequestParameter extractRequestParameter(HttpServletRequest request, LoggingContext loggingContext) throws Exception {
HttpRequestParameter parameter = new HttpRequestParameter();
Map<String,String[]> originalParamMap = request.getParameterMap();
Map<String,String> paramMap = new HashMap<String,String>();
if(originalParamMap != null && !originalParamMap.isEmpty()){
for(Map.Entry<String, String[]> entry : originalParamMap.entrySet()){
paramMap.put(entry.getKey(), getStringParameterValue(entry.getValue()));
}
}
parameter.setParameter(paramMap);
MediaType contentType = loggingContext.getHttpAccessLog().getRequestContentType();
if(contentType != null){
if(isContentCachingRequest(request)){
String charset = request.getCharacterEncoding();
if(StringUtils.isEmpty(charset)){
charset = StringUtils.defaultIfEmpty(contentType.getCharset().name(), GlobalConstants.SYSTEM_DEFAULT_CHARSET);
}
ContentCachingRequestWrapper requestToUse = (ContentCachingRequestWrapper) request;
byte[] bytes = requestToUse.getContentAsByteArray();
if(bytes != null){
String body = IOUtils.toString(bytes, charset);
Object bodyObj = body;
if(MediaType.APPLICATION_JSON.getType().equals(contentType.getType())){ //目前只处理JSON类型的数据
if(body.startsWith("[") && body.endsWith("]")){ //JSON Array String -> List<Map<String,Object>>
bodyObj = JsonUtils.json2Object(body, new TypeReference<List<Map<String,Object>>>(){});
}else if(body.startsWith("{") && body.endsWith("}")){ //JSON Object String -> Map<String,Object>
bodyObj = JsonUtils.json2Object(body, new TypeReference<Map<String,Object>>(){});
}
}
parameter.setBody(bodyObj);
}
}
}
return excludeRequestParameter(parameter, loggingContext);
}
开发者ID:penggle,项目名称:xproject,代码行数:44,代码来源:AbstractHttpAccessLoggingInterceptor.java
示例6: isContentCachingRequest
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
protected boolean isContentCachingRequest(ServletRequest request) {
if(request instanceof ContentCachingRequestWrapper){
return true;
}else if(request instanceof HttpServletRequestWrapper) {
HttpServletRequestWrapper requestToUse = (HttpServletRequestWrapper) request;
return isContentCachingRequest(requestToUse.getRequest());
}
return false;
}
开发者ID:penggle,项目名称:xproject,代码行数:10,代码来源:AbstractHttpAccessLoggingInterceptor.java
示例7: doFilterInternal
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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
示例8: doFilterInternal_NoCacheRequestAndResponse_CallWithCacheRequestAndResponse
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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
示例9: doFilterInternal_CacheRequestAndNoCacheResponse_CallWithCacheRequestAndResponse
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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
示例10: doFilterInternal_NoCacheRequestAndCacheResponse_CallWithCacheRequestAndResponse
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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
示例11: buildRequestLog_WithNoBody_ReturnStringRequestWithUriAndEmptyBody
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
@Test
public void buildRequestLog_WithNoBody_ReturnStringRequestWithUriAndEmptyBody() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
String log = FILTER.buildRequestLog(new ContentCachingRequestWrapper(request));
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n[empty]"));
}
开发者ID:cmateosl,项目名称:role-api,代码行数:11,代码来源:BaseLoggingFilterTest.java
示例12: buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
@Test
public void buildRequestLog_WithCorrectBody_ReturnStringRequestWithUriAndBody() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
request.setContent("test".getBytes());
String log = FILTER.buildRequestLog(new ContentCachingRequestWrapper(request));
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\ntest"));
}
开发者ID:cmateosl,项目名称:role-api,代码行数:12,代码来源:BaseLoggingFilterTest.java
示例13: buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的package包/类
@Test
public void buildRequestLog_WithUnreadableBody_ReturnStringRequestWithUriAndUnknownBody() throws IOException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod(HttpMethod.GET.name());
request.setRequestURI("/dummy");
request.setContent(nextBytes(4));
ContentCachingRequestWrapper requestWrapper = spy(new ContentCachingRequestWrapper(request));
when(requestWrapper.getReader()).thenThrow(new UnsupportedEncodingException());
String log = FILTER.buildRequestLog(requestWrapper);
assertThat(log, is("Server has received a request\n > GET http://localhost/dummy\n > Content-Length: 4\n[unknown]"));
}
开发者ID:cmateosl,项目名称:role-api,代码行数:15,代码来源:BaseLoggingFilterTest.java
示例14: doFilter
import org.springframework.web.util.ContentCachingRequestWrapper; //导入依赖的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.ContentCachingRequestWrapper类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论