1 package cn.home1.oss.lib.errorhandle.api;
2
3 import static com.google.common.collect.Lists.newArrayList;
4
5 import com.fasterxml.jackson.annotation.JsonProperty;
6
7 import lombok.Data;
8
9 import org.springframework.http.HttpHeaders;
10
11 import java.io.Serializable;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlElementWrapper;
19 import javax.xml.bind.annotation.XmlRootElement;
20
21
22
23
24 @XmlRootElement(name = "header")
25 @XmlAccessorType(XmlAccessType.FIELD)
26 @Data
27 public class HttpHeader implements Serializable {
28
29 @JsonProperty("name")
30 @XmlElement(name = "name")
31 private String name;
32 @JsonProperty("values")
33 @XmlElementWrapper(name = "values")
34 @XmlElement(name = "value")
35 private String[] values;
36
37 public static HttpHeaders toHttpHeaders(final HttpHeader... headers) {
38 final HttpHeaders result;
39 if (headers == null) {
40 result = null;
41 } else {
42 result = new HttpHeaders();
43 for (final HttpHeader header : headers) {
44 result.put(header.getName(), newArrayList(header.getValues()));
45 }
46 }
47 return result;
48 }
49
50 public static HttpHeader[] fromHttpHeaders(final HttpHeaders headers) {
51 final HttpHeader[] result;
52 if (headers == null) {
53 result = null;
54 } else {
55 result = headers.entrySet().stream()
56 .map(entry -> {
57
58 final List<String> value = entry.getValue();
59 final HttpHeader header = new HttpHeader();
60 header.setName(entry.getKey());
61 header.setValues(value != null ? value.toArray(new String[value.size()]) : new String[]{});
62 return header;
63 })
64 .toArray(size -> new HttpHeader[size]);
65 }
66 return result;
67 }
68
69 public String getName() {
70 return this.name;
71 }
72
73 public String[] getValues() {
74 return this.values != null ? Arrays.copyOf(this.values, this.values.length) : null;
75 }
76 }