Example usage for org.springframework.util MultiValueMap getFirst

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

Introduction

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

Prototype

@Nullable
V getFirst(K key);

Source Link

Document

Return the first value for the given key.

Usage

From source file:minium.script.js.JsVariablePostProcessor.java

public void populateEngine(JsEngine engine) {
    for (Entry<String, MultiValueMap<String, Object>> entry : variables.entrySet()) {
        String beanDefinitionName = entry.getKey();
        MultiValueMap<String, Object> variable = entry.getValue();

        Object bean = beanFactory.getBean(beanDefinitionName);
        String jsVar = (String) variable.getFirst("value");
        String expression = (String) variable.getFirst("expression");
        Boolean deleteAfterExpression = (Boolean) variable.getFirst("deleteAfterExpression");

        if (jsVar == null)
            jsVar = beanDefinitionName;//from   ww w  .ja v  a  2s .  c  o m
        if (bean instanceof Jsonable) {
            engine.putJson(jsVar, ((Jsonable) bean).toJson());
        } else {
            engine.put(jsVar, bean);
        }

        try {
            if (!Strings.isNullOrEmpty(expression)) {
                engine.eval(expression, 1);
            }
        } finally {
            if (deleteAfterExpression != null && deleteAfterExpression.booleanValue()) {
                engine.delete(jsVar);
            }
        }
    }
}

From source file:cf.spring.CfComponentConfiguration.java

private String evaluate(MultiValueMap<String, Object> annotationAttributes, String attribute) {
    final String expression = annotationAttributes.getFirst(attribute).toString();
    final Object value = expressionResolver.evaluate(expression, expressionContext);
    return value == null ? null : value.toString();
}

From source file:io.restassured.module.mockmvc.ContentTypeTest.java

@Test
public void adds_default_charset_to_content_type_by_default() {
    final AtomicReference<String> contentType = new AtomicReference<String>();

    RestAssuredMockMvc.given().standaloneSetup(new GreetingController()).contentType(ContentType.JSON)
            .interceptor(new MockHttpServletRequestBuilderInterceptor() {
                public void intercept(MockHttpServletRequestBuilder requestBuilder) {
                    MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder,
                            "headers");
                    contentType.set(String.valueOf(headers.getFirst("Content-Type")));
                }//from ww  w  . ja  v a2 s  . c  o  m
            }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);

    assertThat(contentType.get()).isEqualTo("application/json;charset="
            + RestAssuredMockMvc.config().getEncoderConfig().defaultContentCharset());
}

From source file:io.restassured.module.mockmvc.ContentTypeTest.java

@Test
public void doesnt_add_default_charset_to_content_type_if_charset_is_defined_explicitly() {
    final AtomicReference<String> contentType = new AtomicReference<String>();

    RestAssuredMockMvc.given().standaloneSetup(new GreetingController())
            .contentType(ContentType.JSON.withCharset("UTF-16"))
            .interceptor(new MockHttpServletRequestBuilderInterceptor() {
                public void intercept(MockHttpServletRequestBuilder requestBuilder) {
                    MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder,
                            "headers");
                    contentType.set(String.valueOf(headers.getFirst("Content-Type")));
                }/* w  w w. j  ava  2 s .c o  m*/
            }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);

    assertThat(contentType.get()).isEqualTo("application/json;charset=UTF-16");
}

From source file:io.restassured.module.mockmvc.ContentTypeTest.java

@Test
public void doesnt_add_default_charset_to_content_type_if_configured_not_to_do_so() {
    final AtomicReference<String> contentType = new AtomicReference<String>();

    RestAssuredMockMvc.given()/*from  ww  w .j  a  va  2s. com*/
            .config(RestAssuredMockMvc.config().encoderConfig(
                    EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
            .standaloneSetup(new GreetingController()).contentType(ContentType.JSON)
            .interceptor(new MockHttpServletRequestBuilderInterceptor() {
                public void intercept(MockHttpServletRequestBuilder requestBuilder) {
                    MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder,
                            "headers");
                    contentType.set(String.valueOf(headers.getFirst("Content-Type")));
                }
            }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);

    assertThat(contentType.get()).isEqualTo("application/json");
}

From source file:io.restassured.module.mockmvc.ContentTypeTest.java

@Test
public void adds_specific_charset_to_content_type_by_default() {
    final AtomicReference<String> contentType = new AtomicReference<String>();

    RestAssuredMockMvc.given().standaloneSetup(new GreetingController())
            .config(RestAssuredMockMvc.config()
                    .encoderConfig(EncoderConfig.encoderConfig().defaultCharsetForContentType(
                            StandardCharsets.UTF_16.toString(), ContentType.JSON)))
            .contentType(ContentType.JSON).interceptor(new MockHttpServletRequestBuilderInterceptor() {
                public void intercept(MockHttpServletRequestBuilder requestBuilder) {
                    MultiValueMap<String, Object> headers = Whitebox.getInternalState(requestBuilder,
                            "headers");
                    contentType.set(String.valueOf(headers.getFirst("Content-Type")));
                }/*from www  .  j  a v  a  2s. com*/
            }).when().get("/greeting?name={name}", "Johan").then().statusCode(200);

    assertThat(contentType.get()).isEqualTo("application/json;charset=" + StandardCharsets.UTF_16.toString());
    assertThat(contentType.get())
            .doesNotContain(RestAssuredMockMvc.config().getEncoderConfig().defaultContentCharset());
}

From source file:org.cloudfoundry.identity.uaa.login.feature.ImplicitGrantIT.java

@Test
public void testInvalidScopes() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    LinkedMultiValueMap<String, String> postBody = new LinkedMultiValueMap<>();
    postBody.add("client_id", "cf");
    postBody.add("redirect_uri", "https://uaa.cloudfoundry.com/redirect/cf");
    postBody.add("response_type", "token");
    postBody.add("source", "credentials");
    postBody.add("username", testAccounts.getUserName());
    postBody.add("password", testAccounts.getPassword());
    postBody.add("scope", "read");

    ResponseEntity<Void> responseEntity = restOperations.exchange(baseUrl + "/oauth/authorize", HttpMethod.POST,
            new HttpEntity<>(postBody, headers), Void.class);

    Assert.assertEquals(HttpStatus.FOUND, responseEntity.getStatusCode());

    System.out.println(//  ww w  .  j av  a2s  . com
            "responseEntity.getHeaders().getLocation() = " + responseEntity.getHeaders().getLocation());

    UriComponents locationComponents = UriComponentsBuilder.fromUri(responseEntity.getHeaders().getLocation())
            .build();
    Assert.assertEquals("uaa.cloudfoundry.com", locationComponents.getHost());
    Assert.assertEquals("/redirect/cf", locationComponents.getPath());

    MultiValueMap<String, String> params = parseFragmentParams(locationComponents);

    Assert.assertThat(params.getFirst("error"), is("invalid_scope"));
    Assert.assertThat(params.getFirst("access_token"), isEmptyOrNullString());
    Assert.assertThat(params.getFirst("credentials"), isEmptyOrNullString());
}

From source file:io.pivotal.cla.webdriver.AuthenticationTests.java

@Test
public void requiresAuthenticationAndCreatesValidOAuthTokenRequest() throws Exception {
    String redirect = mockMvc.perform(get("/sign/pivotal")).andExpect(status().is3xxRedirection()).andReturn()
            .getResponse().getRedirectedUrl();

    UriComponents redirectComponent = UriComponentsBuilder.fromHttpUrl(redirect).build();

    assertThat(redirectComponent.getScheme()).isEqualTo("https");
    assertThat(redirectComponent.getHost()).isEqualTo("github.com");
    MultiValueMap<String, String> params = redirectComponent.getQueryParams();
    assertThat(params.getFirst("client_id")).isEqualTo(config.getMain().getClientId());
    assertThat(urlDecode(params.getFirst("redirect_uri"))).isEqualTo("http://localhost/login/oauth2/github");
    assertThat(params.getFirst("state")).isNotNull();

    String[] scopes = urlDecode(params.getFirst("scope")).split(",");
    assertThat(scopes).containsOnly("user:email");
}

From source file:io.pivotal.cla.webdriver.AuthenticationTests.java

@Test
public void adminRequiresAuthenticationAndCreatesValidOAuthTokenRequest() throws Exception {
    String redirect = mockMvc.perform(get("/admin/cla/link")).andExpect(status().is3xxRedirection()).andReturn()
            .getResponse().getRedirectedUrl();

    UriComponents redirectComponent = UriComponentsBuilder.fromHttpUrl(redirect).build();

    assertThat(redirectComponent.getScheme()).isEqualTo("https");
    assertThat(redirectComponent.getHost()).isEqualTo("github.com");
    MultiValueMap<String, String> params = redirectComponent.getQueryParams();
    assertThat(params.getFirst("client_id")).isEqualTo(config.getMain().getClientId());
    assertThat(urlDecode(params.getFirst("redirect_uri"))).isEqualTo("http://localhost/login/oauth2/github");
    assertThat(params.getFirst("state")).isNotNull();

    String[] scopes = urlDecode(params.getFirst("scope")).split(",");
    assertThat(scopes).containsOnly("user:email", "repo:status", "admin:repo_hook", "admin:org_hook",
            "read:org");
}

From source file:cf.spring.servicebroker.ServiceBrokerConfiguration.java

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
    final MultiValueMap<String, Object> annotationAttributes = importMetadata
            .getAllAnnotationAttributes(EnableServiceBroker.class.getName());
    final String username = evaluate(annotationAttributes.getFirst("username").toString());
    final String password = evaluate(annotationAttributes.getFirst("password").toString());
    if (username == null) {
        throw new IllegalArgumentException("username cannot be null");
    }//from   w ww  .  j  av  a  2  s  .c om
    if (password == null) {
        throw new IllegalArgumentException("password cannot be null");
    }
    authenticator = new HttpBasicAuthenticator("", username, password);
}