List of usage examples for org.springframework.http HttpHeaders setAccept
public void setAccept(List<MediaType> acceptableMediaTypes)
From source file:com.muk.services.util.GoogleOAuthService.java
private String requestAccessToken(String jwt) { final HttpHeaders headers = new HttpHeaders(); String token = "error"; headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); final MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>(); body.add("grant_type", TOKEN_GRANT); body.add("assertion", jwt); try {/*from ww w . j a v a2s. co m*/ final ResponseEntity<JsonNode> response = restTemplate.postForEntity(JWT_AUD, new HttpEntity<MultiValueMap<String, String>>(body, headers), JsonNode.class); token = response.getBody().get("access_token").asText(); } catch (final HttpClientErrorException httpClientEx) { LOG.error("Failed to request token.", httpClientEx); } return token; }
From source file:com.orange.ngsi.client.NgsiClient.java
/** * The default HTTP request headers, depends on the host supporting xml or json * @param url/*from w w w. j av a2 s .c o m*/ * @return the HTTP request headers. */ public HttpHeaders getRequestHeaders(String url, HttpServletRequest request) { HttpHeaders requestHeaders = new HttpHeaders(); MediaType mediaType = MediaType.APPLICATION_JSON; // if (request != null && request.getAttribute("Content-Type") != null // && !request.getAttribute("Content-Type").equals("application/json")){ //// MediaType mediaType = MediaType.APPLICATION_JSON; // if (url == null || protocolRegistry.supportXml(url)) { // mediaType = MediaType.APPLICATION_XML; // } // } if ((url == null || protocolRegistry.supportXml(url)) && !isJsonContentType(request)) { mediaType = MediaType.APPLICATION_XML; } requestHeaders.setContentType(mediaType); requestHeaders.setAccept(Collections.singletonList(mediaType)); if (request != null) { requestHeaders.add("Fiware-Service", request.getHeader("Fiware-Service")); requestHeaders.add("Fiware-ServicePath", request.getHeader("Fiware-ServicePath")); } return requestHeaders; }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
private <R> R get(Path path, Function<UriComponentsBuilder, UriComponentsBuilder> filterFunct, ParameterizedTypeReference<R> responseEntity) { HttpHeaders headers = new HttpHeaders(); headers.setAccept(ACCEPT_TYPES); ResponseEntity<R> resp = this.template.exchange( (filterFunct != null ? filterFunct.apply(base(path)) : base(path)).build().toUri(), HttpMethod.GET, new HttpEntity<Object>(headers), responseEntity); return handle(resp); }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
private <R> R post(Path path, Object body, MediaType mediaType, Class<R> resultType) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(mediaType);/* w ww . j av a 2s .co m*/ headers.setAccept(ACCEPT_TYPES); return this.template.postForObject(base(path).build().toUri(), new HttpEntity<>(body, headers), resultType); }
From source file:com.thinkbiganalytics.metadata.rest.client.MetadataClient.java
private <R> R put(Path path, Object body, MediaType mediaType, Class<R> resultType) { URI uri = base(path).build().toUri(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(mediaType);/*from w ww. j a va 2 s .c om*/ headers.setAccept(ACCEPT_TYPES); this.template.put(uri, new HttpEntity<>(body, headers)); // Silly that put() doesn't return an object. if (resultType != null) { return get(path, resultType); } else { return null; } }
From source file:com.vmware.bdd.cli.rest.RestClient.java
private HttpHeaders buildHeaders(boolean withCookie) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); List<MediaType> acceptedTypes = new ArrayList<MediaType>(); acceptedTypes.add(MediaType.APPLICATION_JSON); acceptedTypes.add(MediaType.TEXT_HTML); headers.setAccept(acceptedTypes); if (withCookie) { String cookieInfo = readCookieInfo(); headers.add("Cookie", cookieInfo == null ? "" : cookieInfo); }// ww w. ja v a 2 s.c o m return headers; }
From source file:es.onebox.rest.utils.service.QueryService.java
/** * Main method to perform request to Onebox REST API. * * @param authenticationForm/*w w w . ja v a2 s.c om*/ * @param queryForm * @return Response form request */ public ResponseDTO query(AuthenticationForm authenticationForm, QueryForm queryForm) { ResponseDTO responseDTO = new ResponseDTO(); Exception ex = null; try { URL url = new URL(queryForm.getUrl()); URI uri = url.toURI(); Date date = new Date(); long timestamp = date.getTime(); HttpMethod httpMethod; if (queryForm.getMethod().equalsIgnoreCase("post")) { httpMethod = HttpMethod.POST; } else { httpMethod = HttpMethod.GET; } // Getting String to encode with HMAC-SHA1 // First step in the signing algorithm String stringToSign = getStringToSign(uri, httpMethod.name(), timestamp, queryForm); logger.info("String to sign: " + stringToSign); // Encoding String // This is the actual authorization string that will be sent in the request String authorization = generate_HMAC_SHA1_Signature(stringToSign, authenticationForm.getPassword() + authenticationForm.getLicense()); logger.info("Authorization string: " + authorization); // Adding to return object responseDTO.setDate(date); responseDTO.setStringToSign(stringToSign); responseDTO.setAuthorization(authorization); // Setting Headers HttpHeaders headers = new HttpHeaders(); if (queryForm.getAccept().equals("json")) { headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); } else { headers.setAccept(Arrays.asList(MediaType.TEXT_XML)); } headers.add("Authorization", authorization); headers.add("OB_DATE", "" + timestamp); headers.add("OB_Terminal", authenticationForm.getTerminal()); headers.add("OB_User", authenticationForm.getUser()); headers.add("OB_Channel", authenticationForm.getChannelId()); headers.add("OB_POS", authenticationForm.getPos()); // Adding Headers to return object responseDTO.setHttpHeaders(headers); HttpEntity<String> entity; if (httpMethod == HttpMethod.POST) { // Adding post parameters to POST body String parameterStringBody = queryForm.getParametersAsString(); entity = new HttpEntity<String>(parameterStringBody, headers); logger.info("POST Body: " + parameterStringBody); } else { entity = new HttpEntity<String>(headers); } // Creating rest client RestTemplate restTemplate = new RestTemplate(); // Converting to UTF-8. OB Rest replies in windows charset. restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); // Performing request to Onebox REST API ResponseEntity<String> result = restTemplate.exchange(uri, httpMethod, entity, String.class); // TODO this functionlity is to map response to objetcs. It is not finished. Only placed here for POC /* if (queryForm.getMapResponse().booleanValue()) { ResponseEntity<EventSearchBean> event = restTemplate.exchange(uri, httpMethod, entity, EventSearchBean.class); } */ // Adding response to return object responseDTO.setResponseEntity(result); logger.debug(result.toString()); } catch (HttpClientErrorException e) { logger.error(e.getMessage()); ex = e; e.printStackTrace(); responseDTO.setError(e); responseDTO.setAdditionalErrorMessage(AppUtils.getMessage("error.request.parameters")); } catch (MalformedURLException e) { logger.error(e.getMessage()); ex = e; e.printStackTrace(); responseDTO.setError(e); responseDTO.setAdditionalErrorMessage(AppUtils.getMessage("error.request.parameters")); } catch (SignatureException e) { logger.error(e.getMessage()); ex = e; e.printStackTrace(); responseDTO.setError(e); responseDTO.setAdditionalErrorMessage(AppUtils.getMessage("error.request.parameters")); } catch (URISyntaxException e) { logger.error(e.getMessage()); ex = e; e.printStackTrace(); responseDTO.setError(e); responseDTO.setAdditionalErrorMessage(AppUtils.getMessage("error.request.parameters")); } catch (Exception e) { logger.error(e.getMessage()); ex = e; e.printStackTrace(); responseDTO.setError(e); responseDTO.setAdditionalErrorMessage(AppUtils.getMessage("error.request.authentication")); } finally { if (ex != null && ex instanceof HttpServerErrorException) { HttpServerErrorException e2 = (HttpServerErrorException) ex; ResponseEntity<String> responseEntity = new ResponseEntity<String>(e2.getResponseHeaders(), HttpStatus.INTERNAL_SERVER_ERROR); List<String> ob_error_codes = e2.getResponseHeaders().get("OB_Error_Code"); String ob_error_code; ResponseErrorCodesEnum ob_error = null; if (ob_error_codes != null && ob_error_codes.size() == 1) { ob_error_code = ob_error_codes.get(0); try { ob_error = ResponseErrorCodesEnum.valueOf("ERROR_" + ob_error_code); } catch (Exception e) { logger.error("API ERROR CODE NOT DEFINED: " + "ERROR_" + ob_error_code); } responseDTO.setObResponseErrorCode(ob_error); } responseDTO.setResponseEntity(responseEntity); } } return responseDTO; }
From source file:io.spring.initializr.web.AbstractInitializrIntegrationTests.java
protected String htmlHome() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML)); return restTemplate.exchange(createUrl("/"), HttpMethod.GET, new HttpEntity<Void>(headers), String.class) .getBody();/*from ww w .j a v a 2 s . c o m*/ }
From source file:io.spring.initializr.web.AbstractInitializrIntegrationTests.java
protected <T> ResponseEntity<T> execute(String contextPath, Class<T> responseType, String userAgentHeader, String... acceptHeaders) { HttpHeaders headers = new HttpHeaders(); if (userAgentHeader != null) { headers.set("User-Agent", userAgentHeader); }/*from w ww .jav a2 s .c o m*/ if (acceptHeaders != null) { List<MediaType> mediaTypes = new ArrayList<>(); for (String acceptHeader : acceptHeaders) { mediaTypes.add(MediaType.parseMediaType(acceptHeader)); } headers.setAccept(mediaTypes); } else { headers.setAccept(Collections.emptyList()); } return restTemplate.exchange(createUrl(contextPath), HttpMethod.GET, new HttpEntity<Void>(headers), responseType); }
From source file:org.apache.fineract.infrastructure.sms.scheduler.SmsMessageScheduledJobServiceImpl.java
/** * get a new HttpEntity with the provided body **/// w w w. ja v a 2s.com private HttpEntity<String> getHttpEntity(String body, String apiAuthUsername, String apiAuthPassword) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); String authorization = apiAuthUsername + ":" + apiAuthPassword; byte[] encodedAuthorisation = Base64.encode(authorization.getBytes()); headers.add("Authorization", "Basic " + new String(encodedAuthorisation)); return new HttpEntity<String>(body, headers); }