List of usage examples for org.springframework.http HttpHeaders setAccept
public void setAccept(List<MediaType> acceptableMediaTypes)
From source file:com.expedia.client.WunderGroundClient.java
@Override public ResponseEntity<ResponseWrapper> getJSONResponse(Object request) { ResponseEntity<ResponseWrapper> responseEntity = null; Weather weather = null;/* w w w. java 2s . c o m*/ if (request instanceof Weather) { weather = (Weather) request; List<MediaType> mediaTypes = new ArrayList<MediaType>(); mediaTypes.add(MediaType.APPLICATION_JSON); HttpHeaders headers = new HttpHeaders(); headers.setAccept(mediaTypes); HttpEntity<Weather> httpEntity = new HttpEntity<Weather>(null, headers); try { System.out.println("Hitting weather service for JSON response!"); responseEntity = restTemplate.exchange(weatherServiceJsonUrl, HttpMethod.GET, httpEntity, ResponseWrapper.class, weatherApiKey, weather.getZipCode()); } catch (RuntimeException e) { e.printStackTrace(); weather.setErrorDesc("Get failed" + e.getMessage()); } } return responseEntity; }
From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java
private HttpHeaders getHeaders(String auth) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); byte[] encodedAuthorisation = Base64.encodeBase64(auth.getBytes()); headers.add("Authorization", "Basic " + new String(encodedAuthorisation)); return headers; }
From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java
@Test public void testDenied() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "user"); form.set("password", "user"); getCsrf(form, headers);//from w ww . j a va 2s . co m ResponseEntity<String> entity = new TestRestTemplate().exchange("http://localhost:" + this.port + "/login", HttpMethod.POST, new HttpEntity<MultiValueMap<String, String>>(form, headers), String.class); assertEquals(HttpStatus.FOUND, entity.getStatusCode()); String cookie = entity.getHeaders().getFirst("Set-Cookie"); headers.set("Cookie", cookie); ResponseEntity<String> page = new TestRestTemplate().exchange(entity.getHeaders().getLocation(), HttpMethod.GET, new HttpEntity<Void>(headers), String.class); assertEquals(HttpStatus.FORBIDDEN, page.getStatusCode()); assertTrue("Wrong body (message doesn't match):\n" + entity.getBody(), page.getBody().contains("Access denied")); }
From source file:com.logaritex.hadoop.configuration.manager.http.AndroidHttpService.java
private HttpHeaders createHttpHeaders(String username, String password) { HttpBasicAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setAuthorization(authHeader); httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); httpHeaders.setAcceptEncoding(ContentCodingType.GZIP); return httpHeaders; }
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:com.epam.reportportal.gateway.CompositeInfoEndpoint.java
private Map<String, ?> composeInfo() { return eurekaClient.getApplications().getRegisteredApplications().stream() .flatMap(app -> app.getInstances().stream()).map(instanceInfo -> { try { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); return new AbstractMap.SimpleImmutableEntry<>(instanceInfo.getAppName(), restTemplate.exchange(instanceInfo.getStatusPageUrl(), HttpMethod.GET, new HttpEntity<>(null, headers), Map.class).getBody()); } catch (Exception e) { LOGGER.error("Unable to obtain service info", e); return new AbstractMap.SimpleImmutableEntry<>(instanceInfo.getAppName(), "DOWN"); }// ww w . j av a 2s . c om }).collect(toMap(AbstractMap.SimpleImmutableEntry::getKey, AbstractMap.SimpleImmutableEntry::getValue, (value1, value2) -> value2)); }
From source file:cz.jirutka.spring.exhandler.handlers.HttpMediaTypeNotSupportedExceptionHandler.java
@Override protected HttpHeaders createHeaders(HttpMediaTypeNotSupportedException ex, HttpServletRequest req) { HttpHeaders headers = super.createHeaders(ex, req); List<MediaType> mediaTypes = ex.getSupportedMediaTypes(); if (!isEmpty(mediaTypes)) { headers.setAccept(mediaTypes); }/*w ww . j a v a 2 s . c om*/ return headers; }
From source file:uta.ak.TestNodejsInterface.java
private void testFromDB() throws Exception { Connection con = null; //MYSQL Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/USTTMP", "root", "root.123"); //MYSQL System.out.println("connection yes"); System.out.println("query records..."); String querySQL = "SELECT" + " * " + "FROM " + " c_rawtext " + "WHERE " + "tag like 'function%'"; PreparedStatement preparedStmt = con.prepareStatement(querySQL); ResultSet rs = preparedStmt.executeQuery(); Set<String> filterDupSet = new HashSet<>(); Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1);/*from w w w . jav a 2s.c o m*/ SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); while (rs.next()) { System.out.println(rs.getString("title") + " " + rs.getString("text") + " " + rs.getString("tag")); String formattedDate = format1.format(new Date()); String interfaceMsg = "<message> " + " <title> " + rs.getString("title") + " </title> " + " <text> " + StringEscapeUtils.escapeXml10(rs.getString("text")) + " </text> " + " <textCreatetime> " + formattedDate + " </textCreatetime> " + " <tag> " + rs.getString("tag") + " </tag> " + "</message>"; // String restUrl="http://192.168.0.103:8991/usttmp_textreceiver/rest/addText"; String restUrl = "http://127.0.0.1:8991/usttmp_textreceiver/rest/addText"; RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_XML); headers.setAccept(Arrays.asList(MediaType.TEXT_XML)); // headers.setContentLength(); HttpEntity<String> entity = new HttpEntity<String>(interfaceMsg, headers); ResponseEntity<String> result = restTemplate.exchange(restUrl, HttpMethod.POST, entity, String.class); System.out.println(result.getBody()); } }
From source file:com.epam.reportportal.gateway.CompositeInfoEndpoint.java
@RequestMapping(value = "/composite/{endpoint}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseBody//from w ww . j a v a2s. com public Map<String, ?> compose(@PathVariable("endpoint") String endpoint) { return discoveryClient.getServices().stream() .map((Function<String, AbstractMap.SimpleImmutableEntry<String, Object>>) service -> { try { List<ServiceInstance> instances = discoveryClient.getInstances(service); if (instances.isEmpty()) { return new AbstractMap.SimpleImmutableEntry<>(service, "DOWN"); } ServiceInstance instance = instances.get(0); String protocol = instance.isSecure() ? "https" : "http"; HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); return new AbstractMap.SimpleImmutableEntry<>(instance.getServiceId(), loadBalancedRestTemplate.exchange(protocol + "://{service}/{endpoint}", HttpMethod.GET, new HttpEntity<>(null, headers), Map.class, instance.getServiceId(), endpoint).getBody()); } catch (Exception e) { return new AbstractMap.SimpleImmutableEntry<>(service, "DOWN"); } }).collect(toMap(AbstractMap.SimpleImmutableEntry::getKey, AbstractMap.SimpleImmutableEntry::getValue, (value1, value2) -> value2)); }
From source file:com.github.ffremont.microservices.springboot.node.services.MsService.java
public List<MicroServiceRest> getMicroServices() { MasterUrlBuilder builder = new MasterUrlBuilder(cluster, node, masterhost, masterPort, masterCR); 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()) ? new ArrayList<>(Arrays.asList(response.getBody())) : null;/*from w w w .jav a 2 s . c o m*/ }