List of usage examples for org.springframework.http HttpHeaders HttpHeaders
public HttpHeaders()
From source file:org.openbaton.autoscaling.api.exceptions.GlobalExceptionHandler.java
@ExceptionHandler({ BadFormatException.class, NetworkServiceIntegrityException.class, WrongStatusException.class }) @ResponseStatus(value = HttpStatus.BAD_REQUEST) protected ResponseEntity<Object> handleInvalidRequest(Exception e, WebRequest request) { log.error("Exception with message " + e.getMessage() + " was thrown"); ExceptionResource exc = new ExceptionResource("Bad Request", e.getMessage()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return handleExceptionInternal(e, exc, headers, HttpStatus.UNPROCESSABLE_ENTITY, request); }
From source file:org.openbaton.vnfm.api.exceptions.GlobalExceptionHandler.java
@ExceptionHandler({ BadFormatException.class, NetworkServiceIntegrityException.class, WrongStatusException.class }) @ResponseStatus(value = HttpStatus.BAD_REQUEST) protected ResponseEntity<Object> handleInvalidRequest(Exception e, WebRequest request) { log.error("Exception with message " + e.getMessage() + " was thrown"); ExceptionResource exc = new ExceptionResource("Bad Request", e.getMessage()); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return handleExceptionInternal(e, exc, headers, HttpStatus.BAD_REQUEST, request); }
From source file:org.asqatasun.websnapshot.webapp.controller.IndexController.java
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody/*from w ww . j av a 2 s . c o m*/ 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:org.jnrain.mobile.accounts.kbs.KBSRegisterRequest.java
@Override public KBSRegisterResult loadDataFromNetwork() throws Exception { MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); if (isPreflight) { // preflight request, only prereg flag is sent params.set("prereg", "1"); } else {/*from w w w . j a v a 2s. co m*/ // please keep this in sync with rainstyle/apiregister.php params.set("userid", _uid); params.set("password", _password); params.set("nickname", _nickname); params.set("realname", _realname); params.set("email", _email); params.set("phone", _phone); params.set("idnumber", _idnumber); params.set("gender", Integer.toString(_gender)); params.set("captcha", _captcha); } HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String, String>> req = new HttpEntity<MultiValueMap<String, String>>(params, headers); return getRestTemplate().postForObject("http://bbs.jnrain.com/rainstyle/apiregister.php", req, KBSRegisterResult.class); }
From source file:org.openlmis.fulfillment.web.BaseController.java
ResponseEntity<String> getAuditLogResponse(Map<UUID, Class> pairs, String author, String changedPropertyName, Pageable page) {/*from w w w . ja v a 2s. c o m*/ String auditLogs = getAuditLog(pairs, author, changedPropertyName, page); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return new ResponseEntity<>(auditLogs, headers, HttpStatus.OK); }
From source file:comsat.sample.ui.method.SampleMethodSecurityApplicationTests.java
@Test public void testLogin() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Arrays.asList(MediaType.TEXT_HTML)); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); form.set("username", "admin"); form.set("password", "admin"); getCsrf(form, headers);/* ww w. j a v a 2s . c o 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()); assertEquals("http://localhost:" + this.port + "/", entity.getHeaders().getLocation().toString()); }
From source file:svc.data.citations.datasources.tyler.TylerCitationDataSource.java
private List<Citation> performRestTemplateCall(URI uri) { HttpHeaders headers = new HttpHeaders(); headers.add("apikey", tylerConfiguration.apiKey); HttpEntity<?> query = new HttpEntity<>(headers); ResponseEntity<List<TylerCitation>> tylerCitationsResponse = null; ParameterizedTypeReference<List<TylerCitation>> type = new ParameterizedTypeReference<List<TylerCitation>>() { };/*ww w . ja v a 2 s . co m*/ List<TylerCitation> tylerCitations = null; try { tylerCitationsResponse = restTemplate.exchange(uri, HttpMethod.GET, query, type); tylerCitations = tylerCitationsResponse.getBody(); return citationFilter .RemoveCitationsWithExpiredDates(citationTransformer.fromTylerCitations(tylerCitations)); } catch (RestClientException ex) { System.out.println("Tyler datasource is down."); return Lists.newArrayList(); } }
From source file:com.bradley.musicapp.test.restapi.PersonRestControllerTest.java
private HttpHeaders getContentType() { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setContentType(new MediaType("application", "json")); return requestHeaders; }
From source file:cn.aozhi.songify.rest.TaskRestController.java
@RequestMapping(method = RequestMethod.POST, consumes = MediaTypes.JSON) public ResponseEntity<?> create(@RequestBody Task task, UriComponentsBuilder uriBuilder) { // JSR303 Bean Validator, RestExceptionHandler?. BeanValidators.validateWithException(validator, task); // ?/*from www. j a v a2s . co m*/ taskService.saveTask(task); // Restful?url, ?id. Long id = task.getId(); URI uri = uriBuilder.path("/api/v1/task/" + id).build().toUri(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uri); return new ResponseEntity(headers, HttpStatus.CREATED); }
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 2 s . 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()); }