Java Code Examples for org.eclipse.jetty.server.Request#setAttribute()
The following examples show how to use
org.eclipse.jetty.server.Request#setAttribute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebCampaign.java From bidder with Apache License 2.0 | 5 votes |
public String multiPart(Request baseRequest, HttpServletRequest request, MultipartConfigElement config) throws Exception { HttpSession session = request.getSession(false); String user = (String) session.getAttribute("user"); baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, config); Collection<Part> parts = request.getParts(); for (Part part : parts) { System.out.println("" + part.getName()); } Part filePart = request.getPart("file"); InputStream imageStream = filePart.getInputStream(); byte[] resultBuff = new byte[0]; byte[] buff = new byte[1024]; int k = -1; while ((k = imageStream.read(buff, 0, buff.length)) > -1) { byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size // = bytes already // read + bytes last // read System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy // previous // bytes System.arraycopy(buff, 0, tbuff, resultBuff.length, k); // copy // current // lot resultBuff = tbuff; // call the temp buffer as your result buff } Map response = new HashMap(); return getString(response); }
Example 2
Source File: JwtAuthenticatorTest.java From cruise-control with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void testSuccessfulLogin() throws Exception { UserStore testUserStore = new UserStore(); testUserStore.addUser(TEST_USER, SecurityUtils.NO_CREDENTIAL, new String[]{USER_ROLE}); TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER); JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null); Authenticator.AuthConfiguration configuration = mock(Authenticator.AuthConfiguration.class); expect(configuration.getLoginService()).andReturn(loginService); expect(configuration.getIdentityService()).andReturn(new DefaultIdentityService()); expect(configuration.isSessionRenewedOnAuthentication()).andReturn(true); Request request = niceMock(Request.class); expect(request.getMethod()).andReturn(HttpMethod.GET.asString()); expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(null); request.setAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE, tokenAndKeys.token()); expectLastCall().andVoid(); expect(request.getCookies()).andReturn(new Cookie[] {new Cookie(JWT_TOKEN, tokenAndKeys.token())}); expect(request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE)).andReturn(tokenAndKeys.token()); HttpServletResponse response = mock(HttpServletResponse.class); replay(configuration, request, response); JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN); authenticator.setConfiguration(configuration); UserAuthentication authentication = (UserAuthentication) authenticator.validateRequest(request, response, true); verify(configuration, request, response); assertNotNull(authentication); assertTrue(authentication.getUserIdentity().getUserPrincipal() instanceof JwtUserPrincipal); JwtUserPrincipal userPrincipal = (JwtUserPrincipal) authentication.getUserIdentity().getUserPrincipal(); assertEquals(TEST_USER, userPrincipal.getName()); assertEquals(tokenAndKeys.token(), userPrincipal.getSerializedToken()); }
Example 3
Source File: JwtAuthenticatorTest.java From cruise-control with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void testFailedLoginWithUserNotFound() throws Exception { UserStore testUserStore = new UserStore(); testUserStore.addUser(TEST_USER_2, SecurityUtils.NO_CREDENTIAL, new String[] {USER_ROLE}); TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER); JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null); Authenticator.AuthConfiguration configuration = mock(Authenticator.AuthConfiguration.class); expect(configuration.getLoginService()).andReturn(loginService); expect(configuration.getIdentityService()).andReturn(new DefaultIdentityService()); expect(configuration.isSessionRenewedOnAuthentication()).andReturn(true); Request request = niceMock(Request.class); expect(request.getMethod()).andReturn(HttpMethod.GET.asString()); expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(null); request.setAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE, tokenAndKeys.token()); expectLastCall().andVoid(); expect(request.getCookies()).andReturn(new Cookie[] {new Cookie(JWT_TOKEN, tokenAndKeys.token())}); expect(request.getAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE)).andReturn(tokenAndKeys.token()); HttpServletResponse response = mock(HttpServletResponse.class); response.setStatus(HttpStatus.UNAUTHORIZED_401); expectLastCall().andVoid(); replay(configuration, request, response); JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN); authenticator.setConfiguration(configuration); Authentication authentication = authenticator.validateRequest(request, response, true); verify(configuration, request, response); assertNotNull(authentication); assertEquals(Authentication.SEND_FAILURE, authentication); }
Example 4
Source File: JwtAuthenticatorTest.java From cruise-control with BSD 2-Clause "Simplified" License | 5 votes |
@Test public void testFailedLoginWithInvalidToken() throws Exception { UserStore testUserStore = new UserStore(); testUserStore.addUser(TEST_USER_2, SecurityUtils.NO_CREDENTIAL, new String[] {USER_ROLE}); TokenGenerator.TokenAndKeys tokenAndKeys = TokenGenerator.generateToken(TEST_USER); TokenGenerator.TokenAndKeys tokenAndKeys2 = TokenGenerator.generateToken(TEST_USER); JwtLoginService loginService = new JwtLoginService(new UserStoreAuthorizationService(testUserStore), tokenAndKeys.publicKey(), null); Authenticator.AuthConfiguration configuration = mock(Authenticator.AuthConfiguration.class); expect(configuration.getLoginService()).andReturn(loginService); expect(configuration.getIdentityService()).andReturn(new DefaultIdentityService()); expect(configuration.isSessionRenewedOnAuthentication()).andReturn(true); Request request = niceMock(Request.class); expect(request.getMethod()).andReturn(HttpMethod.GET.asString()); expect(request.getHeader(HttpHeader.AUTHORIZATION.asString())).andReturn(null); request.setAttribute(JwtAuthenticator.JWT_TOKEN_REQUEST_ATTRIBUTE, tokenAndKeys2.token()); expectLastCall().andVoid(); expect(request.getCookies()).andReturn(new Cookie[] {new Cookie(JWT_TOKEN, tokenAndKeys2.token())}); HttpServletResponse response = mock(HttpServletResponse.class); response.setStatus(HttpStatus.UNAUTHORIZED_401); expectLastCall().andVoid(); replay(configuration, request, response); JwtAuthenticator authenticator = new JwtAuthenticator(TOKEN_PROVIDER, JWT_TOKEN); authenticator.setConfiguration(configuration); Authentication authentication = authenticator.validateRequest(request, response, true); verify(configuration, request, response); assertNotNull(authentication); assertEquals(Authentication.SEND_FAILURE, authentication); }
Example 5
Source File: RequestContextScope.java From jetty-runtime with Apache License 2.0 | 5 votes |
@Override public void enterScope(Context context, Request request, Object reason) { if (logger.isLoggable(Level.FINE)) { logger.fine("enterScope " + context); } if (request != null) { Integer depth = contextDepth.get(); if (depth == null || depth.intValue() == 0) { contextDepth.set(1); String currentTraceId = (String) request.getAttribute(X_CLOUD_TRACE); if (currentTraceId == null) { // extract xCloud Trace in format: TRACE_ID/SPAN_ID;o=TRACE_TRUE String cloudTrace = request.getHeader(X_CLOUD_TRACE); if (cloudTrace != null) { int split = cloudTrace.indexOf('/'); if (split < 0) { split = cloudTrace.indexOf(';'); } String traceId = split >= 0 ? cloudTrace.substring(0, split) : cloudTrace; if (traceId != null) { currentTraceId = String.format("projects/%s/traces/%s", projectId, traceId); request.setAttribute(X_CLOUD_TRACE, currentTraceId); TraceLoggingEnhancer.setCurrentTraceId(currentTraceId); } } } } else { contextDepth.set(depth + 1); } } }
Example 6
Source File: KeycloakAuthFilter.java From keycloak-dropwizard-integration with Apache License 2.0 | 5 votes |
public void validateRequest(final ContainerRequestContext requestContext) { if (requestContext.getSecurityContext().getUserPrincipal() != null) { // the user is already authenticated, further processing is not necessary return; } Request request = Request.getBaseRequest((ServletRequest) requestContext.getProperty(HttpServletRequest.class.getName())); JaxrsHttpFacade facade = new JaxrsHttpFacade(requestContext, requestContext.getSecurityContext()); request.setAttribute(AdapterDeploymentContext.class.getName(), deploymentContext); KeycloakDeployment deployment = deploymentContext.resolveDeployment(facade); if (deployment == null || !deployment.isConfigured()) { return; } AdapterTokenStore tokenStore = getTokenStore(request, facade, deployment); tokenStore.checkCurrentToken(); JettyRequestAuthenticator authenticator = createRequestAuthenticator(request, facade, deployment, tokenStore); AuthOutcome outcome = authenticator.authenticate(); if (outcome == AuthOutcome.AUTHENTICATED) { return; } AuthChallenge challenge = authenticator.getChallenge(); if (challenge != null) { challenge.challenge(facade); if (!adapterConfig.isBearerOnly()) { // create session and set cookie for client facade.getResponse().setCookie("JSESSIONID", request.getSession().getId(), "/", null, -1, false, false); } facade.getResponse().end(); } }
Example 7
Source File: JettyServer.java From pippo with Apache License 2.0 | 5 votes |
@Override public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { if (isMultipartRequest(request)) { baseRequest.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, multipartConfig); } super.doHandle(target, baseRequest, request, response); }
Example 8
Source File: AbstractKeycloakJettyAuthenticator.java From keycloak with Apache License 2.0 | 5 votes |
public AdapterTokenStore getTokenStore(Request request, HttpFacade facade, KeycloakDeployment resolvedDeployment) { AdapterTokenStore store = (AdapterTokenStore) request.getAttribute(TOKEN_STORE_NOTE); if (store != null) { return store; } if (resolvedDeployment.getTokenStore() == TokenStore.SESSION) { store = createSessionTokenStore(request, resolvedDeployment); } else { store = new JettyCookieTokenStore(request, facade, resolvedDeployment); } request.setAttribute(TOKEN_STORE_NOTE, store); return store; }
Example 9
Source File: AbstractKeycloakJettyAuthenticator.java From keycloak with Apache License 2.0 | 5 votes |
protected Authentication register(Request request, KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal) { request.setAttribute(AdapterDeploymentContext.class.getName(), deploymentContext); Authentication authentication = request.getAuthentication(); if (!(authentication instanceof KeycloakAuthentication)) { UserIdentity userIdentity = createIdentity(principal); authentication = createAuthentication(userIdentity, request); request.setAuthentication(authentication); } return authentication; }
Example 10
Source File: AbstractSamlAuthenticator.java From keycloak with Apache License 2.0 | 5 votes |
public JettySamlSessionStore getTokenStore(Request request, HttpFacade facade, SamlDeployment resolvedDeployment) { JettySamlSessionStore store = (JettySamlSessionStore) request.getAttribute(TOKEN_STORE_NOTE); if (store != null) { return store; } store = createJettySamlSessionStore(request, facade, resolvedDeployment); request.setAttribute(TOKEN_STORE_NOTE, store); return store; }
Example 11
Source File: WebCampaign.java From XRTB with Apache License 2.0 | 4 votes |
public String multiPart(Request baseRequest, HttpServletRequest request, MultipartConfigElement config) throws Exception { HttpSession session = request.getSession(false); String user = (String) session.getAttribute("user"); User u = db.getUser(user); if (u == null) throw new Exception("No such user"); baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, config); Collection<Part> parts = request.getParts(); for (Part part : parts) { System.out.println("" + part.getName()); } Part filePart = request.getPart("file"); InputStream imageStream = filePart.getInputStream(); byte[] resultBuff = new byte[0]; byte[] buff = new byte[1024]; int k = -1; while ((k = imageStream.read(buff, 0, buff.length)) > -1) { byte[] tbuff = new byte[resultBuff.length + k]; // temp buffer size // = bytes already // read + bytes last // read System.arraycopy(resultBuff, 0, tbuff, 0, resultBuff.length); // copy // previous // bytes System.arraycopy(buff, 0, tbuff, resultBuff.length, k); // copy // current // lot resultBuff = tbuff; // call the temp buffer as your result buff } System.out.println(resultBuff.length + " bytes read."); if (k == 0) { // no file provided throw new Exception("No file provided"); } else { byte[] bytes = new byte[1024]; Part namePart = request.getPart("name"); InputStream nameStream = namePart.getInputStream(); int rc = nameStream.read(bytes); String name = new String(bytes, 0, rc); FileOutputStream fos = new FileOutputStream(u.directory + "/" + name); fos.write(resultBuff); fos.close(); } Map response = new HashMap(); response.put("images", getFiles(u)); return getString(response); }