List of usage examples for org.springframework.http HttpHeaders add
@Override public void add(String headerName, @Nullable String headerValue)
From source file:com.counter.counter.api.MainController.java
@RequestMapping(value = "/top/{t}", method = { RequestMethod.GET }) public ResponseEntity<String> top(@PathVariable Integer t) { final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Content-Type", "text/csv;charset=utf-8"); List<Word> ss = null;/*from w w w . j a v a 2s. c o m*/ StringBuilder tempSb = new StringBuilder(); ss = findTopWords(); for (int i = 0; i <= t; i++) { Word word = ss.get(i); tempSb.append(word.word + "|" + word.count + " "); } //return tempSb.toString(); return new ResponseEntity<>(tempSb.toString(), httpHeaders, HttpStatus.OK); }
From source file:org.cloudfoundry.identity.uaa.integration.VmcScimUserEndpointIntegrationTests.java
@Test public void userInfoSucceeds() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("Accept", "*/*"); RestOperations client = serverRunning.getRestTemplate(); ResponseEntity<Void> result = client.exchange(serverRunning.getUrl("/userinfo"), HttpMethod.GET, new HttpEntity<Void>(null, headers), null, joe.getId()); assertEquals(HttpStatus.OK, result.getStatusCode()); }
From source file:com.gopivotal.cla.github.GitHubConditionalTest.java
@Test public void withNextLink() { HttpHeaders headers = new HttpHeaders(); headers.add("Link", "<" + URL + ">; rel=\"first\""); headers.add("Link", "<" + LINK_URL + ">; rel=\"next\""); ResponseEntity<Set> response1 = new ResponseEntity<Set>(Sets.asSet(), headers, HttpStatus.OK); when(this.restOperations.exchange(URL, HttpMethod.GET, REQUEST_ENTITY, Set.class)).thenReturn(response1); ResponseEntity<Set> response2 = new ResponseEntity<Set>(Sets.asSet(), HttpStatus.OK); when(this.restOperations.exchange(LINK_URL, HttpMethod.GET, REQUEST_ENTITY, Set.class)) .thenReturn(response2);/*from w w w . j av a2s . c o m*/ this.gitHubType.getTrigger(); assertTrue(this.gitHubType.initializedCalled); }
From source file:eu.freme.common.exception.ExceptionHandlerService.java
public ResponseEntity<String> handleError(HttpServletRequest req, Throwable exception) { logger.error("Request: " + req.getRequestURL() + " raised ", exception); HttpStatus statusCode = null;//from w w w . jav a 2s. c om if (exception instanceof MissingServletRequestParameterException) { // create response for spring exceptions statusCode = HttpStatus.BAD_REQUEST; } else if (exception instanceof FREMEHttpException && ((FREMEHttpException) exception).getHttpStatusCode() != null) { // get response code from FREMEHttpException statusCode = ((FREMEHttpException) exception).getHttpStatusCode(); } else if (exception instanceof AccessDeniedException) { statusCode = HttpStatus.UNAUTHORIZED; } else if (exception instanceof HttpMessageNotReadableException) { statusCode = HttpStatus.BAD_REQUEST; } else { // get status code from exception class annotation Annotation responseStatusAnnotation = exception.getClass().getAnnotation(ResponseStatus.class); if (responseStatusAnnotation instanceof ResponseStatus) { statusCode = ((ResponseStatus) responseStatusAnnotation).value(); } else { // set default status code 500 statusCode = HttpStatus.INTERNAL_SERVER_ERROR; } } JSONObject json = new JSONObject(); json.put("status", statusCode.value()); json.put("message", exception.getMessage()); json.put("error", statusCode.getReasonPhrase()); json.put("timestamp", new Date().getTime()); json.put("exception", exception.getClass().getName()); json.put("path", req.getRequestURI()); if (exception instanceof AdditionalFieldsException) { Map<String, JSONObject> additionalFields = ((AdditionalFieldsException) exception) .getAdditionalFields(); for (String key : additionalFields.keySet()) { json.put(key, additionalFields.get(key)); } } HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "application/json"); return new ResponseEntity<String>(json.toString(2), responseHeaders, statusCode); }
From source file:io.neba.core.mvc.MultipartSlingHttpServletRequest.java
@Override public HttpHeaders getMultipartHeaders(String name) { String contentType = getMultipartContentType(name); if (contentType != null) { HttpHeaders headers = new HttpHeaders(); headers.add(CONTENT_TYPE, contentType); return headers; } else {//from w w w . j a v a 2 s . com return null; } }
From source file:com.cloudera.nav.plugin.client.NavApiCient.java
private HttpHeaders getAuthHeaders() { // basic authentication with base64 encoding String plainCreds = String.format("%s:%s", config.getUsername(), config.getPassword()); byte[] plainCredsBytes = plainCreds.getBytes(); byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes); String base64Creds = new String(base64CredsBytes); HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "Basic " + base64Creds); return headers; }
From source file:com.counter.counter.api.MainController.java
@RequestMapping(value = "/search", method = { RequestMethod.POST }) public ResponseEntity<String> searchText(@RequestParam List<String> searchText) throws JsonProcessingException, JSONException { final HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("Content-Type", "application/json;charset=utf-8"); String rVal = ""; //System.out.println("searchText-> " + searchText.size()); List<String> tempList = new ArrayList(); Map<String, Integer> map; JSONObject jsonMap = new JSONObject(); for (String st : searchText) { Integer occurence = 0;//from w w w . j a v a 2 s.c om map = new HashMap(); for (int i = sourceText.indexOf(st); i >= 0; i = sourceText.indexOf(st, i + 1)) occurence++; map.put(st, occurence); jsonMap.put(st, occurence); String json = new ObjectMapper().writeValueAsString(map); System.out.println(json); tempList.add(json); } StringBuilder sb = new StringBuilder(); JSONArray jsonArray = new JSONArray(); jsonArray.put(jsonMap); System.out.println(jsonArray.toString()); for (String s : tempList) { sb.append(s); sb.append(","); } sb.deleteCharAt(sb.lastIndexOf(",")); //String json = new ObjectMapper().writeValueAsString(map); //System.out.println(json); return new ResponseEntity<>("[" + sb.toString() + "]", httpHeaders, HttpStatus.OK); }
From source file:org.cloudfoundry.client.lib.oauth2.OauthClient.java
@SuppressWarnings({ "rawtypes", "unchecked" }) public void changePassword(String oldPassword, String newPassword) { HttpHeaders headers = new HttpHeaders(); headers.add(AUTHORIZATION_HEADER_KEY, token.getTokenType() + " " + token.getValue()); HttpEntity info = new HttpEntity(headers); ResponseEntity<String> response = restTemplate.exchange(authorizationUrl + "/userinfo", HttpMethod.GET, info, String.class); Map<String, Object> responseMap = JsonUtil.convertJsonToMap(response.getBody()); String userId = (String) responseMap.get("user_id"); Map<String, Object> body = new HashMap<String, Object>(); body.put("schemas", new String[] { "urn:scim:schemas:core:1.0" }); body.put("password", newPassword); body.put("oldPassword", oldPassword); HttpEntity<Map> httpEntity = new HttpEntity<Map>(body, headers); restTemplate.put(authorizationUrl + "/User/{id}/password", httpEntity, userId); }
From source file:uk.ac.ebi.emma.controller.BackgroundManagementListController.java
/** * Deletes the background identified by <b>id</b>. This method is configured as * a GET because it is intended to be called as an ajax call. Using GET * avoids re-posting problems with the back button. NOTE: It is the caller's * responsibility to insure there are no foreign key constraints. * //from w w w . ja va 2 s. co m * @param background_key primary key of the background to be deleted * @return a JSON string containing 'status' [ok or fail], and a message [ * empty string if status is ok; error message otherwise] */ @RequestMapping(value = "/deleteBackground", method = RequestMethod.GET) @ResponseBody public ResponseEntity<String> deleteBackground(@RequestParam int background_key) { String status, message; try { backgroundManager.delete(background_key); status = "ok"; message = ""; } catch (Exception e) { status = "fail"; message = e.getLocalizedMessage(); } JSONObject returnStatus = new JSONObject(); returnStatus.put("status", status); returnStatus.put("message", message); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json; charset=utf-8"); return new ResponseEntity(returnStatus.toJSONString(), headers, HttpStatus.OK); }
From source file:org.dawnsci.marketplace.controllers.ExtendedRestApiController.java
/** * Used by the client software to obtain a CSRF token to use in further * communication with the server.// w w w.j a va2 s .com */ @ResponseBody @RequestMapping(value = "/token") public ResponseEntity<String> login(HttpServletRequest request) { CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); HttpHeaders headers = new HttpHeaders(); headers.add(token.getHeaderName(), token.getToken()); return new ResponseEntity<>("you got your token", headers, HttpStatus.OK); }