List of usage examples for javax.servlet.http HttpServletRequest getRequestURL
public StringBuffer getRequestURL();
From source file:com.netflix.genie.web.controllers.JobRestControllerUnitTests.java
/** * Makes sure we can successfully forward a job kill request. * * @throws IOException on error//from w ww . ja v a 2s .c o m * @throws ServletException on error * @throws GenieException on error */ @Test public void canForwardJobKillRequest() throws IOException, ServletException, GenieException { this.jobsProperties.getForwarding().setEnabled(true); final String jobId = UUID.randomUUID().toString(); final String forwardedFrom = null; final HttpServletRequest request = Mockito.mock(HttpServletRequest.class); final HttpServletResponse response = Mockito.mock(HttpServletResponse.class); Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer(UUID.randomUUID().toString())); Mockito.when(this.jobSearchService.getJobHost(jobId)).thenReturn(UUID.randomUUID().toString()); final StatusLine statusLine = Mockito.mock(StatusLine.class); Mockito.when(statusLine.getStatusCode()).thenReturn(HttpStatus.ACCEPTED.value()); final HttpResponse forwardResponse = Mockito.mock(HttpResponse.class); Mockito.when(forwardResponse.getStatusLine()).thenReturn(statusLine); Mockito.when(forwardResponse.getAllHeaders()).thenReturn(new Header[0]); Mockito.when(this.restTemplate.execute(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyString())).thenReturn(null); this.controller.killJob(jobId, forwardedFrom, request, response); Mockito.verify(response, Mockito.never()).sendError(Mockito.anyInt(), Mockito.anyString()); Mockito.verify(this.jobSearchService, Mockito.times(1)).getJobHost(jobId); Mockito.verify(this.restTemplate, Mockito.times(1)).execute(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyString()); }
From source file:com.github.safrain.remotegsh.server.RgshFilter.java
private void performRgsh(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter()/*from w w w . j ava 2 s . co m*/ .println(getResource(RESOURCE_PATH + "rgsh.txt", DEFAULT_CHARSET) .replaceAll(PLACEHOLDER_SERVER, request.getRequestURL().toString()) .replaceAll(PLACEHOLDER_CHARSET, charset)); response.setStatus(200); }
From source file:com.github.safrain.remotegsh.server.RgshFilter.java
private void performInstall(HttpServletRequest request, HttpServletResponse response) throws IOException { response.getWriter()//from ww w . j a v a 2 s.co m .println(getResource(RESOURCE_PATH + "install.txt", DEFAULT_CHARSET) .replaceAll(PLACEHOLDER_SERVER, request.getRequestURL().toString()) .replaceAll(PLACEHOLDER_CHARSET, charset)); response.setStatus(200); }
From source file:org.apache.hadoop.gateway.dispatch.DefaultDispatchTest.java
@Test(timeout = TestUtils.SHORT_TIMEOUT) public void testGetDispatchUrl() throws Exception { HttpServletRequest request; Dispatch dispatch;/*from w w w . ja v a 2 s . c o m*/ String path; String query; URI uri; dispatch = new DefaultDispatch(); path = "http://test-host:42/test-path"; request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(request.getRequestURI()).andReturn(path).anyTimes(); EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer(path)).anyTimes(); EasyMock.expect(request.getQueryString()).andReturn(null).anyTimes(); EasyMock.replay(request); uri = dispatch.getDispatchUrl(request); assertThat(uri.toASCIIString(), is("http://test-host:42/test-path")); path = "http://test-host:42/test,path"; request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(request.getRequestURI()).andReturn(path).anyTimes(); EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer(path)).anyTimes(); EasyMock.expect(request.getQueryString()).andReturn(null).anyTimes(); EasyMock.replay(request); uri = dispatch.getDispatchUrl(request); assertThat(uri.toASCIIString(), is("http://test-host:42/test,path")); path = "http://test-host:42/test%2Cpath"; request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(request.getRequestURI()).andReturn(path).anyTimes(); EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer(path)).anyTimes(); EasyMock.expect(request.getQueryString()).andReturn(null).anyTimes(); EasyMock.replay(request); uri = dispatch.getDispatchUrl(request); assertThat(uri.toASCIIString(), is("http://test-host:42/test%2Cpath")); path = "http://test-host:42/test%2Cpath"; query = "test%26name=test%3Dvalue"; request = EasyMock.createNiceMock(HttpServletRequest.class); EasyMock.expect(request.getRequestURI()).andReturn(path).anyTimes(); EasyMock.expect(request.getRequestURL()).andReturn(new StringBuffer(path)).anyTimes(); EasyMock.expect(request.getQueryString()).andReturn(query).anyTimes(); EasyMock.replay(request); uri = dispatch.getDispatchUrl(request); assertThat(uri.toASCIIString(), is("http://test-host:42/test%2Cpath?test%26name=test%3Dvalue")); }
From source file:cec.easyshop.storefront.controllers.misc.StoreSessionController.java
protected String getReturnRedirectUrlForUrlEncoding(final HttpServletRequest request, final String old, final String current) { final String originalReferer = (String) request.getSession() .getAttribute(StorefrontFilter.ORIGINAL_REFERER); if (StringUtils.isNotBlank(originalReferer)) { return REDIRECT_PREFIX + StringUtils.replace(originalReferer, "/" + old + "/", "/" + current + "/"); }//from w w w . j av a 2s . c om String referer = StringUtils.remove(request.getRequestURL().toString(), request.getServletPath()); if (!StringUtils.endsWith(referer, "/")) { referer = referer + "/"; } if (referer != null && !referer.isEmpty() && StringUtils.contains(referer, "/" + old + "/")) { return REDIRECT_PREFIX + StringUtils.replace(referer, "/" + old + "/", "/" + current + "/"); } return REDIRECT_PREFIX + referer; }
From source file:com.sg.rest.filters.LoggerFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (!(request instanceof HttpServletRequest)) { LOGGER.error(NON_HTTP_REQUEST + System.lineSeparator() + request.getInputStream().toString()); throw new RuntimeException(EXPECTING_AN_HTTP_REQUEST); }/*from w w w .j av a 2 s .c o m*/ HttpServletRequest httpRequest = (HttpServletRequest) request; StringBuilder sb = new StringBuilder(); //General header sb.append(System.lineSeparator()); sb.append(INCOMING_REQUEST); //Request url sb.append(System.lineSeparator()); sb.append(REQUEST_URL); sb.append(httpRequest.getRequestURL()); //Method sb.append(System.lineSeparator()); sb.append(METHOD); sb.append(httpRequest.getMethod()); //Parameters if (httpRequest.getParameterNames().hasMoreElements()) { sb.append(System.lineSeparator()); sb.append(PARAMETERS); Enumeration enParams = httpRequest.getParameterNames(); while (enParams.hasMoreElements()) { sb.append(System.lineSeparator()); String paramName = (String) enParams.nextElement(); sb.append(paramName); sb.append(" : "); sb.append(httpRequest.getParameter(paramName)); } } //Attributes if (httpRequest.getAttributeNames().hasMoreElements()) { sb.append(System.lineSeparator()); sb.append(ATTRIBUTES); Enumeration enAttribs = httpRequest.getAttributeNames(); while (enAttribs.hasMoreElements()) { sb.append(System.lineSeparator()); String attribName = (String) enAttribs.nextElement(); sb.append(attribName); sb.append(" : "); sb.append(httpRequest.getAttribute(attribName)); } } //Headers if (httpRequest.getHeaderNames().hasMoreElements()) { sb.append(System.lineSeparator()); sb.append(HEADERS); Enumeration enHeaders = httpRequest.getHeaderNames(); while (enHeaders.hasMoreElements()) { sb.append(System.lineSeparator()); String headerName = (String) enHeaders.nextElement(); sb.append(headerName); sb.append(" : "); sb.append(httpRequest.getHeader(headerName)); } } //AuthType if (httpRequest.getAuthType() != null && !httpRequest.getAuthType().isEmpty()) { sb.append(System.lineSeparator()); sb.append(AUTH_TYPE); sb.append(httpRequest.getAuthType()); } //Cookies if (httpRequest.getCookies() != null && httpRequest.getCookies().length > 0) { sb.append(System.lineSeparator()); sb.append(COOKIES); for (Cookie cookie : httpRequest.getCookies()) { sb.append(System.lineSeparator()); sb.append(cookie.getName()); sb.append(" : "); sb.append(cookie.getValue()); } } //RemoteAddr if (httpRequest.getRemoteAddr() != null && !httpRequest.getRemoteAddr().isEmpty()) { sb.append(System.lineSeparator()); sb.append(REMOTE_ADDR); sb.append(httpRequest.getRemoteAddr()); } //RemoteHost if (httpRequest.getRemoteHost() != null && !httpRequest.getRemoteHost().isEmpty()) { sb.append(System.lineSeparator()); sb.append(REMOTE_HOST); sb.append(httpRequest.getRemoteHost()); } //User principal if (httpRequest.getUserPrincipal() != null) { if (httpRequest.getUserPrincipal().getName() != null && !httpRequest.getUserPrincipal().getName().isEmpty()) { sb.append(System.lineSeparator()); sb.append(PRINCIPAL); sb.append(httpRequest.getUserPrincipal().getName()); } } //Body ResettableStreamHttpServletRequest wrappedRequest = new ResettableStreamHttpServletRequest( (HttpServletRequest) request); String body = IOUtils.toString(wrappedRequest.getReader()); if (body != null && !body.isEmpty()) { sb.append(System.lineSeparator()); sb.append(BODY); sb.append(System.lineSeparator()); sb.append(body); } wrappedRequest.resetInputStream(); LOGGER.info(sb.toString()); chain.doFilter(wrappedRequest, response); }
From source file:com.ravens.filters.GlobalDefaultExceptionHandler.java
@ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it - like the OrderNotFoundException example // at the start of this post. // AnnotationUtils is a Spring Framework utility class. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e;/* w w w. j a va 2 s .c om*/ // Otherwise setup and send the user to a default error-view. ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); e.printStackTrace(); return mav; }
From source file:org.slc.sli.dashboard.security.SLIAuthenticationEntryPoint.java
private void initiatingAuthentication(HttpServletRequest request, HttpServletResponse response, HttpSession session, OAuthService service) throws IOException { LOG.info(LOG_MESSAGE_AUTH_INITIATING, new Object[] { request.getRemoteAddr() }); session.setAttribute(ENTRY_URL, request.getRequestURL()); // The request token doesn't matter for OAuth 2.0 which is why it's null String authUrl = service.getAuthorizationUrl(null); response.sendRedirect(authUrl);/*from w w w . ja va 2s.com*/ }
From source file:kornell.server.ProxyServlet.java
/** For a redirect response from the target server, this translates {@code theUrl} to redirect to * and translates it to one the original client can use. */ protected String rewriteUrlFromResponse(HttpServletRequest servletRequest, String theUrl) { //TODO document example paths if (theUrl.startsWith(targetUri)) { String curUrl = servletRequest.getRequestURL().toString();//no query String pathInfo = servletRequest.getPathInfo(); if (pathInfo != null) { assert curUrl.endsWith(pathInfo); curUrl = curUrl.substring(0, curUrl.length() - pathInfo.length());//take pathInfo off }/*from w ww .j a v a2 s . co m*/ theUrl = curUrl + theUrl.substring(targetUri.length()); } return theUrl; }
From source file:pt.webdetails.browserid.spring.BrowserIdProcessingFilter.java
/** * //from w w w .ja v a 2 s . co m */ @Override public Authentication attemptAuthentication(HttpServletRequest request) throws AuthenticationException { String browserIdAssertion = request.getParameter(getAssertionParameterName()); // String assertionAudience = request.getParameter(getAudienceParameterName()); if (browserIdAssertion != null) { BrowserIdVerifier verifier = new BrowserIdVerifier(getVerificationServiceUrl()); BrowserIdResponse response = null; String audience = request.getRequestURL().toString(); try { URL url = new URL(audience); audience = url.getHost(); } catch (MalformedURLException e) { throw new BrowserIdAuthenticationException("Malformed request URL", e); } // Assert.hasLength("Unable to determine hostname",audience); // if(!StringUtils.equals(audience, assertionAudience)){ // logger.error("Server and client-side audience don't match"); // } try { response = verifier.verify(browserIdAssertion, audience); } catch (HttpException e) { throw new BrowserIdAuthenticationException( "Error calling verify service [" + verifier.getVerifyUrl() + "]", e); } catch (IOException e) { throw new BrowserIdAuthenticationException( "Error calling verify service [" + verifier.getVerifyUrl() + "]", e); } catch (JSONException e) { throw new BrowserIdAuthenticationException( "Could not parse response from verify service [" + verifier.getVerifyUrl() + "]", e); } if (response != null) { if (response.getStatus() == BrowserIdResponse.Status.OK) { BrowserIdAuthenticationToken token = new BrowserIdAuthenticationToken(response, browserIdAssertion); //send to provider to get authorities return getAuthenticationManager().authenticate(token); } else { throw new BrowserIdAuthenticationException( "BrowserID verification failed, reason: " + response.getReason()); } } else throw new BrowserIdAuthenticationException("Verification yielded null response"); } //may not be a BrowserID authentication return null; }