Example usage for org.springframework.http MediaType APPLICATION_FORM_URLENCODED

List of usage examples for org.springframework.http MediaType APPLICATION_FORM_URLENCODED

Introduction

In this page you can find the example usage for org.springframework.http MediaType APPLICATION_FORM_URLENCODED.

Prototype

MediaType APPLICATION_FORM_URLENCODED

To view the source code for org.springframework.http MediaType APPLICATION_FORM_URLENCODED.

Click Source Link

Document

Public constant media type for application/x-www-form-urlencoded .

Usage

From source file:com.t163.http.client.T163HttpClient.java

/**
 * postForm/*  ww  w . j  a  v  a2 s  . co m*/
 * @param url
 * @param request
 * @param responseType
 * @return
 */
public <T> T postForm(String url, Object request, Class<T> responseType) {
    if (!(request instanceof MultiValueMap)) {
        try {
            request = getBodyMap(request);
        } catch (IllegalArgumentException e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
        } catch (IllegalAccessException e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
        }
    }
    return post(url, request, responseType, MediaType.APPLICATION_FORM_URLENCODED);
}

From source file:com.mycompany.trader.TradingConnect.java

private static void loginAndSaveJsessionIdCookie(final String user, final String password,
        final HttpHeaders headersToUpdate) {

    String url = "http://localhost:" + port + "/blueprint-trading-services/login.html";

    new RestTemplate().execute(url, HttpMethod.POST, new RequestCallback() {
        @Override/*  w w  w . j ava 2  s  .  c o  m*/
        public void doWithRequest(ClientHttpRequest request) throws IOException {
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.add("username", user);
            map.add("password", password);
            new FormHttpMessageConverter().write(map, MediaType.APPLICATION_FORM_URLENCODED, request);
        }
    }, new ResponseExtractor<Object>() {
        @Override
        public Object extractData(ClientHttpResponse response) throws IOException {
            headersToUpdate.add("Cookie", response.getHeaders().getFirst("Set-Cookie"));
            return null;
        }
    });
}

From source file:com.pluralsight.controller.test.GoalControllerTest.java

@Test
public void addGoalTest() throws Exception {

    login("prasady:mindfire");

    this.mockMvc/*from  ww w  .  j a v  a  2  s  .  c  o m*/
            .perform(post("/addGoal").contentType(MediaType.APPLICATION_FORM_URLENCODED).param("minutes", "30")
                    .sessionAttr("goal", new Goal()))
            .andExpect(status().isMovedTemporarily()).andExpect(redirectedUrl("index.jsp"));
}

From source file:org.awesomeagile.webapp.security.google.GoogleConfigurableOAuth2Template.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
protected AccessGrant postForAccessGrant(String accessTokenUrl, MultiValueMap<String, String> parameters) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
            parameters, headers);//from www.  j  a  v  a2 s  .c o m
    ResponseEntity<Map> responseEntity = getRestTemplate().exchange(accessTokenUrl, HttpMethod.POST,
            requestEntity, Map.class);
    Map<String, Object> responseMap = responseEntity.getBody();
    return extractAccessGrant(responseMap);
}

From source file:com.bailen.radioOnline.recursos.REJA.java

public Incidencia ratings(String apiKey, String rating, String idCancion, String fav) {
    MultiValueMap<String, String> params1 = new LinkedMultiValueMap<>();
    params1.add("rating", (rating));
    params1.add("idCancion", (idCancion));
    params1.add("fav", (fav));

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.add("Authorization", apiKey);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params1, headers);

    String result = new RestTemplate().postForObject("http://ceatic.ujaen.es:8075/radioapi/v2/ratings", request,
            String.class);

    try {/*w  ww.java  2s .c  om*/

        ObjectMapper a = new ObjectMapper();
        Incidencia listilla = a.readValue(result, Incidencia.class);
        return listilla;

    } catch (Exception e) {
        return null;

    }

}

From source file:net.unicon.cas.addon.rest.TicketsResourceTests.java

@Before
public void setup() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(this.ticketsResourceUnderTest).defaultRequest(
            get("/").contextPath("/cas").servletPath("/v1").contentType(MediaType.APPLICATION_FORM_URLENCODED))
            .build();/*from  w w w.j av  a2s .  co m*/
}

From source file:nl.flotsam.calendar.web.UriHttpMessageConverter.java

@Override
protected URI readInternal(Class<? extends URI> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {
    InputStream in = null;//from www . j  a v a 2  s  .c  om
    try {
        in = inputMessage.getBody();
        String uri = IOUtils.toString(in);
        if (inputMessage.getHeaders().getContentType() == MediaType.APPLICATION_FORM_URLENCODED) {
            uri = URLDecoder.decode(uri, "UTF-8");
        }
        return new URI(uri);
    } catch (URISyntaxException urie) {
        throw new HttpMessageNotReadableException("Failed to parse incoming String into a URI.", urie);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:org.trustedanalytics.servicebroker.hdfs.users.UaaClientTokenRetriver.java

public String getToken() {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    URI uaaUri = UriComponentsBuilder.fromHttpUrl(uaaConfiguration.getTokenUri())
            .queryParam(GRANT_TYPE, GRANT_TYPE_CREDENTIALS).queryParam(RESPONSE_TYPE, RESPONSE_TYPE_TOKEN)
            .build().encode().toUri();/*from ww  w  .j  a  va2 s .  co m*/
    HttpEntity<String> entity = new HttpEntity<>(PARAMETERS, headers);
    return uaaRestTemplate.postForObject(uaaUri, entity, UaaTokenResponse.class).getAccessToken();
}

From source file:org.keycloak.adapters.springsecurity.service.KeycloakDirectAccessGrantService.java

@Override
public RefreshableKeycloakSecurityContext login(String username, String password) throws VerificationException {

    final MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    final HttpHeaders headers = new HttpHeaders();

    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    body.set("username", username);
    body.set("password", password);
    body.set(OAuth2Constants.GRANT_TYPE, OAuth2Constants.PASSWORD);

    AccessTokenResponse response = template.postForObject(keycloakDeployment.getTokenUrl(),
            new HttpEntity<>(body, headers), AccessTokenResponse.class);

    return KeycloakSpringAdapterUtils.createKeycloakSecurityContext(keycloakDeployment, response);
}

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

@Test
public void mock_mvc_example_for_post_greeting_controller() throws Exception {
    MockMvc mockMvc = standaloneSetup(new PostController())
            .setMessageConverters(new MappingJackson2HttpMessageConverter()).build();

    String contentAsString = mockMvc
            .perform(post("/greetingPost").param("name", "Johan")
                    .contentType(MediaType.APPLICATION_FORM_URLENCODED))
            .andReturn().getResponse().getContentAsString();
    JsonPath jsonPath = new JsonPath(contentAsString);

    assertThat(jsonPath.getInt("id"), equalTo(1));
    assertThat(jsonPath.getString("content"), equalTo("Hello, Johan!"));
}