View Javadoc
1   package org.kuali.common.httplib.api.model;
2   
3   import static java.lang.String.format;
4   import static org.kuali.common.jute.base.Precondition.checkMin;
5   import static org.kuali.common.jute.base.Precondition.checkNotBlank;
6   
7   import org.apache.http.HttpStatus;
8   import org.apache.http.StatusLine;
9   
10  import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
11  
12  @JsonDeserialize(builder = ResponseStatus.Builder.class)
13  public final class ResponseStatus {
14  
15      public static final int HTTP_STATUS_CODE_MIN_VALUE = HttpStatus.SC_CONTINUE;
16  
17      private final int code;
18      private final String text;
19  
20      private ResponseStatus(Builder builder) {
21          this.code = builder.code;
22          this.text = builder.text;
23      }
24  
25      public static ResponseStatus build(org.apache.http.HttpResponse mutable) {
26          StatusLine status = mutable.getStatusLine();
27          return build(status.getStatusCode(), status.getReasonPhrase());
28      }
29  
30      public static ResponseStatus build(int code, String text) {
31          return builder().withCode(code).withText(text).build();
32      }
33  
34      public static Builder builder() {
35          return new Builder();
36      }
37  
38      public static class Builder implements org.apache.commons.lang3.builder.Builder<ResponseStatus> {
39  
40          private int code;
41          private String text;
42  
43          public Builder withCode(int code) {
44              this.code = code;
45              return this;
46          }
47  
48          public Builder withText(String text) {
49              this.text = text;
50              return this;
51          }
52  
53          @Override
54          public ResponseStatus build() {
55              return validate(new ResponseStatus(this));
56          }
57  
58          private static ResponseStatus validate(ResponseStatus instance) {
59              checkMin(instance.code, HTTP_STATUS_CODE_MIN_VALUE, "code");
60              checkNotBlank(instance.text, "text");
61              return instance;
62          }
63  
64      }
65  
66      public int getCode() {
67          return code;
68      }
69  
70      public String getText() {
71          return text;
72      }
73  
74      @Override
75      public String toString() {
76          return format("%s %s", code, text);
77      }
78  
79  }