Example usage for org.springframework.util MultiValueMap set

List of usage examples for org.springframework.util MultiValueMap set

Introduction

In this page you can find the example usage for org.springframework.util MultiValueMap set.

Prototype

void set(K key, @Nullable V value);

Source Link

Document

Set the given single value under the given key.

Usage

From source file:org.zaizi.AuthServerApplicationTests.java

@Test
public void loginSucceeds() {
    ResponseEntity<String> response = template.getForEntity("http://localhost:" + port + contextPath + "/login",
            String.class);
    String csrf = getCsrf(response.getBody());
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "admin");
    form.set("password", "ulibraxi");
    form.set("_csrf", csrf);
    HttpHeaders headers = new HttpHeaders();
    headers.put("COOKIE", response.getHeaders().get("Set-Cookie"));
    RequestEntity<MultiValueMap<String, String>> request = new RequestEntity<MultiValueMap<String, String>>(
            form, headers, HttpMethod.POST, URI.create("http://localhost:" + port + contextPath + "/login"));
    ResponseEntity<Void> location = template.exchange(request, Void.class);
    assertEquals("http://localhost:" + port + contextPath + "/", location.getHeaders().getFirst("Location"));
}

From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java

@Test
public void testLogin() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "admin");
    form.set("password", "admin");
    getCsrf(form, headers);// w  w w .  ja v a  2  s. co m
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login",
            HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    assertEquals("http://localhost:" + this.port + "/", entity.getHeaders().getLocation().toString());
}

From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java

@Test
public void testDenied() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", "user");
    form.set("password", "user");
    getCsrf(form, headers);//  w ww  .j a v  a  2 s .c om
    ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login",
            HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class);
    assertEquals(HttpStatus.FOUND, entity.getStatusCode());
    String cookie = entity.getHeaders().getFirst("Set-Cookie");
    headers.set("Cookie", cookie);
    ResponseEntity<String> page = new TestRestTemplate().exchange(entity.getHeaders().getLocation(),
            HttpMethod.GET, new HttpEntity<Void>(headers), String.class);
    assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode());
    assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(),
            page.getBody().contains("Access denied"));
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
    parameters.set("username", username);
    parameters.set("password", password);

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response = restTemplate.exchange(loginUrl, HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, Object>>(parameters, headers), Map.class);

    if (response.getStatusCode() == HttpStatus.OK) {
        String userFromUaa = (String) response.getBody().get("username");

        if (userFromUaa.equals(userFromUaa)) {
            logger.info("Successful authentication request for " + authentication.getName());
            return new UsernamePasswordAuthenticationToken(username, null, UaaAuthority.USER_AUTHORITIES);
        }/*  ww w  .  jav  a2s.c o  m*/
    } else if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
        logger.info("Failed authentication request");
        throw new BadCredentialsException("Authentication failed");
    } else if (response.getStatusCode() == HttpStatus.INTERNAL_SERVER_ERROR) {
        logger.info("Internal error from UAA. Please Check the UAA logs.");
    } else {
        logger.error("Unexpected status code " + response.getStatusCode() + " from the UAA."
                + " Is a compatible version running?");
    }
    throw new RuntimeException("Could not authenticate with remote server");
}

From source file:org.cloudfoundry.identity.uaa.login.LinkedMaskingMultiValueMapTests.java

@Test
public void equals() {
    map.set("key1", "value1");
    assertEquals(map, map);/* www.jav a2s.c  om*/
    MultiValueMap<String, String> o1 = new LinkedMaskingMultiValueMap<String, String>();
    o1.set("key1", "value1");
    assertEquals(map, o1);
    assertEquals(o1, map);
    Map<String, List<String>> o2 = new HashMap<String, List<String>>();
    o2.put("key1", Collections.singletonList("value1"));
    assertEquals(map, o2);
    assertEquals(o2, map);
}

From source file:com.work.petclinic.SampleWebUiApplicationTests.java

@After
public void cleanSession() throws Exception {
    ResponseEntity<String> page = sendRequest("http://localhost:" + this.port, HttpMethod.GET);

    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("_csrf", csrfValue);
    httpHeaders.set("X-CSRF-TOKEN", csrfValue);

    page = sendRequest("http://localhost:" + this.port + "/logout", HttpMethod.GET, form);

    assertEquals(HttpStatus.FOUND, page.getStatusCode());

    if (page.getStatusCode() == HttpStatus.FOUND) {
        page = sendRequest(page.getHeaders().getLocation(), HttpMethod.GET);
    }/*from  w ww . j  av a 2  s .c o  m*/

    httpHeaders = null;
    csrfValue = null;
}

From source file:org.agorava.yammer.impl.MessageServiceImpl.java

private MultiValueMap<String, String> buildParams(long olderThan, long newerThan, String threaded, int limit) {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    if (olderThan != 0) {
        params.set("older_than", String.valueOf(olderThan));
    }//from  ww  w.j  a  v a  2s  .c  o m
    if (newerThan != 0) {
        params.set("newer_than", String.valueOf(newerThan));
    }
    if (threaded != null) {
        params.set("threaded", String.valueOf(threaded));
    }
    if (limit != 0) {
        params.set("limit", String.valueOf(limit));
    }
    return params;
}

From source file:com.work.petclinic.SampleWebUiApplicationTests.java

@Test
public void testCreateUserAndLogin() throws Exception {
    ResponseEntity<String> page = getPage("http://localhost:" + this.port + "/users");

    assertTrue("Client or server error.",
            !page.getStatusCode().is4xxClientError() && !page.getStatusCode().is5xxServerError());

    if (page.getStatusCode() == HttpStatus.FOUND) {
        page = getPage(page.getHeaders().getLocation());
    }/*from   w ww  . j  a v  a 2  s.  c  o  m*/

    String body = page.getBody();
    assertNotNull("Body was null", body);

    String username = "newuser";
    String password = "password";
    String formAction = getFormAction(page);

    MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
    form.set("username", username);
    form.set("email", "newuser@newuser.org");
    form.set("name", "New User");
    form.set("uiPassword", password);
    form.set("verifyPassword", password);
    form.set("_eventId_saveUser", "Create User");
    form.set("_csrf", csrfValue);

    httpHeaders.set("X-CSRF-TOKEN", csrfValue);

    page = postPage(formAction, form);

    if (page.getStatusCode() == HttpStatus.FOUND) {
        page = getPage(page.getHeaders().getLocation());
    }

    assertEquals(HttpStatus.OK, page.getStatusCode());
    body = page.getBody();
    assertNotNull("Body was null", body);
    assertTrue("User not created:\n" + body, body.contains("User " + username + " saved"));

    executeLogin(username, password);
}

From source file:com.hyrt.cnp.account.AccountAuthenticator.java

@Override
public Bundle getAuthToken(final AccountAuthenticatorResponse response, final Account account,
        final String authTokenType, final Bundle options) throws NetworkErrorException {
    Log.d(TAG, "Retrieving OAuth2 token");

    final Bundle bundle = new Bundle();

    if (!ACCOUNT_TYPE.equals(authTokenType))
        return bundle;

    AccountManager am = AccountManager.get(context);
    String password = am.getPassword(account);
    if (TextUtils.isEmpty(password)) {
        bundle.putParcelable(KEY_INTENT, createLoginIntent(response));
        return bundle;
    }/*from w  w w.  j av  a2  s  .  c  o m*/
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
    params.set("username", account.name);
    params.set("password", password);
    User.UserModel userModel = null;
    try {
        userModel = getCustomRestTemplate().postForObject("http://api.chinaxueqian.com/account/login", params,
                User.UserModel.class);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (userModel == null || userModel.getData() == null || TextUtils.isEmpty(userModel.getData().getToken()))
        bundle.putParcelable(KEY_INTENT, createLoginIntent(response));
    else {
        bundle.putString(KEY_ACCOUNT_NAME, account.name);
        bundle.putString(KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
        bundle.putString(KEY_AUTHTOKEN, userModel.getData().getToken() + "&uuid="
                + userModel.getData().getUuid() + "&sid=" + userModel.getData().getNursery_id());
        am.clearPassword(account);
    }
    return bundle;
}

From source file:org.jnrain.mobile.accounts.kbs.KBSRegisterRequest.java

@Override
public KBSRegisterResult loadDataFromNetwork() throws Exception {
    MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();

    if (isPreflight) {
        // preflight request, only prereg flag is sent
        params.set("prereg", "1");
    } else {//from ww w. ja v  a  2  s  .c  o m
        // please keep this in sync with rainstyle/apiregister.php
        params.set("userid", _uid);
        params.set("password", _password);
        params.set("nickname", _nickname);
        params.set("realname", _realname);
        params.set("email", _email);
        params.set("phone", _phone);
        params.set("idnumber", _idnumber);
        params.set("gender", Integer.toString(_gender));
        params.set("captcha", _captcha);
    }

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, String>> req = new HttpEntity<MultiValueMap<String, String>>(params,
            headers);

    return getRestTemplate().postForObject("http://bbs.jnrain.com/rainstyle/apiregister.php", req,
            KBSRegisterResult.class);
}