List of usage examples for org.springframework.http RequestEntity RequestEntity
public RequestEntity(@Nullable T body, @Nullable MultiValueMap<String, String> headers,
@Nullable HttpMethod method, URI url)
From source file:org.cloudfoundry.example.Controller.java
private static RequestEntity<?> getOutgoingRequest(RequestEntity<?> incoming) { HttpHeaders headers = new HttpHeaders(); headers.putAll(incoming.getHeaders()); URI uri = headers.remove(FORWARDED_URL).stream().findFirst().map(URI::create) .orElseThrow(() -> new IllegalStateException(String.format("No %s header present", FORWARDED_URL))); return new RequestEntity<>(incoming.getBody(), headers, incoming.getMethod(), uri); }
From source file:com.github.ljtfreitas.restify.http.spring.client.request.RequestEntityConverter.java
@Override public RequestEntity<Object> convert(EndpointRequest source) { Object body = source.body().orElse(null); HttpHeaders headers = headersOf(source.headers()); HttpMethod method = HttpMethod.resolve(source.method()); return new RequestEntity<>(body, headers, method, source.endpoint()); }
From source file:io.pivotalservices.wiretaprouteservice.CatchAllController.java
private static RequestEntity<?> getOutgoingRequest(RequestEntity<?> incoming) { HttpHeaders headers = new HttpHeaders(); headers.putAll(incoming.getHeaders()); URI uri = headers.remove(FORWARDED_URL).stream().findFirst().map(URI::create) .orElseThrow(() -> new IllegalStateException(String.format("No %s header present", FORWARDED_URL))); return new RequestEntity<>(incoming.getBody(), headers, incoming.getMethod(), uri); }
From source file:com.ginema.ApplicationTests.java
@Test public void loginSucceeds() { ResponseEntity<String> response = template.getForEntity("http://localhost:" + port + "/ginema-server/login", String.class); String csrf = getCsrf(response.getBody()); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "password"); 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 + "/ginema-server/login")); ResponseEntity<Void> location = template.exchange(request, Void.class); assertEquals("http://localhost:" + port + "/ginema-server/", location.getHeaders().getFirst("Location")); }
From source file:io.bosh.client.internal.AbstractSpringOperations.java
protected final <T, R> Observable<ResponseEntity<R>> exchangeForEntity(T request, Class<R> responseType, HttpHeaders headers, HttpMethod method, Consumer<UriComponentsBuilder> builderCallback) { return createObservable(() -> { UriComponentsBuilder builder = UriComponentsBuilder.fromUri(this.root); builderCallback.accept(builder); URI uri = builder.build().toUri(); RequestEntity<T> requestEntity = new RequestEntity<T>(request, headers, HttpMethod.PUT, uri); this.logger.debug("{} {}", method, uri); return this.restOperations.exchange(requestEntity, responseType); });//from w w w. j ava2s .c om }
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:org.zalando.boot.etcd.EtcdClient.java
/** * Executes the given method on the given location using the given request * data./*w ww .ja v a2 s . c om*/ * * @param uri * the location * @param method * the HTTP method * @param requestData * the request data * @return the etcd response * @throws EtcdException * in case etcd returned an error */ private <T> T execute(UriComponentsBuilder uriTemplate, HttpMethod method, MultiValueMap<String, String> requestData, Class<T> responseType) throws EtcdException { long startTimeMillis = System.currentTimeMillis(); int retry = -1; ResourceAccessException lastException = null; do { lastException = null; URI uri = uriTemplate.buildAndExpand(locations[locationIndex]).toUri(); RequestEntity<MultiValueMap<String, String>> requestEntity = new RequestEntity<>(requestData, null, method, uri); try { ResponseEntity<T> responseEntity = template.exchange(requestEntity, responseType); return responseEntity.getBody(); } catch (HttpStatusCodeException e) { EtcdError error = null; try { error = responseConverter.getObjectMapper().readValue(e.getResponseBodyAsByteArray(), EtcdError.class); } catch (IOException ex) { error = null; } throw new EtcdException(error, "Failed to execute " + requestEntity + ".", e); } catch (ResourceAccessException e) { log.debug("Failed to execute " + requestEntity + ", retrying if possible.", e); if (locationIndex == locations.length - 1) { locationIndex = 0; } else { locationIndex++; } lastException = e; } } while (retry <= retryCount && System.currentTimeMillis() - startTimeMillis < retryDuration); if (lastException != null) { throw lastException; } else { return null; } }
From source file:org.cloudfoundry.identity.uaa.integration.feature.AutologinIT.java
@Test public void testSimpleAutologinFlow() throws Exception { HttpHeaders headers = getAppBasicAuthHttpHeaders(); LinkedMultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>(); requestBody.add("username", testAccounts.getUserName()); requestBody.add("password", testAccounts.getPassword()); //generate an autologin code with our credentials ResponseEntity<Map> autologinResponseEntity = restOperations.exchange(baseUrl + "/autologin", HttpMethod.POST, new HttpEntity<>(requestBody.toSingleValueMap(), headers), Map.class); String autologinCode = (String) autologinResponseEntity.getBody().get("code"); //start the authorization flow - this will issue a login event //by using the autologin code String authorizeUrl = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/oauth/authorize") .queryParam("redirect_uri", appUrl).queryParam("response_type", "code") .queryParam("client_id", "app").queryParam("code", autologinCode).build().toUriString(); //rest template that does NOT follow redirects RestTemplate template = new RestTemplate(new DefaultIntegrationTestConfig.HttpClientFactory()); headers.remove("Authorization"); headers.add(HttpHeaders.ACCEPT, MediaType.TEXT_HTML_VALUE); ResponseEntity<String> authorizeResponse = template.exchange(authorizeUrl, HttpMethod.GET, new HttpEntity<>(new HashMap<String, String>(), headers), String.class); //we are now logged in. retrieve the JSESSIONID List<String> cookies = authorizeResponse.getHeaders().get("Set-Cookie"); int cookiesAdded = 0; headers = getAppBasicAuthHttpHeaders(); for (String cookie : cookies) { if (cookie.startsWith("X-Uaa-Csrf=") || cookie.startsWith("JSESSIONID=")) { headers.add("Cookie", cookie); cookiesAdded++;// w w w . j a v a 2 s. c om } } assertEquals(2, cookiesAdded); //if we receive a 200, then we must approve our scopes if (HttpStatus.OK == authorizeResponse.getStatusCode()) { authorizeUrl = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/oauth/authorize") .queryParam("user_oauth_approval", "true") .queryParam(DEFAULT_CSRF_COOKIE_NAME, IntegrationTestUtils.extractCookieCsrf(authorizeResponse.getBody())) .build().toUriString(); authorizeResponse = template.exchange(authorizeUrl, HttpMethod.POST, new HttpEntity<>(new HashMap<String, String>(), headers), String.class); } //approval is complete, we receive a token code back assertEquals(HttpStatus.FOUND, authorizeResponse.getStatusCode()); List<String> location = authorizeResponse.getHeaders().get("Location"); assertEquals(1, location.size()); String newCode = location.get(0).substring(location.get(0).indexOf("code=") + 5); //request a token using our code String tokenUrl = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/oauth/token").build().toUriString(); MultiValueMap<String, String> tokenParams = new LinkedMultiValueMap<>(); tokenParams.add("response_type", "token"); tokenParams.add("grant_type", GRANT_TYPE_AUTHORIZATION_CODE); tokenParams.add("code", newCode); tokenParams.add("redirect_uri", appUrl); headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE); headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); RequestEntity<MultiValueMap<String, String>> requestEntity = new RequestEntity<>(tokenParams, headers, HttpMethod.POST, new URI(tokenUrl)); ResponseEntity<Map> tokenResponse = template.exchange(requestEntity, Map.class); assertEquals(HttpStatus.OK, tokenResponse.getStatusCode()); //here we must reset our state. we do that by following the logout flow. headers.clear(); BasicCookieStore cookieStore = new BasicCookieStore(); ResponseEntity<String> loginResponse = template.exchange(baseUrl + "/login", HttpMethod.GET, new HttpEntity<>(null, getHeaders(cookieStore)), String.class); setCookiesFromResponse(cookieStore, loginResponse); String csrf = IntegrationTestUtils.extractCookieCsrf(loginResponse.getBody()); requestBody.add(DEFAULT_CSRF_COOKIE_NAME, csrf); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); loginResponse = restOperations.exchange(baseUrl + "/login.do", HttpMethod.POST, new HttpEntity<>(requestBody, getHeaders(cookieStore)), String.class); cookies = loginResponse.getHeaders().get("Set-Cookie"); assertThat(cookies, hasItem(startsWith("JSESSIONID"))); assertThat(cookies, hasItem(startsWith("X-Uaa-Csrf"))); if (IdentityZoneHolder.get().getConfig().isAccountChooserEnabled()) { assertThat(cookies, hasItem(startsWith("Saved-Account-"))); } assertThat(cookies, hasItem(startsWith("Current-User"))); cookieStore.clear(); setCookiesFromResponse(cookieStore, loginResponse); headers.add(HttpHeaders.ACCEPT, MediaType.TEXT_HTML_VALUE); ResponseEntity<String> profilePage = restOperations.exchange(baseUrl + "/profile", HttpMethod.GET, new HttpEntity<>(null, getHeaders(cookieStore)), String.class); setCookiesFromResponse(cookieStore, profilePage); String revokeApprovalsUrl = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/profile").build() .toUriString(); requestBody.clear(); requestBody.add("clientId", "app"); requestBody.add("delete", ""); requestBody.add(DEFAULT_CSRF_COOKIE_NAME, IntegrationTestUtils.extractCookieCsrf(profilePage.getBody())); ResponseEntity<Void> revokeResponse = template.exchange(revokeApprovalsUrl, HttpMethod.POST, new HttpEntity<>(requestBody, getHeaders(cookieStore)), Void.class); assertEquals(HttpStatus.FOUND, revokeResponse.getStatusCode()); }