javax.servlet.http.HttpServletRequestWrapper.<init>()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.8k)|赞(0)|评价(0)|浏览(156)

本文整理了Java中javax.servlet.http.HttpServletRequestWrapper.<init>()方法的一些代码示例,展示了HttpServletRequestWrapper.<init>()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpServletRequestWrapper.<init>()方法的具体详情如下:
包路径:javax.servlet.http.HttpServletRequestWrapper
类名称:HttpServletRequestWrapper
方法名:<init>

HttpServletRequestWrapper.<init>介绍

[英]Constructs a request object wrapping the given request.
[中]构造一个封装给定请求的请求对象。

代码示例

代码示例来源:origin: stackoverflow.com

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  chain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
    @Override
    public HttpSession getSession() {
      return null;
    }
    @Override
    public HttpSession getSession(boolean create) {
      return null;
    }
  }, response);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
      FilterChain filterChain) throws IOException, ServletException {
    request = new HttpServletRequestWrapper(request);
    filterChain.doFilter(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test  // SPR-13317
public void getBodyWithWrappedRequest() throws Exception {
  byte[] bytes = "content".getBytes("UTF-8");
  MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes);
  this.mockRequest.addFile(part);
  HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest);
  ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part");
  byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
  assertArrayEquals(bytes, result);
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void existingMultipartRequestButWrapped() throws Exception {
  MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
  request.addPreferredLocale(Locale.CANADA);
  request.addUserRole("role1");
  MockHttpServletResponse response = new MockHttpServletResponse();
  ComplexWebApplicationContext.MockMultipartResolver multipartResolver =
      (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext()
          .getBean("multipartResolver");
  MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
  complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
  multipartResolver.cleanupMultipart(multipartRequest);
  assertNull(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE));
  assertNotNull(request.getAttribute("cleanedUp"));
}

代码示例来源:origin: stackoverflow.com

public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
  final HttpServletRequest httpRequest = (HttpServletRequest) request;
  HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(httpRequest) {
    @Override
    public String getHeader(String name) {
      final String value = request.getParameter(name);
      if (value != null) {
        return value;
      }
      return super.getHeader(name);
    }
  };
  chain.doFilter(wrapper, response);
}

代码示例来源:origin: stackoverflow.com

public class DisableGetAuthFiler extends OncePerRequestFilter {
  ...

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    filterChain.doFilter(
        new HttpServletRequestWrapper(request) {
          @Override
          public String getParameter(String name) {
            if (("login".equals(name) && getQueryString().contains("login"))
                || ("password".equals(name) && getQueryString().contains("password"))) {
              return null;
            } else {
              return super.getParameter(name);
            }
          }
        },
        response
    );
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void decoratedNativeRequest() {
  HttpServletRequest decoratedRequest = new HttpServletRequestWrapper(servletRequest);
  HttpServletResponse decoratedResponse = new HttpServletResponseWrapper(servletResponse);
  ServletWebRequest request = new ServletWebRequest(decoratedRequest, decoratedResponse);
  assertSame(decoratedRequest, request.getNativeRequest());
  assertSame(decoratedRequest, request.getNativeRequest(ServletRequest.class));
  assertSame(decoratedRequest, request.getNativeRequest(HttpServletRequest.class));
  assertSame(servletRequest, request.getNativeRequest(MockHttpServletRequest.class));
  assertNull(request.getNativeRequest(MultipartRequest.class));
  assertSame(decoratedResponse, request.getNativeResponse());
  assertSame(decoratedResponse, request.getNativeResponse(ServletResponse.class));
  assertSame(decoratedResponse, request.getNativeResponse(HttpServletResponse.class));
  assertSame(servletResponse, request.getNativeResponse(MockHttpServletResponse.class));
  assertNull(request.getNativeResponse(MultipartRequest.class));
}

代码示例来源:origin: stackoverflow.com

return show2(model, new HttpServletRequestWrapper(request) {
  public StringBuffer getRequestURL() {
    return new StringBuffer(
      super.getRequestURL().toString().replaceFirst("/show1$", "/show2"));
  }
});

代码示例来源:origin: sixt/ja-micro

Map<String, String> headers = gatherHttpHeaders(req);
OrangeContext context = new OrangeContext(headers);
HttpServletRequest blubb = new HttpServletRequestWrapper(req);

代码示例来源:origin: stackoverflow.com

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

  HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper((HttpServletRequest) request) {

    @Override
    public Enumeration<String> getHeaderNames() {
      List<String> hdrs = Collections.list(super.getHeaderNames())
          .stream()
          .filter(h -> !h.equals(IF_MODIFIED_SINCE))
          .collect(Collectors.toList());

      return Collections.enumeration(hdrs);
    }
  };
  chain.doFilter(wrapper, response);
}

代码示例来源:origin: stackoverflow.com

@Override
 public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
   HttpServletRequest request = (HttpServletRequest) servletRequest;
   final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
     @Override
     public StringBuffer getRequestURL() {
       final StringBuffer originalUrl = ((HttpServletRequest) getRequest()).getRequestURL();
       final String updatedUrl = originalUrl.toString().replace("http://", "https://");
       return new StringBuffer(updatedUrl);
     }
   };
   filterChain.doFilter(wrapped, servletResponse);
 }

代码示例来源:origin: stackoverflow.com

public void testSomething() {
  myServlet.doGet(createMockRequest(), createMockResponse());
}

protected HttpServletRequest createMockRequest() {
  HttpServletRequest request = new HttpServletRequestWrapper() {
    //overrided methods   
  }
}

代码示例来源:origin: stackoverflow.com

httpServletRequest = new HttpServletRequestWrapper(httpServletRequest)

代码示例来源:origin: caelum/vraptor4

/** 
 * @deprecated CDI eyes only
 */
protected HttpServletRequestMock() {
}

代码示例来源:origin: stackoverflow.com

private static HttpServletRequest wrapRequest(
     HttpServletRequest request, final Map<String, String[]> parameterMap)
 {
   return new HttpServletRequestWrapper(request)
   {
     @Override
     public Map<String, String[]> getParameterMap()
     {
       return parameterMap;
     }
     public String[] getParameterValues(String name)
     {
       return parameterMap.get(name);
     }
     public String getParameter(String name)
     {
       String[] params = getParameterValues(name);
       return params != null && params.length > 0 ? params[0] : null;
     }
     public Enumeration<String> getParameterNames()
     {
       return Collections.enumeration(parameterMap.keySet());
     }
   };
 }

代码示例来源:origin: smartthings/dropwizard-logging

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  throws IOException, ServletException {
  String requestId = null;
  if (request instanceof HttpServletRequest) {
    HttpServletRequestWrapper req = new HttpServletRequestWrapper((HttpServletRequest) request);
    requestId = req.getHeader(LoggingContext.CORRELATION_ID_HEADER);
    Optional.ofNullable(req.getHeader(LoggingContext.LOG_LEVEL_HEADER))
      .map(String::toUpperCase)
      .filter(logLevelStr -> Arrays.stream(allLevels).anyMatch(level -> level.toString().equals(logLevelStr)))
      .ifPresent(logLevel -> MDC.put(LoggingContext.DYNAMIC_LOG_LEVEL, logLevel));
  }
  if (requestId == null || requestId.isEmpty()) {
    requestId = UUID.randomUUID().toString();
  }
  MDC.put(LoggingContext.LOGGING_ID, requestId);
  try {
    chain.doFilter(request, response);
  } finally {
    MDC.remove(LoggingContext.LOGGING_ID);
    MDC.remove(LoggingContext.DYNAMIC_LOG_LEVEL);
  }
}

代码示例来源:origin: OneBusAway/onebusaway-application-modules

@Override
public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {
  HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper((HttpServletRequest)request);
  
  String url = wrapper.getRequestURL().toString();
  if(url.contains(URL_LEGACY_SIGN_PATH)) {
    String queryString = wrapper.getQueryString().toString();
    
    if(queryString.contains(PARAM_LEGACY_STOP_ID)){
      queryString = StringUtils.replace(queryString, PARAM_LEGACY_STOP_ID, PARAM_STOP_ID);
    }
    if(queryString.contains(PARAM_LEGACY_ROUTE_ID)){
      queryString = StringUtils.replace(queryString, PARAM_LEGACY_ROUTE_ID, PARAM_ROUTE_ID);
    }
    final String filtered = wrapper.getContextPath() + URL_SIGN_PATH + "?" + queryString;
    
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    httpResponse.sendRedirect(filtered);
  } else {
    chain.doFilter(wrapper, response);
  }
  
}

代码示例来源:origin: com.github.wuic/wuic-servlet

/**
 * <p>
 * Includes the resources at given path in the specified request and response.
 * </p>
 *
 * @param path the path
 * @param response the response
 * @param request the request
 * @throws IOException if include fails
 */
private void include(final String path, final HttpServletRequest request, final HttpServletResponse response)
    throws IOException {
  try {
    final RequestDispatcher rd = servletContext.getRequestDispatcher(slashPath(path));
    // Wrap request and response since servlet container expects standard wrappers
    rd.include(new HttpServletRequestWrapper(request), new HttpServletResponseWrapper(response));
  } catch (ServletException se) {
    WuicException.throwStreamException(new IOException(se));
  }
}

代码示例来源:origin: stackoverflow.com

public class JobInterceptor extends HandlerInterceptorAdapter {
private static final String requestLocations[]={"rt", "jobType"};
private boolean isEmpty(String arg)
{
 return (arg !=null && arg.length() > 0);
}
public boolean preHandle(HttpServletRequest request,
 HttpServletResponse response, Object handler) throws Exception {
 //Maybe something like this
 if(!isEmpty(request.getParameter(requestLocations[0]))|| !isEmpty(request.getParameter(requestLocations[1]))
 {
 final String value =
   !isEmpty(request.getParameter(requestLocations[0])) ? request.getParameter(requestLocations[0]) : !isEmpty(request
   .getParameter(requestLocations[1])) ? request.getParameter(requestLocations[1]) : null;
 HttpServletRequest wrapper = new HttpServletRequestWrapper(request)
 {
  public String getParameter(String name)
  {
  super.getParameterMap().put("JobType", value);
  return super.getParameter(name);
  }
 };
 //Accepted request - Handler should carry on.
 return super.preHandle(request, response, handler);
 }
 //Ignore request if above condition was false
 return false;
 }
}

代码示例来源:origin: com.github.wuic/wuic-servlet

/**
 * <p>
 * Extracts the {@link HttpServletRequest} from the given {@link ProcessContext} if it's a {@link ServletProcessContext}.
 * Otherwise, an adapter wrapping the specified path will be returned.
 * </p>
 *
 * @param path the path of the adapter
 * @param processContext the potential instance wrapping the {@link HttpServletRequest}
 * @return the resolved {@link HttpServletRequest}
 */
private HttpServletRequest getRequest(final String path, final ProcessContext processContext) {
  final HttpServletRequest retval;
  if (processContext instanceof ServletProcessContext) {
    retval = new HttpServletRequestWrapper(ServletProcessContext.class.cast(processContext).getHttpServletRequest()) {
      /**
       * {@inheritDoc}
       */
      @Override
      public String getPathInfo() {
        return path;
      }
    };
  } else {
    retval = new HttpServletRequestAdapter(slashPath(path));
    logger.warn(WARN_MOCK_REQUEST_MESSAGE, processContext);
  }
  retval.setAttribute(HtmlParserFilter.SKIP_FILTER, "");
  return retval;
}

相关文章

HttpServletRequestWrapper类方法