Class PrincipalParser
- java.lang.Object
-
- org.simpleframework.common.parse.Parser
-
- org.simpleframework.http.parse.PrincipalParser
-
- All Implemented Interfaces:
Principal
public class PrincipalParser extends Parser implements Principal
PrincipalParser is a parser class for the HTTP basic authorization header. It decodes thebase64encoding of the user and password pair.This follows the parsing tree of RFC 2617. The goal of this parser is to decode the
base64encoding of the user name and password. After the string has been decoded then the user name and password are extracted. This will only parse headers that are from theBasicauthorization scheme. The format of the basic scheme can be found in RFC 2617 and is of the formBasic SP base64-encoding.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]fourKeeps thebytesused for decoding base64.private ParseBufferpasswordKeeps the characters consumed for the password token.private intreadTracks the read offset for the buffer.private intreadyTracks the ready offset for the four buffer.private ParseBufferuserKeeps the characters consumed for the user name token.private intwriteTracks the write offset for the buffer.
-
Constructor Summary
Constructors Constructor Description PrincipalParser()Creates aParserfor the basic authorization scheme.PrincipalParser(java.lang.String header)Creates aParserfor the basic authorization scheme.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voiddecode()This is used to remove decode thebase64encoding of the user name and password.private charfirst(byte[] four)This is used to extract thebytefrom the set of fourbytesgiven.java.lang.StringgetName()Gets the users name from the Authorization header value.java.lang.StringgetPassword()Gets the users password parsed from the Authorization header value.protected voidinit()This will initialize theParserwhen it is ready to parse a newString.private voidpack()This is used to remove all whitespace characters from theStringexcluding the whitespace within literals.protected voidparse()Used to parse the actual header data.private voidpassword()Extracts the password from the buffer.private charsecond(byte[] four)This is used to extract thebytefrom the set of fourbytesgiven.private booleantext(char c)This is used to determine wheather or not a character is aTEXTcharacter according to the HTTP specification, that is RFC 2616 specifies aTEXTcharacter as one that is any octet except those less than 32 and not 127.private charthird(byte[] four)This is used to extract thebytefrom the set of fourbytesgiven.private inttranslate(int octet)This uses a basic translation from thebytecharacter to thebytenumber.private voiduserid()Extracts the user name from the buffer.private voiduserpass()Extracts the name and password of the user from thename : passwordpair that was given.
-
-
-
Field Detail
-
password
private ParseBuffer password
Keeps the characters consumed for the password token.
-
user
private ParseBuffer user
Keeps the characters consumed for the user name token.
-
four
private byte[] four
Keeps thebytesused for decoding base64.
-
write
private int write
Tracks the write offset for the buffer.
-
ready
private int ready
Tracks the ready offset for the four buffer.
-
read
private int read
Tracks the read offset for the buffer.
-
-
Constructor Detail
-
PrincipalParser
public PrincipalParser()
Creates aParserfor the basic authorization scheme. This allows headers that are of this scheme to be broken into its component parts i.e. user name and password.
-
PrincipalParser
public PrincipalParser(java.lang.String header)
Creates aParserfor the basic authorization scheme. This allows headers that are of this scheme to be broken into its component parts i.e. user name and password. This constructor will parse theStringgiven as the header.- Parameters:
header- this is a header value from the basic scheme
-
-
Method Detail
-
getPassword
public java.lang.String getPassword()
Gets the users password parsed from the Authorization header value. If there was not password parsed from the base64 value of the header this returnsnull- Specified by:
getPasswordin interfacePrincipal- Returns:
- the password for the user or
null
-
getName
public java.lang.String getName()
Gets the users name from the Authorization header value. This will returnnullif there is no user name extracted from the base64 header value.
-
parse
protected void parse()
Used to parse the actual header data. This will attempt to read the "Basic" token from the set of characters given, if this is successful then the username and password is extracted.
-
init
protected void init()
This will initialize theParserwhen it is ready to parse a newString. This will reset theParserto a ready state. Theinitmethod is invoked by theParserwhen theparsemethod is invoked.
-
pack
private void pack()
This is used to remove all whitespace characters from theStringexcluding the whitespace within literals. The definition of a literal can be found in RFC 2616.The definition of a literal for RFC 2616 is anything between 2 quotes but excuding quotes that are prefixed with the backward slash character.
-
userpass
private void userpass()
Extracts the name and password of the user from thename : passwordpair that was given. This will take all data up to the first occurence of a ':' character as the users name and all data after the colon as the users password.
-
userid
private void userid()
Extracts the user name from the buffer. This will read up to the first occurence of a colon, ':', character as the user name. For the BNF syntax of this see RFC 2617.
-
password
private void password()
Extracts the password from the buffer. This will all characters from the current offset to the first non text character as the password. For the BNF syntax of this see RFC 2617.
-
decode
private void decode()
This is used to remove decode thebase64encoding of the user name and password. This uses a standartbase64decoding scheme.For information on the decoding scheme used for
base64see the RFC 2045 on MIME, Multipurpose Internet Mail Extensions.
-
translate
private int translate(int octet)
This uses a basic translation from thebytecharacter to thebytenumber.The table for translation the data can be found in RFC 2045 on MIME, Multipurpose Internet Mail Extensions.
- Parameters:
octet- this is the octet ttat is to be translated- Returns:
- this returns the translated octet
-
first
private char first(byte[] four)
This is used to extract thebytefrom the set of fourbytesgiven. This method is used to isolate the correct bits that corrospond to an actual character withing thebase64data.- Parameters:
four- this is the fourbytesthat the character is to be extracted from- Returns:
- this returns the character extracted
-
second
private char second(byte[] four)
This is used to extract thebytefrom the set of fourbytesgiven. This method is used to isolate the correct bits that corrospond to an actual character withing thebase64data.- Parameters:
four- this is the fourbytesthat the character is to be extracted from- Returns:
- this returns the character extracted
-
third
private char third(byte[] four)
This is used to extract thebytefrom the set of fourbytesgiven. This method is used to isolate the correct bits that corrospond to an actual character withing thebase64data.- Parameters:
four- this is the fourbytesthat the character is to be extracted from- Returns:
- this returns the character extracted
-
text
private boolean text(char c)
This is used to determine wheather or not a character is aTEXTcharacter according to the HTTP specification, that is RFC 2616 specifies aTEXTcharacter as one that is any octet except those less than 32 and not 127.- Parameters:
c- this is the character that is to be determined- Returns:
- this returns true if the character is a
TEXT
-
-