|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.mule.transport.http.CookieHelper
public class CookieHelper
Helper functions for parsing, formatting, storing and retrieving cookie headers.
It is important that all access to Cookie data is done using this class. This will help to prevent ClassCastExceptions and data corruption.
The reasons for such a very complex CookieHelper
class are historical and
are related to the fact that cookies are a multivalued property and we store them
as a single message property under the name
"cookies"
.
In an HTTP message going from client to server the cookies come on their own
"Cookie"
header. The HTTP message can have
several of these Cookie headers and each of them can store 1 or more cookies. One
problem with this is that in Mule we use Maps
to store the HTTP
headers and this means that we can only have one object with the key
"cookies"
(yes, we use that constant
instead of "Cookie"
when we store the cookies
inside a MuleMessage
).
In an HTTP message going from server to client the Cookies go in their own
"Set-Cookie"
header. But, again,
internally we store all the HTTP headers inside a Map
that maps each HTTP
header with a single value. For Cookies it is a special case so have to be able to
store many cookies in the value from that map.
With all these layed out one could say that we could just have a
Collection
of Cookies
. But this is not that simple. In some
parts of the code the cookies are stored as an array of Cookies (Cookie[]
) and in some others it is stored as a Map
where each entry
corresponds to a cookie's name/value pair (which is not strictly a cookie).
Specifically, when parsing cookies from the client (ie, acting as a server), the
code stores it as an array of cookies. When the cookies are specified as a
property in the endpoint (like explained in the docs), they are stored as a Map
.
This class has helper methods that helps making code that is independent of the way the cookies are stored and still keep backward compatibility. It is very hacky, but I think it is better than what we had before.
Know Limitation: because of how cookies are handled in Mule, we don't handle well the host, port and path of a Cookie. We just handle Cookies as if they were only name/value pairs. This means that, for example, if a message with cookies is received on an endpoint called http://localhost:4020/hello (1) and that message goes to http://www.mulesoft.org/jira/ (2), then service (2) will receive all the cookies that were sent to service (1) as if they were their own. Furthermore, the same thing will happend on the response: all the returned cookies from service (2) will reach service (1) and then the client will receive them as if they were from service (1).
Field Summary | |
---|---|
protected static Log |
logger
logger used by this class |
Method Summary | |
---|---|
static void |
addCookiesToClient(HttpClient client,
java.lang.Object cookiesObject,
java.lang.String policy,
MuleEvent event,
java.net.URI destinationUri)
Adds to the client all the cookies present in the cookiesObject. |
static Cookie[] |
asArrayOfCookies(java.lang.Object cookiesObject)
Returns an array view of the cookiesObject. |
static java.lang.String |
formatCookieForASetCookieHeader(Cookie cookie)
This method formats the cookie so it can be send from server to client in a "Set-Cookie" header. |
static java.lang.String |
getCookiePolicy(java.lang.String spec)
|
static CookieSpec |
getCookieSpec(java.lang.String spec)
|
static java.lang.String |
getCookieValueFromCookies(java.lang.Object cookiesObject,
java.lang.String cookieName)
Searches and return the cookie with the cookieName in the cookiesObject. |
static Cookie[] |
parseCookiesAsAClient(Header cookieHeader,
java.lang.String spec)
|
static Cookie[] |
parseCookiesAsAClient(Header cookieHeader,
java.lang.String spec,
java.net.URI uri)
|
static Cookie[] |
parseCookiesAsAClient(java.lang.String cookieHeaderValue,
java.lang.String spec)
|
static Cookie[] |
parseCookiesAsAClient(java.lang.String cookieHeaderValue,
java.lang.String spec,
java.net.URI uri)
This method parses the value of "Set-Cookie" HTTP header, returning an array with all the Cookie
found. |
static Cookie[] |
parseCookiesAsAServer(Header header,
java.net.URI uri)
This method parses the value of an HTTP "Cookie" header that comes from a client to a server. |
static Cookie[] |
parseCookiesAsAServer(java.lang.String headerValue,
java.net.URI uri)
This method parses the value of an HTTP "Cookie" header that comes from a client to a server. |
static java.lang.Object |
putAndMergeCookie(java.lang.Object preExistentCookies,
Cookie[] newCookiesArray)
Merges all the Cookies in newCookiesArray with the preExistentCookies, adding the new ones and overwriting the existing ones (existing means same cookie name). |
static java.lang.Object |
putAndMergeCookie(java.lang.Object preExistentCookies,
java.util.Map<java.lang.String,java.lang.String> newCookiesMap)
Merges all the Cookies in newCookiesMap with the preExistentCookies, adding the new ones and overwriting the existing ones (existing means same cookie name). |
static java.lang.Object |
putAndMergeCookie(java.lang.Object preExistentCookies,
java.lang.String cookieName,
java.lang.String cookieValue)
This method merges a new Cookie (or override the previous one if it exists) to the preExistentCookies. |
protected static Cookie |
transformServerCookieToClientCookie(org.apache.tomcat.util.http.ServerCookie serverCookie)
Transforms a ServerCookie (from apache tomcat) into a Cookie
(from commons httpclient). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final Log logger
Method Detail |
---|
public static CookieSpec getCookieSpec(java.lang.String spec)
spec
-
CookieSpec
(defaults to RFC2109Spec
when spec is
null)public static java.lang.String getCookiePolicy(java.lang.String spec)
spec
-
CookiePolicy.RFC_2109
when spec
is null).public static Cookie[] parseCookiesAsAClient(Header cookieHeader, java.lang.String spec) throws MalformedCookieException
MalformedCookieException
parseCookiesAsAClient(String, String, URI)
public static Cookie[] parseCookiesAsAClient(java.lang.String cookieHeaderValue, java.lang.String spec) throws MalformedCookieException
MalformedCookieException
parseCookiesAsAClient(String, String, URI)
public static Cookie[] parseCookiesAsAClient(Header cookieHeader, java.lang.String spec, java.net.URI uri) throws MalformedCookieException
MalformedCookieException
parseCookiesAsAClient(String, String, URI)
public static Cookie[] parseCookiesAsAClient(java.lang.String cookieHeaderValue, java.lang.String spec, java.net.URI uri) throws MalformedCookieException
"Set-Cookie"
HTTP header, returning an array with all the Cookie
found. This method is intended to be used from the client side of the HTTP
connection.
cookieHeaderValue
- the value with the cookie/s to parse.spec
- the spec according to getCookieSpec(String)
(can be null)uri
- the uri information that will be use to complete Cookie information
(host, port and path). If null then the default
will be used.
MalformedCookieException
public static Cookie[] parseCookiesAsAServer(Header header, java.net.URI uri)
"Cookie"
header that comes from a client to a server. It returns all the
Cookies present in the header.
header
- the header from which the cookie will be parsed. Please not that
only the value
of this header will be
used. No validation will be made to make sure that the
headerName
is actually a
HttpConstants.HEADER_COOKIE
.
public static Cookie[] parseCookiesAsAServer(java.lang.String headerValue, java.net.URI uri)
"Cookie"
header that comes from a client to a server. It returns all the
Cookies present in the header.
headerValue
- the value of the header from which the cookie will be
parsed.uri
-
protected static Cookie transformServerCookieToClientCookie(org.apache.tomcat.util.http.ServerCookie serverCookie)
ServerCookie
(from apache tomcat) into a Cookie
(from commons httpclient). Both types of Cookie hold the same data but the
ServerCookie
is the type that you get when parsing cookies as a
Server.
serverCookie
-
public static java.lang.String formatCookieForASetCookieHeader(Cookie cookie)
"Set-Cookie"
header.
cookie
-
public static void addCookiesToClient(HttpClient client, java.lang.Object cookiesObject, java.lang.String policy, MuleEvent event, java.net.URI destinationUri)
client
- cookiesObject
- this must be either a Map<String,
String>
or a Cookie[]
. It can be null.policy
- event
- this one is used only if the cookies are stored in a Map
in order to resolve expressions with the ExpressionManager
.destinationUri
- the host, port and path of this URI
will be used
as the data of the cookies that are added.public static java.lang.Object putAndMergeCookie(java.lang.Object preExistentCookies, java.lang.String cookieName, java.lang.String cookieValue)
It is important that you use the returned value of this method because
for some implementations of preExistentCookies it is not possible to add new
Cookies (for example, on Cookie[]
).
preExistentCookies
- this must be either a Map<String,
String>
or a Cookie[]
. It can be null.cookieName
- the new cookie name to be added.cookieValue
- the new cookie value to be added.
public static java.lang.Object putAndMergeCookie(java.lang.Object preExistentCookies, Cookie[] newCookiesArray)
It is important that you use the returned value of this method because
for some implementations of preExistentCookies it is not possible to add new
Cookies (for example, on Cookie[]
).
preExistentCookies
- newCookiesArray
-
public static java.lang.Object putAndMergeCookie(java.lang.Object preExistentCookies, java.util.Map<java.lang.String,java.lang.String> newCookiesMap)
It is important that you use the returned value of this method because
for some implementations of preExistentCookies it is not possible to add new
Cookies (for example, on Cookie[]
).
preExistentCookies
- newCookiesMap
-
public static java.lang.String getCookieValueFromCookies(java.lang.Object cookiesObject, java.lang.String cookieName)
cookiesObject
- cookieName
-
public static Cookie[] asArrayOfCookies(java.lang.Object cookiesObject)
cookiesObject
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |