List of usage examples for org.springframework.http HttpEntity HttpEntity
public HttpEntity(MultiValueMap<String, String> headers)
From source file:com.jvoid.core.controller.HomeController.java
@RequestMapping(value = "/login", method = RequestMethod.POST) public @ResponseBody String loginToJvoid( @RequestParam(required = false, value = "params") JSONObject jsonParams) { System.out.println("Login:jsonParams=>" + jsonParams.toString()); RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(ServerUris.CUSTOMER_SERVER_URI + URIConstants.GET_CUSTOMER_BY_EMAIL) .queryParam("params", jsonParams); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); return returnString.getBody(); }
From source file:org.springbyexample.contact.web.client.AbstractPersistenceClient.java
@Override public R update(S request) { R response = null;/*from w w w. j a v a2 s. c om*/ String url = client.createUrl(updateRequest); logger.debug("REST client update. id={} url='{}'", request.getId(), url); Map<String, Long> vars = createPkVars(request.getId()); response = client.getRestTemplate() .exchange(url, HttpMethod.PUT, new HttpEntity(request), responseClazz, vars).getBody(); return response; }
From source file:demo.RestServiceIntegrationTests.java
@Test public void findAll() { TestRestTemplate template = new TestRestTemplate(); ResponseEntity<Resource<List<ServiceLocation>>> result = template.exchange( "http://localhost:" + this.port + "/serviceLocations", HttpMethod.GET, new HttpEntity<Void>((Void) null), new ParameterizedTypeReference<Resource<List<ServiceLocation>>>() { });//from w w w. j a v a 2 s. c o m assertEquals(HttpStatus.OK, result.getStatusCode()); }
From source file:org.asqatasun.websnapshot.webapp.controller.IndexController.java
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody/* w w w .j a v a 2 s . c om*/ public HttpEntity<?> getThumbnail(@RequestParam(value = "url", required = true) String url, @RequestParam(value = "width", required = true) String width, @RequestParam(value = "height", required = true) String height, @RequestParam(value = "date", required = false) String date, @RequestParam(value = "status", required = false) boolean status, HttpServletRequest request) throws IOException { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_PNG); String asciiUrl; try { asciiUrl = new URL(url).toURI().toASCIIString(); } catch (URISyntaxException ex) { return new HttpEntity<String>("malformed url"); } String requestStatus = getRequestStatus(asciiUrl, width, height, date); // if the parameters are not valid, we return an auto-generated image // with a text that handles the error type if (!requestStatus.equalsIgnoreCase(SUCCESS_HTTP_REQUEST)) { if (request.getMethod().equals("GET")) { return createErrorMessage(requestStatus, headers, Integer.valueOf(width), Integer.valueOf(height)); } else { return new HttpEntity<String>(requestStatus); } } if (request.getMethod().equals("POST")) { Image image = imageDataService.forceImageCreation(asciiUrl, Integer.valueOf(width), Integer.valueOf(height)); return new HttpEntity<Status>(image.getStatus()); } // regarding the presence of the date parameter, we retrieve the closest // thumbnail or the latest if (date != null) { Date convertDate = convertDate(date); Image image = imageDataService.getImageFromWidthAndHeightAndUrlAndDate(Integer.valueOf(width), Integer.valueOf(height), asciiUrl, convertDate); return testImageAndReturnedIt(image, headers, Integer.valueOf(width), Integer.valueOf(height), status); } else { Image image = imageDataService.getImageFromWidthAndHeightAndUrl(Integer.valueOf(width), Integer.valueOf(height), asciiUrl, status); return testImageAndReturnedIt(image, headers, Integer.valueOf(width), Integer.valueOf(height), status); } }
From source file:ca.qhrtech.services.implementations.BGGServiceImpl.java
private HttpEntity<?> buildXmlHttpHeader() { HttpHeaders headers = new HttpHeaders(); headers.add("Accept", MediaType.APPLICATION_XML_VALUE); return new HttpEntity<>(headers); }
From source file:org.trustedanalytics.routermetrics.gathering.GatheringConfig.java
private HttpEntity<byte[]> getGorouterHeaders() { HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", "Basic " + new String( encodeBase64((gorouterProperties.getUsername() + ":" + gorouterProperties.getPassword()) .getBytes(Charset.forName("US-ASCII"))))); return new HttpEntity<>(headers); }
From source file:com.sra.biotech.submittool.persistence.service.SubmissionServiceImpl.java
@Override public List<Submission> findAllSubmission() { HttpHeaders headers = new HttpHeaders(); headers.add("Accept", MediaType.APPLICATION_JSON_VALUE); HttpEntity<String> request = new HttpEntity<String>(headers); ResponseEntity<String> response = restTemplate.exchange(getSubmissionUrlTemplate(), HttpMethod.GET, request, String.class); String responseBody = response.getBody(); try {//from w ww . j a v a 2s. co m if (RestUtil.isError(response.getStatusCode())) { ErrorResource error = objectMapper.readValue(responseBody, ErrorResource.class); throw new RestClientException("[" + error.getCode() + "] " + error.getMessage()); } else { SubmissionResources resources = objectMapper.readValue(responseBody, SubmissionResources.class); // SubmissionResources resources = restTemplate.getForObject(getSubmissionUrlTemplate(), SubmissionResources.class); if (resources == null || CollectionUtils.isEmpty(resources.getContent())) { return Collections.emptyList(); } Link listSelfLink = resources.getLink(Link.REL_SELF); Collection<SubmissionResource> content = resources.getContent(); if (!content.isEmpty()) { SubmissionResource firstSubmissionResource = content.iterator().next(); Link linkToFirstResource = firstSubmissionResource.getLink(Link.REL_SELF); System.out.println("href = " + linkToFirstResource.getHref()); System.out.println("rel = " + linkToFirstResource.getRel()); } return resources.unwrap(); // return resources; } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.blogspot.sgdev.blog.GrantByAuthorizationCodeProviderTest.java
@Test public void getJwtTokenByAuthorizationCode() throws JsonParseException, JsonMappingException, IOException, URISyntaxException { String redirectUrl = "http://localhost:" + port + "/resources/user"; ResponseEntity<String> response = new TestRestTemplate("user", "password").postForEntity( "http://localhost:" + port + "/oauth/authorize?response_type=code&client_id=normal-app&redirect_uri={redirectUrl}", null, String.class, redirectUrl); assertEquals(HttpStatus.OK, response.getStatusCode()); List<String> setCookie = response.getHeaders().get("Set-Cookie"); String jSessionIdCookie = setCookie.get(0); String cookieValue = jSessionIdCookie.split(";")[0]; HttpHeaders headers = new HttpHeaders(); headers.add("Cookie", cookieValue); response = new TestRestTemplate("user", "password").postForEntity("http://localhost:" + port + "oauth/authorize?response_type=code&client_id=normal-app&redirect_uri={redirectUrl}&user_oauth_approval=true&authorize=Authorize", new HttpEntity<Void>(headers), String.class, redirectUrl); assertEquals(HttpStatus.FOUND, response.getStatusCode()); assertNull(response.getBody());/*from w w w.j a va 2s. c om*/ String location = response.getHeaders().get("Location").get(0); URI locationURI = new URI(location); String query = locationURI.getQuery(); location = "http://localhost:" + port + "/oauth/token?" + query + "&grant_type=authorization_code&client_id=normal-app&redirect_uri={redirectUrl}"; response = new TestRestTemplate("normal-app", "").postForEntity(location, new HttpEntity<Void>(new HttpHeaders()), String.class, redirectUrl); assertEquals(HttpStatus.OK, response.getStatusCode()); HashMap jwtMap = new ObjectMapper().readValue(response.getBody(), HashMap.class); String accessToken = (String) jwtMap.get("access_token"); headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + accessToken); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/client", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/user", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/principal", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals("user", response.getBody()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/roles", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals("[{\"authority\":\"ROLE_USER\"}]", response.getBody()); }
From source file:com.github.ffremont.microservices.springboot.node.services.MsService.java
/** * Retourne un MS//from ww w. j a v a 2s .c o m * * @param msName * @return */ public MicroServiceRest getMs(String msName) { MasterUrlBuilder builder = new MasterUrlBuilder(cluster, node, masterhost, masterPort, masterCR); builder.setUri(msName); HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.parseMediaType(MS_JSON_TYPE_MIME))); HttpEntity<MicroServiceRest> entity = new HttpEntity<>(headers); ResponseEntity<MicroServiceRest> response = restTempate.exchange(builder.build(), HttpMethod.GET, entity, MicroServiceRest.class); return HttpStatus.OK.equals(response.getStatusCode()) ? response.getBody() : null; }