List of usage examples for org.springframework.http HttpHeaders set
@Override public void set(String headerName, @Nullable String headerValue)
From source file:eu.freme.broker.eservices.ELink.java
@RequestMapping(value = "/e-link/templates", method = RequestMethod.GET) @Secured({ "ROLE_USER", "ROLE_ADMIN" }) public ResponseEntity<String> getAllTemplates( @RequestHeader(value = "Accept", required = false) String acceptHeader, @RequestHeader(value = "Content-Type", required = false) String contentTypeHeader, // @RequestParam(value = "outformat", required=false) String // outformat, // @RequestParam(value = "o", required=false) String o, @RequestParam Map<String, String> allParams) { try {//from w w w . j a v a2 s . c o m NIFParameterSet nifParameters = this.normalizeNif(null, acceptHeader, contentTypeHeader, allParams, true); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.set("Content-Type", nifParameters.getOutformat().contentType()); List<Template> templates = templateDAO.findAllReadAccessible(); if (nifParameters.getOutformat().equals(RDFConstants.RDFSerialization.JSON)) { ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); String serialization = ow.writeValueAsString(templates); return new ResponseEntity<>(serialization, responseHeaders, HttpStatus.OK); } else { Model mergedModel = ModelFactory.createDefaultModel(); for (Template template : templates) { mergedModel.add(template.getRDF()); } return new ResponseEntity<>( rdfConversionService.serializeRDF(mergedModel, nifParameters.getOutformat()), responseHeaders, HttpStatus.OK); } } catch (BadRequestException ex) { throw new BadRequestException(ex.getMessage()); } catch (Exception ex) { Logger.getLogger(ELink.class.getName()).log(Level.SEVERE, null, ex); } throw new InternalServerErrorException("Unknown problem. Please contact us."); }
From source file:com.jvoid.quote.controller.JVoidQuoteController.java
public ProductsMaster getJVoidProduct(int productId) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); JSONObject jsonObj = new JSONObject(); try {//w w w . java 2 s.c om jsonObj.put("id", productId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("param jsonObj=>" + jsonObj.toString()); UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT).queryParam("params", jsonObj); HttpEntity<?> entity = new HttpEntity<>(headers); HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity, String.class); System.out.println("returnString=>" + returnString); JSONObject returnJsonObj = null; try { returnJsonObj = new JSONObject(returnString.getBody()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } JSONArray productsArr = null; ProductsMaster productsMaster = null; try { productsArr = returnJsonObj.getJSONArray("products"); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } productsMaster = new ProductsMaster(); try { ObjectMapper mapper = new ObjectMapper(); try { productsMaster = mapper.readValue(productsArr.getJSONObject(0).toString(), ProductsMaster.class); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return productsMaster; }
From source file:com.emergya.spring.security.oauth.google.GoogleAuthorizationCodeAccessTokenProvider.java
private HttpHeaders getHeadersForAuthorizationRequest(final AccessTokenRequest request) { HttpHeaders headers = new HttpHeaders(); headers.putAll(request.getHeaders()); if (request.getCookie() != null) { headers.set("Cookie", request.getCookie()); }//from w w w.ja v a 2 s . c om return headers; }
From source file:org.fon.documentmanagementsystem.controllers.DocumentController.java
@RequestMapping(path = "/download/{id}", method = RequestMethod.GET) public ResponseEntity<byte[]> downloadFile(@PathVariable("id") long id) { try {//from w w w . ja v a 2s . c o m Dokument document = dokumentService.findOne(id); HttpHeaders header = new HttpHeaders(); //header.setContentType(MediaType.valueOf(document.getFajlTip())); String nazivfajla = document.getFajl(); int li = nazivfajla.lastIndexOf('\\'); String subsnaziv = nazivfajla.substring(li + 1, nazivfajla.length()); header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + subsnaziv); File file = new File(nazivfajla); Path path = file.toPath(); byte[] outputByte = Files.readAllBytes(path); String fajltype = Files.probeContentType(path); System.out.println(fajltype + " je tip"); header.setContentType(MediaType.valueOf(fajltype)); header.setContentLength(outputByte.length); return new ResponseEntity<>(outputByte, header, HttpStatus.OK); } catch (Exception e) { return null; } }
From source file:business.services.FileService.java
public HttpEntity<InputStreamResource> download(Long id) { try {/*from w w w . j a va2 s.co m*/ File attachment = fileRepository.findOne(id); if (attachment == null) { throw new FileNotFound(); } FileSystem fileSystem = FileSystems.getDefault(); Path path = fileSystem.getPath(uploadPath, attachment.getFilename()); InputStream input = new FileInputStream(path.toFile()); InputStreamResource resource = new InputStreamResource(input); HttpHeaders headers = new HttpHeaders(); if (attachment.getMimeType() != null) { headers.setContentType(MediaType.valueOf(attachment.getMimeType())); } else { headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); } headers.set("Content-Disposition", "attachment; filename=" + attachment.getName().replace(" ", "_")); HttpEntity<InputStreamResource> response = new HttpEntity<InputStreamResource>(resource, headers); return response; } catch (IOException e) { log.error(e); throw new FileDownloadError(); } }
From source file:com.oneops.opamp.service.BadStateProcessor.java
private void replace(long ciId, CmsCI env) throws OpampException { try {/* w ww . jav a 2s.c o m*/ // first mark the ci state as "replace" cmManager.updateCiState(ciId, "replace", "bom.ManagedVia", "to", false, ONEOPS_AUTOREPLACE_USER); logger.info("marked the ciId [" + ciId + "] for replace using headers using user" + ONEOPS_AUTOREPLACE_USER); // now submit the deployment Map<String, String> params = new HashMap<>(); params.put("envId", String.valueOf(env.getCiId())); Map<String, String> request = new HashMap<>(); request.put("description", "Auto-Replace by OneOps [" + env.getNsPath() + "]"); CmsCI platformOfBomCi = envProcessor.getPlatform4Bom(ciId); List<CmsCI> platformsOfEnv = envProcessor.getPlatformsForEnv(env.getCiId()); if (platformsOfEnv.size() > 1) { StringBuilder excludePlatforms = new StringBuilder(); for (CmsCI platform : platformsOfEnv) { if (platform.getCiId() != platformOfBomCi.getCiId()) { if (excludePlatforms.length() > 0) excludePlatforms.append(","); excludePlatforms.append(platform.getCiId()); } } request.put("exclude", excludePlatforms.toString()); } //TODO move it to the bean HttpHeaders headers = new HttpHeaders(); headers.set(X_CMS_USER, ONEOPS_AUTOREPLACE_USER); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Map<String, String>> requestWitHeaders = new HttpEntity<>(request, headers); @SuppressWarnings("unchecked") Map<String, Integer> response = restTemplate.postForObject( transistorUrl + "environments/" + env.getCiId() + "/deployments/deploy", requestWitHeaders, Map.class, params); Integer exitCode = response.get("deploymentId"); if (exitCode != null && exitCode == 0) { logger.info("auto-replace deployment submitted successfully by opamp. Env: " + env.getNsPath() + "/" + env.getCiName() + " Env ciId: " + env.getCiId()); } else { logger.error("Transistor returned non-zero response for auto-replace deployment. Env: " + env.getNsPath() + "/" + env.getCiName() + +env.getCiId()); throw new OpampException("Auto-Replace Could not be submitted. Transistor threw error. Env - " + env.getNsPath() + "/" + env.getCiName()); } } catch (RestClientException e) { logger.error("Error while submitting auto-replace deployment to transistor", e); throw new OpampException(e); } catch (CIValidationException cive) { logger.error("Error updating ci state to replace, ci_id = " + ciId, cive); throw new OpampException(cive); } }
From source file:access.controller.AccessController.java
/** * @param type//from www . j av a 2 s . com * MediaType to set http header content type * @param fileName * file name to set for content disposition * @param bytes * file bytes * @return ResponseEntity */ private ResponseEntity<byte[]> getResponse(MediaType type, String fileName, byte[] bytes) { HttpHeaders header = new HttpHeaders(); header.setContentType(type); header.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName); header.setContentLength(bytes.length); return new ResponseEntity<>(bytes, header, HttpStatus.OK); }
From source file:com.capitalone.dashboard.collector.DefaultNexusIQClient.java
private HttpHeaders createHeaders(String username, String password) { HttpHeaders headers = new HttpHeaders(); if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { String auth = username + ":" + password; byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII"))); String authHeader = "Basic " + new String(encodedAuth); headers.set("Authorization", authHeader); }/* w w w . j ava 2s . co m*/ return headers; }