List of usage examples for org.springframework.http MediaType APPLICATION_FORM_URLENCODED
MediaType APPLICATION_FORM_URLENCODED
To view the source code for org.springframework.http MediaType APPLICATION_FORM_URLENCODED.
Click Source Link
From source file:org.zalando.boot.etcd.EtcdClientTest.java
@Test public void compareAndSwapWithPrevValue() throws EtcdException { server.expect(//from w w w . j av a 2 s. c om MockRestRequestMatchers.requestTo("http://localhost:2379/v2/keys/sample?prevValue=Hello%20etcd")) .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) .andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(MockRestRequestMatchers.content().string("value=Hello+world")).andRespond( MockRestResponseCreators.withSuccess(new ClassPathResource("EtcdClientTest_delete.json"), MediaType.APPLICATION_JSON)); EtcdResponse response = client.compareAndSwap("sample", "Hello world", "Hello etcd"); Assert.assertNotNull("response", response); server.verify(); }
From source file:org.opentides.rest.impl.BaseCrudRestServiceImpl.java
/** * Make sure RestTemplate is properly set. *//*from w ww . j a v a 2s . com*/ @SuppressWarnings("unchecked") @PostConstruct public final void afterPropertiesSet() throws Exception { try { this.entityBeanType = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()) .getActualTypeArguments()[0]; } catch (ClassCastException cc) { // if dao is extended from the generic dao class this.entityBeanType = (Class<T>) ((ParameterizedType) getClass().getSuperclass().getGenericSuperclass()) .getActualTypeArguments()[0]; } // setup entity(headers) for basic authentication. HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); this.entity = new HttpEntity<String>(headers); // setup the rest template if (restTemplate == null) { restTemplate = new RestTemplate(); } Assert.notNull(this.entityBeanType, "Unable to retrieve entityBeanType for " + this.getClass().getSimpleName()); }
From source file:org.mitreid.multiparty.web.ClientController.java
@RequestMapping(value = "claims_submitted") public String claimsSubmissionCallback(@RequestParam("authorization_state") String authorizationState, @RequestParam("state") String returnState, @RequestParam("ticket") String ticket, HttpSession session, Model m) {/*w ww .j a va 2 s . c om*/ // get our saved information out of the session String savedState = (String) session.getAttribute(STATE_SESSION_VAR); String savedResource = (String) session.getAttribute(RESOURCE_SESSION_VAR); String savedAuthServerUri = (String) session.getAttribute(AUTHSERVERURI_SESSION_VAR); // make sure the state matches if (Strings.isNullOrEmpty(returnState) || !returnState.equals(savedState)) { // it's an error if it doesn't logger.error("Unable to match states"); return "home"; } if (authorizationState.equals("claims_submitted")) { // claims have been submitted, let's go try to get a token again // find the AS we need to talk to (maybe discover) MultipartyServerConfiguration server = serverConfig.getServerConfiguration(savedAuthServerUri); // find the client configuration (maybe register) RegisteredClient client = clientConfig.getClientConfiguration(server); HttpHeaders tokenHeaders = new HttpHeaders(); tokenHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // send request to the token endpoint MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("client_id", client.getClientId()); params.add("client_secret", client.getClientSecret()); params.add("grant_type", "urn:ietf:params:oauth:grant_type:multiparty-delegation"); params.add("ticket", ticket); //params.add("scope", "read write"); HttpEntity<MultiValueMap<String, String>> tokenRequest = new HttpEntity<>(params, tokenHeaders); ResponseEntity<String> tokenResponse = restTemplate.postForEntity(server.getTokenEndpointUri(), tokenRequest, String.class); JsonObject o = parser.parse(tokenResponse.getBody()).getAsJsonObject(); if (o.has("error")) { if (o.get("error").getAsString().equals("need_info")) { // if we get need info, redirect JsonObject details = o.get("error_details").getAsJsonObject(); // this is the URL to send the user to String claimsEndpoint = details.get("requesting_party_claims_endpoint").getAsString(); String newTicket = details.get("ticket").getAsString(); // set a state value for our return String state = UUID.randomUUID().toString(); session.setAttribute(STATE_SESSION_VAR, state); // save bits about the request we were trying to make session.setAttribute(RESOURCE_SESSION_VAR, savedResource); session.setAttribute(AUTHSERVERURI_SESSION_VAR, savedAuthServerUri); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(claimsEndpoint) .queryParam("client_id", client.getClientId()).queryParam("ticket", newTicket) .queryParam("claims_redirect_uri", client.getClaimsRedirectUris().iterator().next()) // get the first one and punt .queryParam("state", state); return "redirect:" + builder.build(); } else { // it's an error we don't know how to deal with, give up logger.error("Unknown error from token endpoint: " + o.get("error").getAsString()); return "home"; } } else { // if we get an access token, try it again String accessTokenValue = o.get("access_token").getAsString(); acccessTokenService.saveAccesstoken(savedResource, accessTokenValue); HttpHeaders headers = new HttpHeaders(); if (!Strings.isNullOrEmpty(accessTokenValue)) { headers.add("Authorization", "Bearer " + accessTokenValue); } HttpEntity<Object> request = new HttpEntity<>(headers); ResponseEntity<String> responseEntity = restTemplate.exchange(savedResource, HttpMethod.GET, request, String.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { // if we get back data, display it JsonObject rso = parser.parse(responseEntity.getBody()).getAsJsonObject(); m.addAttribute("label", rso.get("label").getAsString()); m.addAttribute("value", rso.get("value").getAsString()); return "home"; } else { logger.error("Unable to get a token"); return "home"; } } } else { logger.error("Unknown response from claims endpoing: " + authorizationState); return "home"; } }
From source file:org.zalando.boot.etcd.EtcdClientTest.java
@Test public void compareAndSwapWithPrevValueAndSetTtl() throws EtcdException { server.expect(MockRestRequestMatchers .requestTo("http://localhost:2379/v2/keys/sample?ttl=60&prevValue=Hello%20etcd")) .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) .andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(MockRestRequestMatchers.content().string("value=Hello+world")).andRespond( MockRestResponseCreators.withSuccess(new ClassPathResource("EtcdClientTest_delete.json"), MediaType.APPLICATION_JSON)); EtcdResponse response = client.compareAndSwap("sample", "Hello world", 60, "Hello etcd"); Assert.assertNotNull("response", response); server.verify();//from w w w. ja v a2s . c o m }
From source file:org.zalando.boot.etcd.EtcdClientTest.java
@Test public void compareAndSwapWithPrevValueAndUnsetTtl() throws EtcdException { server.expect(MockRestRequestMatchers .requestTo("http://localhost:2379/v2/keys/sample?ttl=&prevValue=Hello%20etcd")) .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) .andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(MockRestRequestMatchers.content().string("value=Hello+world")).andRespond( MockRestResponseCreators.withSuccess(new ClassPathResource("EtcdClientTest_delete.json"), MediaType.APPLICATION_JSON)); EtcdResponse response = client.compareAndSwap("sample", "Hello world", -1, "Hello etcd"); Assert.assertNotNull("response", response); server.verify();/* ww w . j av a 2 s .c o m*/ }
From source file:org.zalando.boot.etcd.EtcdClientTest.java
@Test public void putDir() throws EtcdException { server.expect(MockRestRequestMatchers.requestTo("http://localhost:2379/v2/keys/sample")) .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) .andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(MockRestRequestMatchers.content().string("dir=true")).andRespond( MockRestResponseCreators.withSuccess(new ClassPathResource("EtcdClientTest_delete.json"), MediaType.APPLICATION_JSON)); EtcdResponse response = client.putDir("sample"); Assert.assertNotNull("response", response); server.verify();/*from ww w . j a va 2 s .c om*/ }
From source file:org.zalando.boot.etcd.EtcdClientTest.java
@Test public void putDirWithSetTtl() throws EtcdException { server.expect(MockRestRequestMatchers.requestTo("http://localhost:2379/v2/keys/sample")) .andExpect(MockRestRequestMatchers.method(HttpMethod.PUT)) .andExpect(MockRestRequestMatchers.content().contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(MockRestRequestMatchers.content().string("dir=true&ttl=60")).andRespond( MockRestResponseCreators.withSuccess(new ClassPathResource("EtcdClientTest_delete.json"), MediaType.APPLICATION_JSON)); EtcdResponse response = client.putDir("sample", 60); Assert.assertNotNull("response", response); server.verify();/* w w w .j a v a 2 s .c o m*/ }
From source file:org.mitreid.multiparty.web.ResourceController.java
/** * @param incomingAccessToken/*from w ww . j a v a2 s . co m*/ * @param server * @param client * @param protectionAccessTokenValue * @return */ private JsonObject introspectToken(String incomingAccessToken, MultipartyServerConfiguration server, RegisteredClient client, String protectionAccessTokenValue) { // POST to the introspection endpoint and get the results MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("token", incomingAccessToken); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.add("Authorization", "Bearer " + protectionAccessTokenValue); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); HttpEntity<String> responseEntity = restTemplate.postForEntity(server.getIntrospectionEndpointUri(), request, String.class); JsonObject rso = parser.parse(responseEntity.getBody()).getAsJsonObject(); return rso; }
From source file:com.muk.services.api.impl.PayPalPaymentService.java
private String getTokenHeader() { final Cache cache = cacheManager.getCache(ServiceConstants.CacheNames.paymentApiTokenCache); final String token = "paypal"; ValueWrapper valueWrapper = cache.get(token); String cachedHeader = StringUtils.EMPTY; if (valueWrapper == null || valueWrapper.get() == null) { try {//from w w w. j a v a 2 s . com final String value = securityCfgService.getPayPalClientId() + ":" + keystoreService.getPBEKey(securityCfgService.getPayPalClientId()); final HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8)); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); headers.add(HttpHeaders.AUTHORIZATION, "Basic " + nonceService.encode(value.getBytes(StandardCharsets.UTF_8))); final MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>(); body.add("grant_type", "client_credentials"); final HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>( body, headers); final ResponseEntity<JsonNode> response = restTemplate.postForEntity( securityCfgService.getPayPalUri() + "/oauth2/token", request, JsonNode.class); cache.put(token, response.getBody().get("access_token").asText()); valueWrapper = cache.get(token); cachedHeader = (String) valueWrapper.get(); } catch (final IOException ioEx) { LOG.error("Failed read keystore", ioEx); cachedHeader = StringUtils.EMPTY; } catch (final GeneralSecurityException secEx) { LOG.error("Failed to get key", secEx); cachedHeader = StringUtils.EMPTY; } } else { cachedHeader = (String) valueWrapper.get(); } return "Bearer " + cachedHeader; }