當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpServletRequestWrapper類代碼示例

本文整理匯總了Java中javax.servlet.http.HttpServletRequestWrapper的典型用法代碼示例。如果您正苦於以下問題:Java HttpServletRequestWrapper類的具體用法?Java HttpServletRequestWrapper怎麽用?Java HttpServletRequestWrapper使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


HttpServletRequestWrapper類屬於javax.servlet.http包,在下文中一共展示了HttpServletRequestWrapper類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doGet

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
/**
 * Rewrites all request without a file extension to <code>/index.html</code> and returns it
 * to the {@link DefaultServlet}. This is necessary because for URLs which will be handled
 * in Angular.js. It requires that Angular.js is loaded and that is by returning the content
 * of the index.html.
 */
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response)
		throws ServletException, IOException {
	final HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {
		@Override
		public String getServletPath() {
			return "";
		}

		@Override
		public String getRequestURI() {
			return "";
		}

		@Override
		public String getPathInfo() {
			return super.getPathInfo().matches(".*\\/[^\\/]*\\.[^\\/]*$") ? super.getPathInfo() : "/index.html";
		}
	};

	final RequestDispatcher dispatcher = getServletContext().getNamedDispatcher("default");
	dispatcher.forward(wrapper, response);
}
 
開發者ID:XMBomb,項目名稱:InComb,代碼行數:30,代碼來源:InCombServlet.java

示例2: doFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request,
                     ServletResponse response,
                     FilterChain chain
                     ) throws IOException, ServletException {
  HttpServletRequestWrapper quoted =
    new RequestQuoter((HttpServletRequest) request);
  HttpServletResponse httpResponse = (HttpServletResponse) response;

  String mime = inferMimeType(request);
  if (mime == null) {
    httpResponse.setContentType("text/plain; charset=utf-8");
  } else if (mime.startsWith("text/html")) {
    // HTML with unspecified encoding, we want to
    // force HTML with utf-8 encoding
    // This is to avoid the following security issue:
    // http://openmya.hacker.jp/hasegawa/security/utf7cs.html
    httpResponse.setContentType("text/html; charset=utf-8");
  } else if (mime.startsWith("application/xml")) {
    httpResponse.setContentType("text/xml; charset=utf-8");
  }
  chain.doFilter(quoted, httpResponse);
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:24,代碼來源:HttpServer2.java

示例3: doFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain
                     ) throws IOException, ServletException {
  HttpServletRequest httpRequest = (HttpServletRequest) request;
  // if the user is already authenticated, don't override it
  if (httpRequest.getRemoteUser() != null) {
    chain.doFilter(request, response);
  } else {
    HttpServletRequestWrapper wrapper = 
        new HttpServletRequestWrapper(httpRequest) {
      @Override
      public Principal getUserPrincipal() {
        return user;
      }
      @Override
      public String getRemoteUser() {
        return username;
      }
    };
    chain.doFilter(wrapper, response);
  }
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:24,代碼來源:StaticUserWebFilter.java

示例4: testFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Test
public void testFilter() throws Exception {
  FilterConfig config = mockConfig("myuser");
  StaticUserFilter suf = new StaticUserFilter();
  suf.init(config);
  
  ArgumentCaptor<HttpServletRequestWrapper> wrapperArg =
    ArgumentCaptor.forClass(HttpServletRequestWrapper.class);

  FilterChain chain = mock(FilterChain.class);
  
  suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class),
      chain);
      
  Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject());
  
  HttpServletRequestWrapper wrapper = wrapperArg.getValue();
  assertEquals("myuser", wrapper.getUserPrincipal().getName());
  assertEquals("myuser", wrapper.getRemoteUser());
  
  suf.destroy();
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:23,代碼來源:TestStaticUserWebFilter.java

示例5: wrapRequest

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
private HttpServletRequest wrapRequest(final HttpServletRequest request, boolean jukebox) {
    final String playerId = createPlayerIfNecessary(request, jukebox);
    return new HttpServletRequestWrapper(request) {
        @Override
        public String getParameter(String name) {
            // Returns the correct player to be used in PlayerService.getPlayer()
            if ("player".equals(name)) {
                return playerId;
            }

            // Support old style ID parameters.
            if ("id".equals(name)) {
                return mapId(request.getParameter("id"));
            }

            return super.getParameter(name);
        }
    };
}
 
開發者ID:airsonic,項目名稱:airsonic,代碼行數:20,代碼來源:SubsonicRESTController.java

示例6: doFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response,
		FilterChain chain) throws IOException, ServletException {
	chain.doFilter(new HttpServletRequestWrapper(
			(HttpServletRequest) request) {
		@Override
		public String getRequestURI() {
			try {
				return URLDecoder.decode(super.getRequestURI(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				throw new IllegalStateException(
						"Cannot decode request URI.", e);
			}
		}
	}, response);

}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:18,代碼來源:RichFacesFirefox11Filter.java

示例7: doFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, 
                     ServletResponse response,
                     FilterChain chain
                     ) throws IOException, ServletException {
  HttpServletRequestWrapper quoted = 
    new RequestQuoter((HttpServletRequest) request);
  HttpServletResponse httpResponse = (HttpServletResponse) response;

  String mime = inferMimeType(request);
  if (mime == null) {
    httpResponse.setContentType("text/plain; charset=utf-8");
  } else if (mime.startsWith("text/html")) {
    // HTML with unspecified encoding, we want to
    // force HTML with utf-8 encoding
    // This is to avoid the following security issue:
    // http://openmya.hacker.jp/hasegawa/security/utf7cs.html
    httpResponse.setContentType("text/html; charset=utf-8");
  } else if (mime.startsWith("application/xml")) {
    httpResponse.setContentType("text/xml; charset=utf-8");
  }
  chain.doFilter(quoted, httpResponse);
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:24,代碼來源:HttpServer.java

示例8: getWrappedHttpServletRequest

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
private static HttpServletRequest getWrappedHttpServletRequest(HttpServletRequest request) throws IOException
{
    //TODO is it really necessary to wrap the request into a BufferedInputStream?
    // If not, then we could remove the check for multipart upload. 
    // The check is needed as we get an IOException (Resetting to invalid mark) for files more than 8193 bytes. 
    boolean resetSupported = true;
    String contentType = request.getHeader(HEADER_CONTENT_TYPE);
    if (contentType != null && contentType.startsWith(MULTIPART_FORM_DATA))
    {
       resetSupported = false;
    }
    final PublicApiServletInputStream sis = new PublicApiServletInputStream(request.getInputStream(), resetSupported);
    HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request)
    {
        public ServletInputStream getInputStream() throws java.io.IOException
        {
            return sis;
        }
    };
    return wrapper;
}
 
開發者ID:Alfresco,項目名稱:alfresco-remote-api,代碼行數:22,代碼來源:PublicApiHttpServletRequest.java

示例9: run

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
public Object run() {
	try {
		RequestContext context = getCurrentContext();
		InputStream in = (InputStream) context.get("requestEntity");
		if (in == null) {
			in = context.getRequest().getInputStream();
		}
		String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
		body = "request body modified via request wrapper: "+ body;
		byte[] bytes = body.getBytes("UTF-8");
		context.setRequest(new HttpServletRequestWrapper(getCurrentContext().getRequest()) {
			@Override
			public ServletInputStream getInputStream() throws IOException {
				return new ServletInputStreamWrapper(bytes);
			}

			@Override
			public int getContentLength() {
				return bytes.length;
			}

			@Override
			public long getContentLengthLong() {
				return bytes.length;
			}
		});
	}
	catch (IOException e) {
		rethrowRuntimeException(e);
	}
	return null;
}
 
開發者ID:spring-cloud-samples,項目名稱:sample-zuul-filters,代碼行數:33,代碼來源:PrefixRequestEntityFilter.java

示例10: testNewSessionIdCreatedIfSessionWasInvalidated

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Test
public void testNewSessionIdCreatedIfSessionWasInvalidated() {
  HttpServletRequest wrappedSimple = mock(HttpServletRequest.class);
  RepositoryBackedHttpSession invalidSession = mock(RepositoryBackedHttpSession.class);
  RepositoryBackedHttpSession newSession = mock(RepositoryBackedHttpSession.class);
  when(newSession.getId()).thenReturn(NEW_SESSION_ID);
  when(invalidSession.getId()).thenReturn(SESSION_ID);
  when(invalidSession.isValid()).thenReturn(false);
  HttpRequestWrapper wrappedHttpRequestWrapper = spy(new HttpRequestWrapper(wrappedSimple, servletContext));
  wrappedHttpRequestWrapper.session = invalidSession;
  HttpServletRequest wrappedComplex = new HttpServletRequestWrapper(wrappedHttpRequestWrapper);
  HttpRequestWrapper req = new HttpRequestWrapper(wrappedComplex, servletContext);
  when(sessionManager.getSession(req, true, SESSION_ID)).thenReturn(invalidSession);
  when(sessionManager.getSession(req, true, null)).thenReturn(newSession);
  RepositoryBackedHttpSession session2 = req.getSession();
  Assert.assertNotNull(session2);
  assertEquals(NEW_SESSION_ID, session2.getId());
}
 
開發者ID:AmadeusITGroup,項目名稱:HttpSessionReplacer,代碼行數:19,代碼來源:TestHttpRequestWrapper.java

示例11: uploadFileToPart

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
/**
 * Test to upload a file to a part
 *
 * @throws Exception
 */
@Test
public void uploadFileToPart() throws Exception {
    //Given
    final File fileToUpload = new File(ResourceUtil.getFilePath(ResourceUtil.SOURCE_PART_STORAGE + ResourceUtil.TEST_PART_FILENAME1));
    File uploadedFile = File.createTempFile(ResourceUtil.TARGET_PART_STORAGE + ResourceUtil.FILENAME_TARGET_PART, ResourceUtil.TEMP_SUFFIX);
    HttpServletRequestWrapper request = Mockito.mock(HttpServletRequestWrapper.class);
    Collection<Part> parts = new ArrayList<>();
    parts.add(new PartImpl(fileToUpload));
    Mockito.when(request.getParts()).thenReturn(parts);
    BinaryResource binaryResource = new BinaryResource(ResourceUtil.FILENAME1, ResourceUtil.DOCUMENT_SIZE, new Date());
    OutputStream outputStream = new FileOutputStream(uploadedFile);
    Mockito.when(request.getRequestURI()).thenReturn(ResourceUtil.WORKSPACE_ID + "/parts/" + ResourceUtil.PART_TEMPLATE_ID + "/");
    Mockito.when(productService.saveFileInPartIteration(Matchers.any(PartIterationKey.class), Matchers.anyString(), Matchers.anyString(), Matchers.anyInt())).thenReturn(binaryResource);
    Mockito.when(storageManager.getBinaryResourceOutputStream(binaryResource)).thenReturn(outputStream);

    //When
    Response response = partBinaryResource.uploadAttachedFiles(request, ResourceUtil.WORKSPACE_ID, ResourceUtil.PART_NUMBER, ResourceUtil.VERSION, ResourceUtil.ITERATION);
    //Then
    assertNotNull(response);
    assertEquals(response.getStatus(), 201);
    assertEquals(response.getStatusInfo(), Response.Status.CREATED);
    //delete temp file
    uploadedFile.deleteOnExit();

}
 
開發者ID:polarsys,項目名稱:eplmp,代碼行數:31,代碼來源:PartBinaryResourceTest.java

示例12: decoratedNativeRequest

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@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));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:ServletWebRequestTests.java

示例13: doFilterInternal

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request,
		HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

	filterChain.doFilter(new HttpServletRequestWrapper(request) {
		@Override
		public Principal getUserPrincipal() {
			return new Principal() {
				@Override
				public String getName() {
					return PRINCIPAL_NAME;
				}
			};
		}
	}, new HttpServletResponseWrapper(response));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:FilterTests.java

示例14: findStripesWrapper

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
/**
 * Looks for the StripesRequesetWrapper for the specific request and returns it. This is done
 * by checking to see if the request is a StripesRequestWrapper, and if not, successively
 * unwrapping the request until the StripesRequestWrapper is found.
 *
 * @param request the ServletRequest that is wrapped by a StripesRequestWrapper
 * @return the StripesRequestWrapper that is wrapping the supplied request
 * @throws IllegalStateException if the request is not wrapped by Stripes
 */
public static StripesRequestWrapper findStripesWrapper(ServletRequest request) {
    // Loop through any request wrappers looking for the stripes one
    while ( !(request instanceof StripesRequestWrapper)
            && request != null
            && request instanceof HttpServletRequestWrapper) {
        request = ((HttpServletRequestWrapper) request).getRequest();
    }

    // If we have our wrapper after the loop exits, we're good; otherwise...
    if (request instanceof StripesRequestWrapper) {
        return (StripesRequestWrapper) request;
    }

    else {
        throw new IllegalStateException(String.format("Request %s made it through to some part of Stripes " +
            "without being wrapped in a StripesRequestWrapper. The StripesFilter is " +
            "responsible for wrapping the request, so it is likely that either the " +
            "StripesFilter is not deployed, or that its mappings do not include the " +
            "DispatcherServlet _and_ *.jsp. Stripes does not require that the Stripes " +
            "wrapper is the only request wrapper, or the outermost; only that it is present.",((HttpServletRequest)request).getRequestURL().toString()));
    }
}
 
開發者ID:nkasvosve,項目名稱:beyondj,代碼行數:32,代碼來源:StripesRequestWrapper.java

示例15: doFilter

import javax.servlet.http.HttpServletRequestWrapper; //導入依賴的package包/類
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    filterChain.doFilter(new HttpServletRequestWrapper((HttpServletRequest) request) {
        @Override
        public HttpSession getSession() {
            throw new UnsupportedOperationException("getSession() is not supported");
        }

        @Override
        public HttpSession getSession(boolean create) {
            if (create) {
                throw new UnsupportedOperationException("getSession(true) is not supported");
            }
            return null;
        }
    }, response);
}
 
開發者ID:AusDTO,項目名稱:spring-security-stateless,代碼行數:18,代碼來源:NoHttpSessionFilter.java


注:本文中的javax.servlet.http.HttpServletRequestWrapper類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。