org.simpleframework.http.message
Class SegmentConsumer

java.lang.Object
  extended by org.simpleframework.http.message.ArrayConsumer
      extended by org.simpleframework.http.message.SegmentConsumer
All Implemented Interfaces:
ByteConsumer, Segment
Direct Known Subclasses:
HeaderConsumer

public class SegmentConsumer
extends ArrayConsumer
implements Segment

The SegmentConsumer object provides a consumer that is used to consume a HTTP header. This will read all headers within a HTTP header message until the carriage return line feed empty line is encountered. Once all headers are consumed they are available using the case insensitive header name. This will remove leading and trailing whitespace from the names and values parsed.

Author:
Niall Gallagher

Field Summary
protected  CookieParser cookies
          This is used to parse the cookie headers that are consumed.
protected  ContentDisposition disposition
          This is used to represent the content disposition header.
protected  String encoding
          This represents the transfer encoding value of the body.
protected  boolean expect
          This is used to determine if there is a continue expected.
protected  MessageHeader header
          This is used to store all consumed headers by the header name.
protected  LanguageParser language
          This is used to parse the languages accepted in the request.
protected  long length
          Represents the length of the body from the content length.
protected  long limit
          This represents the length limit of the HTTP header cosumed.
protected  String name
          During parsing this is used to store the parsed header name,
protected  int pos
          This is used to track the read offset within the header.
protected  int scan
          This is used to track how much of the terminal is read.
protected  ContentType type
          This is used to parse the content type header consumed.
protected  String value
          During parsing this is used to store the parsed header value.
 
Fields inherited from class org.simpleframework.http.message.ArrayConsumer
array, chunk, count, done
 
Constructor Summary
SegmentConsumer()
          Constructor for the SegmentConsumer object.
SegmentConsumer(int limit)
          Constructor for the SegmentConsumer object.
 
Method Summary
protected  void add(String name, String value)
          This is used to add the name and value specified as a special header within the segment.
protected  void adjust()
          This will update the offset variable so that the next read will be of a non whitespace character.
protected  void cookie(String value)
          This will accept any cookie header and parse it such that all cookies within it are converted to Cookie objects and made available as typed objects.
protected  void disposition(String value)
          This is used to parse the content disposition header header so that the MIME type is available to the segment.
protected  void encoding(String value)
          This is used to store the transfer encoding header value.
protected  void end()
          This will update the offset variable so that the next read will be a non whitespace character or terminal character.
protected  boolean equal(String name, String token)
          This is used to determine if two header names are equal, this is done to ensure that the case insensitivity of HTTP header names is observed.
protected  void expect(String value)
          This is used to determine if the expect continue header is present and thus there is a requirement to send the continue status before the client sends the request body.
 long getContentLength()
          This is a convenience method that can be used to determine the length of the message body.
 ContentType getContentType()
          This is a convenience method that can be used to determine the content type of the message body.
 ContentDisposition getDisposition()
          This is a convenience method that can be used to determine the content type of the message body.
 String getFileName()
          This method is used to acquire the file name of the part.
 List<Locale> getLocales()
          This is used to acquire the locales from the request header.
 String getName()
          This method is used to acquire the name of the part.
 String getTransferEncoding()
          This is a convenience method that can be used to determine the content type of the message body.
 String getValue(String name)
          This can be used to get the value of the first message header that has the specified name.
 String getValue(String name, int index)
          This can be used to get the value of the first message header that has the specified name.
 List<String> getValues(String name)
          This can be used to get the values of HTTP message headers that have the specified name.
protected  void headers()
          This is used to parse the headers from the consumed HTTP header and add them to the segment.
 boolean isExpectContinue()
          This is used to determine if the header represents one that requires the HTTP/1.1 continue expectation.
 boolean isFile()
          This method is used to determine the type of a part.
protected  void language(String value)
          This is used to parse the Accept-Language header value.
protected  void length(String value)
          This is used to parse a provided header value for the content length.
protected  void process()
          This is used to process the headers when the terminal token has been fully read from the consumed bytes.
protected  void resize(int size)
          This method is used to add an additional chunk size to the internal array.
protected  int scan()
          This method is used to scan for the terminal token.
protected  boolean space(byte octet)
          This identifies a given ISO-8859-1 byte as a space character.
protected  boolean terminal(byte octet)
          This determines if an ISO-8859-1 byte is a terminal character.
 String toString()
          This is used to provide a string representation of the header read.
protected  void type(String value)
          This is used to parse the content type header header so that the MIME type is available to the segment.
protected  boolean white(byte octet)
          This is used to determine if a given ISO-8859-1 byte is a white space character, such as a tab or space or a terminal character, such as a carriage return or a new line.
 
Methods inherited from class org.simpleframework.http.message.ArrayConsumer
consume, isFinished
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

disposition

protected ContentDisposition disposition
This is used to represent the content disposition header.


language

protected LanguageParser language
This is used to parse the languages accepted in the request.


cookies

protected CookieParser cookies
This is used to parse the cookie headers that are consumed.


header

protected MessageHeader header
This is used to store all consumed headers by the header name.


type

protected ContentType type
This is used to parse the content type header consumed.


encoding

protected String encoding
This represents the transfer encoding value of the body.


name

protected String name
During parsing this is used to store the parsed header name,


value

protected String value
During parsing this is used to store the parsed header value.


expect

protected boolean expect
This is used to determine if there is a continue expected.


length

protected long length
Represents the length of the body from the content length.


limit

protected long limit
This represents the length limit of the HTTP header cosumed.


pos

protected int pos
This is used to track the read offset within the header.


scan

protected int scan
This is used to track how much of the terminal is read.

Constructor Detail

SegmentConsumer

public SegmentConsumer()
Constructor for the SegmentConsumer object. This is used to create a segment consumer used to consume and parse a HTTP message header. This delegates parsing of headers if they represent special headers, like content type or cookies.


SegmentConsumer

public SegmentConsumer(int limit)
Constructor for the SegmentConsumer object. This is used to create a segment consumer used to consume and parse a HTTP message header. This delegates parsing of headers if they represent special headers, like content type or cookies.

Parameters:
limit - this is the length limit for a HTTP header
Method Detail

isFile

public boolean isFile()
This method is used to determine the type of a part. Typically a part is either a text parameter or a file. If this is true then the content represented by the associated part is a file.

Specified by:
isFile in interface Segment
Returns:
this returns true if the associated part is a file

getName

public String getName()
This method is used to acquire the name of the part. Typically this is used when the part represents a text parameter rather than a file. However, this can also be used with a file part.

Specified by:
getName in interface Segment
Returns:
this returns the name of the associated part

getFileName

public String getFileName()
This method is used to acquire the file name of the part. This is used when the part represents a text parameter rather than a file. However, this can also be used with a file part.

Specified by:
getFileName in interface Segment
Returns:
this returns the file name of the associated part

getContentType

public ContentType getContentType()
This is a convenience method that can be used to determine the content type of the message body. This will determine whether there is a Content-Type header, if there is then this will parse that header and represent it as a typed object which will expose the various parts of the HTTP header.

Specified by:
getContentType in interface Segment
Returns:
this returns the content type value if it exists

getContentLength

public long getContentLength()
This is a convenience method that can be used to determine the length of the message body. This will determine if there is a Content-Length header, if it does then the length can be determined, if not then this returns -1.

Specified by:
getContentLength in interface Segment
Returns:
the content length, or -1 if it cannot be determined

getTransferEncoding

public String getTransferEncoding()
This is a convenience method that can be used to determine the content type of the message body. This will determine whether there is a Transfer-Encoding header, if there is then this will parse that header and return the first token in the comma separated list of values, which is the primary value.

Specified by:
getTransferEncoding in interface Segment
Returns:
this returns the transfer encoding value if it exists

getDisposition

public ContentDisposition getDisposition()
This is a convenience method that can be used to determine the content type of the message body. This will determine whether there is a Content-Disposition header, if there is this will parse that header and represent it as a typed object which will expose the various parts of the HTTP header.

Specified by:
getDisposition in interface Segment
Returns:
this returns the content disposition value if it exists

getLocales

public List<Locale> getLocales()
This is used to acquire the locales from the request header. The locales are provided in the Accept-Language header. This provides an indication as to the languages that the client accepts. It provides the locales in preference order.

Returns:
this returns the locales preferred by the client

getValues

public List<String> getValues(String name)
This can be used to get the values of HTTP message headers that have the specified name. This is a convenience method that will present that values as tokens extracted from the header. This has obvious performance benefits as it avoids having to deal with substring and trim calls.

The tokens returned by this method are ordered according to there HTTP quality values, or "q" values, see RFC 2616 section 3.9. This also strips out the quality parameter from tokens returned. So "image/html; q=0.9" results in "image/html". If there are no "q" values present then order is by appearance.

The result from this is either the trimmed header value, that is, the header value with no leading or trailing whitespace or an array of trimmed tokens ordered with the most preferred in the lower indexes, so index 0 is has highest preference.

Specified by:
getValues in interface Segment
Parameters:
name - the name of the headers that are to be retrieved
Returns:
ordered array of tokens extracted from the header(s)

getValue

public String getValue(String name)
This can be used to get the value of the first message header that has the specified name. The value provided from this will be trimmed so there is no need to modify the value, also if the header name specified refers to a comma separated list of values the value returned is the first value in that list. This returns null if theres no HTTP message header.

Specified by:
getValue in interface Segment
Parameters:
name - the HTTP message header to get the value from
Returns:
this returns the value that the HTTP message header

getValue

public String getValue(String name,
                       int index)
This can be used to get the value of the first message header that has the specified name. The value provided from this will be trimmed so there is no need to modify the value, also if the header name specified refers to a comma separated list of values the value returned is the first value in that list. This returns null if there is no HTTP message header.

Specified by:
getValue in interface Segment
Parameters:
name - the HTTP message header to get the value from
index - acquires a specific header value from multiple
Returns:
this returns the value that the HTTP message header

isExpectContinue

public boolean isExpectContinue()
This is used to determine if the header represents one that requires the HTTP/1.1 continue expectation. If the request does require this expectation then it should be send the 100 status code which prompts delivery of the message body.

Returns:
this returns true if a continue expectation exists

resize

protected void resize(int size)
               throws IOException
This method is used to add an additional chunk size to the internal array. Resizing of the internal array is required as the consumed bytes may exceed the initial size of the array. In such a scenario the array is expanded the chunk size.

Overrides:
resize in class ArrayConsumer
Parameters:
size - this is the minimum size to expand the array to
Throws:
IOException

process

protected void process()
                throws IOException
This is used to process the headers when the terminal token has been fully read from the consumed bytes. Processing will extract all headers from the HTTP header message and further parse those values if required.

Specified by:
process in class ArrayConsumer
Throws:
IOException

headers

protected void headers()
This is used to parse the headers from the consumed HTTP header and add them to the segment. Once added they are available via the header name in a case insensitive manner. If the header has a special value, that is, if further information is required it will be extracted and exposed in the segment interface.


add

protected void add(String name,
                   String value)
This is used to add the name and value specified as a special header within the segment. Special headers are those where there are values of interest to the segment. For instance the Content-Length, Content-Type, and Cookie headers are parsed using an external parser to extract the values.

Parameters:
name - this is the name of the header to be added
value - this is the value of the header to be added

expect

protected void expect(String value)
This is used to determine if the expect continue header is present and thus there is a requirement to send the continue status before the client sends the request body. This will basically assume the expectation is always continue.

Parameters:
value - the value in the expect continue header

cookie

protected void cookie(String value)
This will accept any cookie header and parse it such that all cookies within it are converted to Cookie objects and made available as typed objects. If the value can not be parsed this will not add the cookie value.

Parameters:
value - this is the value of the cookie to be parsed

language

protected void language(String value)
This is used to parse the Accept-Language header value. This allows the locales the client is interested in to be provided in preference order and allows the client do alter and response based on the locale the client has provided.

Parameters:
value - this is the value that is to be parsed

type

protected void type(String value)
This is used to parse the content type header header so that the MIME type is available to the segment. This provides an instance of the ContentType object to represent the content type header, which exposes the charset value.

Parameters:
value - this is the content type value to parse

disposition

protected void disposition(String value)
This is used to parse the content disposition header header so that the MIME type is available to the segment. This provides an instance of the Disposition object to represent the content disposition, this exposes the upload type.

Parameters:
value - this is the content type value to parse

encoding

protected void encoding(String value)
This is used to store the transfer encoding header value. This is used to determine the encoding of the body this segment represents. Typically this will be the chunked encoding.

Parameters:
value - this is the value representing the encoding

length

protected void length(String value)
This is used to parse a provided header value for the content length. If the string provided is not an integer value this will throw a number format exception, by default length is -1.

Parameters:
value - this is the header value of the content length

adjust

protected void adjust()
This will update the offset variable so that the next read will be of a non whitespace character. According to RFC 2616 a white space character is a tab or a space. This will remove multiple occurrences of whitespace characters until an non-whitespace character is encountered.


end

protected void end()
This will update the offset variable so that the next read will be a non whitespace character or terminal character. According to RFC 2616 a white space character is a tab or a space. This will remove multiple occurrences of whitespace characters until an non-whitespace character or a non-terminal is encountered. This is basically used to follow through to the end of a header line.


scan

protected int scan()
This method is used to scan for the terminal token. It searches for the token and returns the number of bytes in the buffer after the terminal token. Returning the excess bytes allows the consumer to reset the bytes within the consumer object.

Specified by:
scan in class ArrayConsumer
Returns:
this returns the number of excess bytes consumed

equal

protected boolean equal(String name,
                        String token)
This is used to determine if two header names are equal, this is done to ensure that the case insensitivity of HTTP header names is observed. Special headers are processed using this consumer and this is used to ensure the correct header is always matched.

Parameters:
name - this is the name to compare the parsed token with
token - this is the header name token to examine
Returns:
true of the header name token is equal to the name

space

protected boolean space(byte octet)
This identifies a given ISO-8859-1 byte as a space character. A space is either a space or a tab character in ISO-8859-1.

Parameters:
octet - the byte to determine whether it is a space
Returns:
true if it is a space character, false otherwise

terminal

protected boolean terminal(byte octet)
This determines if an ISO-8859-1 byte is a terminal character. A terminal character is a carriage return or a line feed character.

Parameters:
octet - the byte to determine whether it is a terminal
Returns:
true if it is a terminal character, false otherwise

white

protected boolean white(byte octet)
This is used to determine if a given ISO-8859-1 byte is a white space character, such as a tab or space or a terminal character, such as a carriage return or a new line. If it is, this will return true otherwise it returns false.

Parameters:
octet - this is to be checked to see if it is a space
Returns:
true if the byte is a space character, false otherwise

toString

public String toString()
This is used to provide a string representation of the header read. Providing a string representation of the header is used so that on debugging the contents of the delivered header can be inspected in order to determine a cause of error.

Overrides:
toString in class Object
Returns:
this returns a string representation of the header


Copyright © 2014. All Rights Reserved.