|
| 1 | +package com.javaprogram.api; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.Map.Entry; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.Set; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.HttpStatus; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.util.MultiValueMap; |
| 14 | +import org.springframework.web.bind.annotation.GetMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestHeader; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | + |
| 19 | +@RestController |
| 20 | +@RequestMapping("request/headers") |
| 21 | +public class RequestHeadersAPI { |
| 22 | + Logger logger = LoggerFactory.getLogger(RequestHeadersAPI.class); |
| 23 | + |
| 24 | + // individual headers. |
| 25 | + @GetMapping("individual/country") |
| 26 | + public ResponseEntity<String> getHeaderFromLocale(@RequestHeader(value = "locale") String locale) { |
| 27 | + logger.info("getmapping for request hreaders : " + locale); |
| 28 | + |
| 29 | + String country = "USA is leading country in the world."; |
| 30 | + if (locale.equals("IN")) { |
| 31 | + country = "India"; |
| 32 | + } else if (locale.equals("UK")) { |
| 33 | + country = "United Kingdom"; |
| 34 | + } else if (locale.equals("AUS")) { |
| 35 | + country = "Australia"; |
| 36 | + } else if (locale.equals("IT")) { |
| 37 | + country = "Italiy"; |
| 38 | + } |
| 39 | + |
| 40 | + String response = "Country for the given locale " + locale + " is " + country; |
| 41 | + if (locale.trim().length() == 0) { |
| 42 | + response = country; |
| 43 | + } |
| 44 | + |
| 45 | + return new ResponseEntity<String>(response, HttpStatus.OK); |
| 46 | + } |
| 47 | + |
| 48 | + @GetMapping("individual/phonenumnber") |
| 49 | + public ResponseEntity<String> getHeaderNumberValue(@RequestHeader(value = "phone-number") long phoneNumber) { |
| 50 | + logger.info("getmapping for request hreaders : " + phoneNumber); |
| 51 | + boolean isValidNumber = false; |
| 52 | + if (String.valueOf(phoneNumber).length() == 10) { |
| 53 | + isValidNumber = true; |
| 54 | + } |
| 55 | + |
| 56 | + String response = "Given phone number " + phoneNumber + " is " + (isValidNumber ? "valid" : "invalid"); |
| 57 | + |
| 58 | + return new ResponseEntity<String>(response, HttpStatus.OK); |
| 59 | + } |
| 60 | + |
| 61 | + // All headers once |
| 62 | + |
| 63 | + @GetMapping("allheaders/map") |
| 64 | + public ResponseEntity<String> getAllHeadersAsMap(@RequestHeader Map<String, String> allHeaders) { |
| 65 | + |
| 66 | + logger.info("All headers are"); |
| 67 | + Set<Entry<String, String>> entrySet = allHeaders.entrySet(); |
| 68 | + |
| 69 | + StringBuffer buffer = new StringBuffer(); |
| 70 | + |
| 71 | + buffer.append("header count " + allHeaders.size() + " \n"); |
| 72 | + for (Entry<String, String> e : entrySet) { |
| 73 | + logger.info("Header " + e.getKey() + "= " + e.getValue()); |
| 74 | + buffer.append("Header " + e.getKey() + "= " + e.getValue() + " \n"); |
| 75 | + } |
| 76 | + |
| 77 | + return new ResponseEntity<String>(buffer.toString(), HttpStatus.OK); |
| 78 | + } |
| 79 | + |
| 80 | + @GetMapping("allheaders/multimap") |
| 81 | + public ResponseEntity<String> getAllHeadersAsMultiMap(@RequestHeader MultiValueMap<String, String> allHeaders) { |
| 82 | + |
| 83 | + logger.info("All headers with MultiValueMap"); |
| 84 | + |
| 85 | + // StringBuffer |
| 86 | + StringBuffer localeBuffer = new StringBuffer(); |
| 87 | + |
| 88 | + allHeaders.forEach((k, v) -> { |
| 89 | + logger.info(k + " = value = " + v.stream().collect(Collectors.toList())); |
| 90 | + |
| 91 | + if (k.equals("locale")) { |
| 92 | + localeBuffer.append(v); |
| 93 | + } |
| 94 | + |
| 95 | + }); |
| 96 | + |
| 97 | + return new ResponseEntity<String>("Repeated header : " + localeBuffer.toString(), HttpStatus.OK); |
| 98 | + } |
| 99 | + |
| 100 | + @GetMapping("allheaders/httpheaders") |
| 101 | + public ResponseEntity<String> getAllHeadersAsHttpHeaders(@RequestHeader HttpHeaders allHttpHeaders) { |
| 102 | + |
| 103 | + logger.info("All headers with MultiValueMap"); |
| 104 | + StringBuffer httpBuffer = new StringBuffer(); |
| 105 | + |
| 106 | + String firstValue = allHttpHeaders.getFirst("locale"); |
| 107 | + |
| 108 | + String hostName = allHttpHeaders.getHost().getHostName(); |
| 109 | + |
| 110 | + int port = allHttpHeaders.getHost().getPort(); |
| 111 | + |
| 112 | + httpBuffer.append(" first value of locale : " + firstValue).append("\nhost name : " + hostName) |
| 113 | + .append("\nport : " + port); |
| 114 | + |
| 115 | + return new ResponseEntity<String>("HttpHeaders response: " + httpBuffer.toString(), HttpStatus.OK); |
| 116 | + } |
| 117 | + |
| 118 | + @GetMapping("individual/requried") |
| 119 | + public ResponseEntity<String> mandateHeader(@RequestHeader(value = "locale", required = true) String locale) { |
| 120 | + |
| 121 | + return new ResponseEntity<String>(locale, HttpStatus.OK); |
| 122 | + } |
| 123 | + |
| 124 | + @GetMapping("individual/default") |
| 125 | + public ResponseEntity<String> Header(@RequestHeader(value = "locale", defaultValue = "USA") String locale) { |
| 126 | + |
| 127 | + return new ResponseEntity<String>(locale, HttpStatus.OK); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments