summaryrefslogtreecommitdiffstats
path: root/simple/simple-http/src/main/java/org/simpleframework/http/message/ConsumerFactory.java
blob: b3e5dc09e055f3b0cd91ba3e731e83c94c5dcb7a (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * ConsumerFactory.java February 2007
 *
 * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.
 */

package org.simpleframework.http.message;

import static org.simpleframework.http.Protocol.BOUNDARY;
import static org.simpleframework.http.Protocol.CHUNKED;
import static org.simpleframework.http.Protocol.MULTIPART;

import org.simpleframework.common.buffer.Allocator;
import org.simpleframework.http.ContentType;

/**
 * The <code>ConsumerFactory</code> object is used to create a factory
 * for creating consumers. This allows the request to determine the
 * type of content sent and allows consumption of the request body in
 * a the manner specified by the HTTP header. This will allow multipart
 * and chunked content to be consumed from the pipeline.
 * 
 * @author Niall Gallagher
 */
class ConsumerFactory {
   
   /**
    * This is used to allocate the memory associated with the body.
    */
   protected Allocator allocator;
   
   /**
    * This is the header associated with the request body consumed.
    */
   protected Segment segment;
   
   /**
    * Constructor for the <code>ConsumerFactory</code> object. This
    * will create a factory that makes use of the HTTP header in order
    * to determine the type of the body that is to be consumed.
    * 
    * @param allocator this is the allocator used to allocate memory
    * @param segment this is the HTTP header used to determine type
    */
   public ConsumerFactory(Allocator allocator, Segment segment) {
      this.allocator = allocator;
      this.segment = segment;
   }
   
   /**
    * This method is used to create a body consumer to read the body
    * from the pipeline. This will examine the HTTP header associated
    * with the body to determine how to consume the data. This will 
    * provide an empty consumer if no specific delimiter was provided.
    * 
    * @return this returns the consumer used to consume the body
    */
   public BodyConsumer getInstance() {
      long length = getContentLength();
      
      if(length < 0) { 
         return getInstance(8192);
      }
      return getInstance(length);
   }
   
   /**
    * This method is used to create a body consumer to read the body
    * from the pipeline. This will examine the HTTP header associated
    * with the body to determine how to consume the data. This will 
    * provide an empty consumer if no specific delimiter was provided.
    * 
    * @param length this is the length of the body to be consumed
    * 
    * @return this returns the consumer used to consume the body
    */   
   public BodyConsumer getInstance(long length) {      
      byte[] boundary = getBoundary(segment);
      
      if(isUpload(segment)) { 
         return new FileUploadConsumer(allocator, boundary, length);
      }
      if(isChunked(segment)) {
         return new ChunkedConsumer(allocator);
      }
      if(isFixed(segment)) {
         return new FixedLengthConsumer(allocator, length);
      }
      return new EmptyConsumer();
   }
   
   /**
    * This is used to extract information from the HTTP header that
    * can be used to determine the type of the body. This will look
    * at the HTTP headers provided to find a specific token which
    * enables it to determine how to consume the body. 
    * 
    * @param header this is the header associated with the body
    * 
    * @return the boundary for a multipart upload body
    */
   protected byte[] getBoundary(Segment header) {
      ContentType type = header.getContentType();
      
      if(type != null) {
         String token = type.getParameter(BOUNDARY);
         
         if(token != null) {
            return token.getBytes();
         }
      }
      return null;
   }
   
   /**
    * This is used to extract information from the HTTP header that
    * can be used to determine the type of the body. This will look
    * at the HTTP headers provided to find a specific token which
    * enables it to determine how to consume the body. 
    * 
    * @param segment this is the header associated with the body
    * 
    * @return true if the content type is that of a multipart body
    */   
   protected boolean isUpload(Segment segment) {
      ContentType type = segment.getContentType();      
      
      if(type != null) {
         String token = type.getPrimary();
      
         if(token.equals(MULTIPART)) {
            return true;
         }
      }
      return false;
   }
   
   /**
    * This is used to extract information from the HTTP header that
    * can be used to determine the type of the body. This will look
    * at the HTTP headers provided to find a specific token which
    * enables it to determine how to consume the body. 
    * 
    * @param segment this is the header associated with the body
    * 
    * @return true if the body is to be consumed as a chunked body
    */   
   protected boolean isChunked(Segment segment) {
      String encoding = segment.getTransferEncoding();
      
      if(encoding != null) {
         if(encoding.equals(CHUNKED)) {
            return true;
         }
      }
      return false;
   }

   /**
    * This is used to extract information from the HTTP header that
    * can be used to determine the type of the body. This will look
    * at the HTTP headers provided to find a specific token which
    * enables it to determine how to consume the body. 
    * 
    * @param segment this is the header associated with the body
    * 
    * @return true if there was a content length in the header
    */   
   protected boolean isFixed(Segment segment) {
      long length = segment.getContentLength();
      
      if(length > 0) {
         return true;
      }
      return false;
   }
   
   /**
    * This is a convenience method that can be used to determine
    * the length of the message body. This will determine if there
    * is a <code>Content-Length</code> header, if it does then the
    * length can be determined, if not then this returns -1.
    *
    * @return the content length, or -1 if it cannot be determined
    */
   protected long getContentLength() {
      return segment.getContentLength();
   }
}