summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/test/java/org/simpleframework/http/message/ReplyConsumer.java
blob: a7fbfe5f6f7820ae4b1db4993c481e768986f12e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.simpleframework.http.message;

import org.simpleframework.http.Cookie;
import org.simpleframework.http.ResponseHeader;
import org.simpleframework.http.Status;
import org.simpleframework.http.message.RequestConsumer;

public class ReplyConsumer extends RequestConsumer implements ResponseHeader {

   private String text;
   private int code;

   public ReplyConsumer() {
      super();
   }

   private void status() {
      while(pos < count) {
         if(!digit(array[pos])) {
            break;
         }
         code *= 10;
         code += array[pos];
         code -= '0';
         pos++;
      }
   }

   private void text() {
      StringBuilder builder = new StringBuilder();

      while(pos < count) {
         if(terminal(array[pos])) {
            pos += 2;
            break;
         }
         builder.append((char) array[pos]);
         pos++;
      }
      text = builder.toString();
   }

   public String getDescription() {
      return text;
   }

   public void setDescription(String text) {
      this.text = text;
   }

   public int getCode() {
      return code;
   }

   public void setCode(int status) {
      this.code = status;
   }
   
   public Status getStatus() {
      return Status.getStatus(code);
   }

   public void setStatus(Status status) {
      code = status.code;
      text = status.description;
   }

   @Override
   protected void add(String name, String value) {
      if(equal("Set-Cookie", name)) { // A=b; version=1; path=/;  
         String[] list = value.split(";"); // "A=b", "version=1", "path=/" 

         if(list.length > 0) {
            String[] pair = list[0].split("=");

            if(pair.length > 1) {
               header.setCookie(pair[0], pair[1]); // "A", "b" 
            }
         }
      }
      super.add(name, value);
   }

   @Override
   protected void process() {
      version(); // HTTP/1.1 
      adjust();
      status(); // 200 
      adjust();
      text(); // OK 
      adjust();
      headers();
   }

   public void setMajor(int major) {
      this.major = major;

   }

   public void setMinor(int minor) {
      this.minor = minor;

   }

   public void addValue(String name, String value) {
      header.addValue(name, value);      
   }

   public void addInteger(String name, int value) {
      header.addInteger(name, value);
      
   }

   public void addDate(String name, long date) {
      header.addDate(name, date);
   }

   public void setValue(String name, String value) {
      header.setValue(name, value);
   }

   public void setInteger(String name, int value) {
      header.setInteger(name, value);
   }   

   public void setLong(String name, long value) {
      header.setLong(name, value);
   }
   
   public void setDate(String name, long date) {
      header.setDate(name, date);
   }
   
   public Cookie setCookie(Cookie cookie) {
      return header.setCookie(cookie);
   }

   public Cookie setCookie(String name, String value) {
      return header.setCookie(name, value);
   }
}