@Test public void getsSecurityContextFromServletRequest() throws Exception { when(httpServletRequest.getUserPrincipal()).thenReturn(new SimplePrincipal("andrew")); when(httpServletRequest.isUserInRole("user")).thenReturn(true); when(httpServletRequest.isSecure()).thenReturn(false); when(httpServletRequest.getAuthType()).thenReturn("BASIC_AUTH"); ServletContainerRequest servletContainerRequest = ServletContainerRequest.create(httpServletRequest); assertEquals("BASIC_AUTH", servletContainerRequest.getAuthenticationScheme()); assertEquals("andrew", servletContainerRequest.getUserPrincipal().getName()); assertFalse(servletContainerRequest.isSecure()); assertTrue(servletContainerRequest.isUserInRole("user")); }
public static ServletContainerRequest create(final HttpServletRequest req) { final URL forwardedUrl = getForwardedUrl(req); String host; int port; if (forwardedUrl == null) { host = req.getServerName(); port = req.getServerPort(); } else { host = forwardedUrl.getHost(); port = forwardedUrl.getPort(); if (port < 0) { port = forwardedUrl.getDefaultPort(); } LOG.debug("Assuming forwarded URL: {}", forwardedUrl); } final StringBuilder commonUriBuilder = new StringBuilder(); final String scheme = getScheme(req); commonUriBuilder.append(scheme); commonUriBuilder.append(": commonUriBuilder.append(host); if (!(port < 0 || (port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme)))) { commonUriBuilder.append(':'); commonUriBuilder.append(port); } final String commonUriPrefix = commonUriBuilder.toString(); final StringBuilder baseUriBuilder = new StringBuilder(commonUriPrefix); baseUriBuilder.append(req.getContextPath()); baseUriBuilder.append(req.getServletPath()); final URI baseUri = URI.create(baseUriBuilder.toString()); final StringBuilder requestUriBuilder = new StringBuilder(commonUriPrefix); requestUriBuilder.append(req.getRequestURI()); final String queryString = req.getQueryString(); if (queryString != null) { requestUriBuilder.append('?'); requestUriBuilder.append(queryString); } final URI requestUri = URI.create(requestUriBuilder.toString()); return new ServletContainerRequest(getMethod(req), requestUri, baseUri, getEntityStream(req), getHeaders(req), getSecurityContext(req)); }
ServletContainerRequest extends ContainerRequest { public static ServletContainerRequest create(final HttpServletRequest req) { final URL forwardedUrl = getForwardedUrl(req); String host; int port; if (forwardedUrl == null) { host = req.getServerName(); port = req.getServerPort(); } else { host = forwardedUrl.getHost(); port = forwardedUrl.getPort(); if (port < 0) { port = forwardedUrl.getDefaultPort(); } LOG.debug("Assuming forwarded URL: {}", forwardedUrl); } final StringBuilder commonUriBuilder = new StringBuilder(); final String scheme = getScheme(req); commonUriBuilder.append(scheme); commonUriBuilder.append(": commonUriBuilder.append(host); if (!(port < 0 || (port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme)))) { commonUriBuilder.append(':'); commonUriBuilder.append(port); } final String commonUriPrefix = commonUriBuilder.toString(); final StringBuilder baseUriBuilder = new StringBuilder(commonUriPrefix); baseUriBuilder.append(req.getContextPath()); baseUriBuilder.append(req.getServletPath()); final URI baseUri = URI.create(baseUriBuilder.toString()); final StringBuilder requestUriBuilder = new StringBuilder(commonUriPrefix); requestUriBuilder.append(req.getRequestURI()); final String queryString = req.getQueryString(); if (queryString != null) { requestUriBuilder.append('?'); requestUriBuilder.append(queryString); } final URI requestUri = URI.create(requestUriBuilder.toString()); return new ServletContainerRequest(getMethod(req), requestUri, baseUri, getEntityStream(req), getHeaders(req), getSecurityContext(req)); } }
ServletContainerRequest extends ContainerRequest { public static ServletContainerRequest create(final HttpServletRequest req) { final URL forwardedUrl = getForwardedUrl(req); String host; int port; if (forwardedUrl == null) { host = req.getServerName(); port = req.getServerPort(); } else { host = forwardedUrl.getHost(); port = forwardedUrl.getPort(); if (port < 0) { port = forwardedUrl.getDefaultPort(); } LOG.debug("Assuming forwarded URL: {}", forwardedUrl); } final StringBuilder commonUriBuilder = new StringBuilder(); final String scheme = getScheme(req); commonUriBuilder.append(scheme); commonUriBuilder.append(": commonUriBuilder.append(host); if (!(port < 0 || (port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme)))) { commonUriBuilder.append(':'); commonUriBuilder.append(port); } final String commonUriPrefix = commonUriBuilder.toString(); final StringBuilder baseUriBuilder = new StringBuilder(commonUriPrefix); baseUriBuilder.append(req.getContextPath()); baseUriBuilder.append(req.getServletPath()); final URI baseUri = URI.create(baseUriBuilder.toString()); final StringBuilder requestUriBuilder = new StringBuilder(commonUriPrefix); requestUriBuilder.append(req.getRequestURI()); final String queryString = req.getQueryString(); if (queryString != null) { requestUriBuilder.append('?'); requestUriBuilder.append(queryString); } final URI requestUri = URI.create(requestUriBuilder.toString()); return new ServletContainerRequest(getMethod(req), requestUri, baseUri, getEntityStream(req), getHeaders(req), getSecurityContext(req)); } private  ServletContainerRequest(String method, URI requestUri, URI baseUri, InputStream entityStream,
                                    MultivaluedMap<String, String> httpHeaders, SecurityContext securityContext); }
ServletContainerRequest extends ContainerRequest { public static ServletContainerRequest create(final HttpServletRequest req) { final URL forwardedUrl = getForwardedUrl(req); String host; int port; if (forwardedUrl == null) { host = req.getServerName(); port = req.getServerPort(); } else { host = forwardedUrl.getHost(); port = forwardedUrl.getPort(); if (port < 0) { port = forwardedUrl.getDefaultPort(); } LOG.debug("Assuming forwarded URL: {}", forwardedUrl); } final StringBuilder commonUriBuilder = new StringBuilder(); final String scheme = getScheme(req); commonUriBuilder.append(scheme); commonUriBuilder.append(": commonUriBuilder.append(host); if (!(port < 0 || (port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme)))) { commonUriBuilder.append(':'); commonUriBuilder.append(port); } final String commonUriPrefix = commonUriBuilder.toString(); final StringBuilder baseUriBuilder = new StringBuilder(commonUriPrefix); baseUriBuilder.append(req.getContextPath()); baseUriBuilder.append(req.getServletPath()); final URI baseUri = URI.create(baseUriBuilder.toString()); final StringBuilder requestUriBuilder = new StringBuilder(commonUriPrefix); requestUriBuilder.append(req.getRequestURI()); final String queryString = req.getQueryString(); if (queryString != null) { requestUriBuilder.append('?'); requestUriBuilder.append(queryString); } final URI requestUri = URI.create(requestUriBuilder.toString()); return new ServletContainerRequest(getMethod(req), requestUri, baseUri, getEntityStream(req), getHeaders(req), getSecurityContext(req)); } private  ServletContainerRequest(String method, URI requestUri, URI baseUri, InputStream entityStream,
                                    MultivaluedMap<String, String> httpHeaders, SecurityContext securityContext); static ServletContainerRequest create(final HttpServletRequest req); }
ServletContainerRequest extends ContainerRequest { public static ServletContainerRequest create(final HttpServletRequest req) { final URL forwardedUrl = getForwardedUrl(req); String host; int port; if (forwardedUrl == null) { host = req.getServerName(); port = req.getServerPort(); } else { host = forwardedUrl.getHost(); port = forwardedUrl.getPort(); if (port < 0) { port = forwardedUrl.getDefaultPort(); } LOG.debug("Assuming forwarded URL: {}", forwardedUrl); } final StringBuilder commonUriBuilder = new StringBuilder(); final String scheme = getScheme(req); commonUriBuilder.append(scheme); commonUriBuilder.append(": commonUriBuilder.append(host); if (!(port < 0 || (port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme)))) { commonUriBuilder.append(':'); commonUriBuilder.append(port); } final String commonUriPrefix = commonUriBuilder.toString(); final StringBuilder baseUriBuilder = new StringBuilder(commonUriPrefix); baseUriBuilder.append(req.getContextPath()); baseUriBuilder.append(req.getServletPath()); final URI baseUri = URI.create(baseUriBuilder.toString()); final StringBuilder requestUriBuilder = new StringBuilder(commonUriPrefix); requestUriBuilder.append(req.getRequestURI()); final String queryString = req.getQueryString(); if (queryString != null) { requestUriBuilder.append('?'); requestUriBuilder.append(queryString); } final URI requestUri = URI.create(requestUriBuilder.toString()); return new ServletContainerRequest(getMethod(req), requestUri, baseUri, getEntityStream(req), getHeaders(req), getSecurityContext(req)); } private  ServletContainerRequest(String method, URI requestUri, URI baseUri, InputStream entityStream,
                                    MultivaluedMap<String, String> httpHeaders, SecurityContext securityContext); static ServletContainerRequest create(final HttpServletRequest req);  }