1 package org.kuali.common.httplib.api.model;
2
3 import static com.google.common.collect.Lists.newArrayList;
4 import static java.util.Arrays.asList;
5 import static org.kuali.common.jute.base.Precondition.checkNotNull;
6 import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
7
8 import java.util.List;
9 import java.util.Locale;
10
11 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
12 import com.google.common.collect.ImmutableList;
13
14 @JsonDeserialize(builder = ResponseMetadata.Builder.class)
15 public final class ResponseMetadata {
16
17 private final ResponseStatus status;
18 private final Protocol protocol;
19 private final Locale locale;
20 private final ImmutableList<Header> headers;
21
22 private ResponseMetadata(Builder builder) {
23 this.status = builder.status;
24 this.locale = builder.locale;
25 this.protocol = builder.protocol;
26 this.headers = ImmutableList.copyOf(builder.headers);
27 }
28
29 public static ResponseMetadata copyOf(org.apache.http.HttpResponse mutable) {
30 checkNotNull(mutable, "mutable");
31 List<Header> headers = Header.copyOf(asList(mutable.getAllHeaders()));
32 ResponseStatus status = ResponseStatus.build(mutable);
33 Protocol protocol = Protocol.build(mutable.getProtocolVersion());
34 Locale locale = mutable.getLocale();
35 ResponseMetadata.Builder builder = ResponseMetadata.builder();
36 builder.withStatus(status);
37 builder.withProtocol(protocol);
38 builder.withLocale(locale);
39 builder.withHeaders(headers);
40 return builder.build();
41 }
42
43 public static Builder builder() {
44 return new Builder();
45 }
46
47 public static class Builder implements org.apache.commons.lang3.builder.Builder<ResponseMetadata> {
48
49 private Protocol protocol;
50 private ResponseStatus status;
51 private Locale locale;
52 private List<Header> headers = newArrayList();
53
54 public Builder withHeaders(List<Header> headers) {
55 this.headers = headers;
56 return this;
57 }
58
59 public Builder withProtocol(Protocol protocol) {
60 this.protocol = protocol;
61 return this;
62 }
63
64 public Builder withStatus(ResponseStatus status) {
65 this.status = status;
66 return this;
67 }
68
69 public Builder withLocale(Locale locale) {
70 this.locale = locale;
71 return this;
72 }
73
74 @Override
75 public ResponseMetadata build() {
76 return checkNoNulls(new ResponseMetadata(this));
77 }
78
79 }
80
81 public ResponseStatus getStatus() {
82 return status;
83 }
84
85 public Locale getLocale() {
86 return locale;
87 }
88
89 public Protocol getProtocol() {
90 return protocol;
91 }
92
93 public List<Header> getHeaders() {
94 return headers;
95 }
96
97 }