You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Chris Newland <ch...@emorphia.com> on 2001/08/16 13:57:59 UTC

HttpRequest.getCookies() causes NPE in XSPCookieHelper.getCookies()

Hi All,

I think there is an unhandled case in
org.apache.cocoon.environment.http.HttpRequest (CVS 1.1.1.1.2.4) that causes
a null pointer exception in
org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper (CVS
1.2.2.1).

XSPCookieHelper.getCookie(Map objectModel ,ContentHandler contentHandler):

        Request request =
(Request)objectModel.get(Constants.REQUEST_OBJECT);

        Cookie[] cookies = request.getCookies(); <-- can be null

        int count  = 0;

        String tempStr = null;

        Hashtable nodeTable = new Hashtable();

        if(cookies.length > 0) <-- causes NPE (XSPCookieHelper line 171)

This seems to be caused by (in HttpRequest ):

    public Cookie[] getCookies() {
        if (this.wrappedCookies == null) {
            this.wrappedCookieMap = new HashMap();
            javax.servlet.http.Cookie[] cookies = this.req.getCookies();
            if (cookies != null) {
                this.wrappedCookies = new Cookie[cookies.length];
                for(int i=0; i<cookies.length;i++) {
                    HttpCookie cookie = new HttpCookie(cookies[i]);
                    this.wrappedCookies[i] = cookie;
                    this.wrappedCookieMap.put(cookie.getName(),cookie);
                }
            }
        }
        return this.wrappedCookies;
    }

If

this.wrappedCookies == null
AND
cookies == null

then the array wrappedCookies will be return null.

Is this the desired behaviour when no cookies exist?

One solution is to test for null in XSPCookieHelper, another is to change
HttpRequest.getCookies() so something like:

    public Cookie[] getCookies() {
        if (this.wrappedCookies == null) {
            this.wrappedCookieMap = new HashMap();
            javax.servlet.http.Cookie[] cookies = this.req.getCookies();
            if (cookies != null) {
                this.wrappedCookies = new Cookie[cookies.length];
                for(int i=0; i<cookies.length;i++) {
                    HttpCookie cookie = new HttpCookie(cookies[i]);
                    this.wrappedCookies[i] = cookie;
                    this.wrappedCookieMap.put(cookie.getName(),cookie);
                }
            }
		else {
			this.wrappedCookies = new Cookie[0];
		}

        }
        return this.wrappedCookies;
    }


This is my first post to Cocoon-dev so I'm unsure of what to do about this.
Should I submit it as a bug to BugZilla or would you like me to make the
change to HttpRequest and submit a patch?

Thanks for your time (and patience :),

Best Regards,

Chris

--
Chris Newland
Software Research Engineer

Emorphia Ltd
Registered in England.  4133002
Mill House, Station Approach, Harlow Mill, Harlow, Essex, CM20 2EL, UK

Email: chris.newland@emorphia.com
Tel: +44 (0)1279 450100
Fax: +44 (0)1279 450102

Check out FIPA-OS at http://fipa-os.sourceforge.net/

This message may contain information proprietary to Emorphia so any
unauthorised disclosure, copying or distribution of its contents is strictly
prohibited.



---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


RE: HttpRequest.getCookies() causes NPE in XSPCookieHelper.getCookies()

Posted by Chris Newland <ch...@emorphia.com>.
Hi Carsten,

Thanks for fixing this, there are no more exceptions now but I'm still
getting strange results when running the xsp/cookie example. I'll look into
this further.

Best Regards,

Chris

> -----Original Message-----
> From: Carsten Ziegeler [mailto:cziegeler@sundn.de]
> Sent: 16 August 2001 14:45
> To: cocoon-dev@xml.apache.org
> Subject: AW: HttpRequest.getCookies() causes NPE in
> XSPCookieHelper.getCookies()
>
>
> Thanks again Chris, I just fixed these places (I hope at least so).
>
> So you don't need to post a patch for this.
>
> The usual patch format is making a diff with "cvs diff -u".
>
> Carsten
>
> > -----Ursprüngliche Nachricht-----
> > Von: Chris Newland [mailto:chris.newland@emorphia.com]
> > Gesendet: Donnerstag, 16. August 2001 15:28
> > An: cocoon-dev@xml.apache.org
> > Betreff: RE: HttpRequest.getCookies() causes NPE in
> > XSPCookieHelper.getCookies()
> >
> >
> > Hi Carsten,
> >
> > That fixed the first problem but I'm afraid that cookies.length
> is called
> > without testing for null in 5 more places (sorry for not spotting this
> > earlier):
> >
> > XSPCookieHelper.getCookie(Map objectModel , String cookieName ,int
> > cookieIndex):
> >
> >
> >         if (retrieveByName)
> >         {
> >             for(count=0; count<cookies.length; count++)
> <---- *here*
> >             {
> >                 currentCookie = cookies[count];
> >
> >                 if (currentCookie.getName().equals(cookieName))
> >                     matchFound = true;
> >             }
> >         }
> >         else if(retrieveByIndex)
> >         {
> >             if(cookies.length > cookieIndex)   <---- *here*
> >             {
> >                 currentCookie = cookies[cookieIndex];
> >                 matchFound = true;
> >             }
> >         }
> >
> > XSPCookieHelper.returnCookieProperty(Map objectModel , String
> cookieName ,
> > int cookieIndex , String propertyPrefix):
> >
> >         Cookie[] cookies = request.getCookies();
> >
> >         if (cookies.length > 0)  <---- *here*
> >         {
> >             if (retrieveByName)
> >             {
> >                 for(count=0; count<cookies.length; count++)
> <---- *here*
> >                 {
> >                     currentCookie = cookies[count];
> >
> >                     if (currentCookie.getName().equals(cookieName))
> >                         matchFound = true;
> >                 }
> >             }
> >             else if (retrieveByIndex)
> >             {
> >                 if(cookies.length > cookieIndex) <---- *here*
> >                 {
> >                     currentCookie = cookies[cookieIndex];
> >                     matchFound = true;
> >                 }
> >             }
> >
> > Do you want me to fix these? If so, what format do you want the
> > patches in?
> >
> > Best Regards,
> >
> > Chris
> >
> >
> > > -----Original Message-----
> > > From: Carsten Ziegeler [mailto:cziegeler@sundn.de]
> > > Sent: 16 August 2001 13:23
> > > To: cocoon-dev@xml.apache.org
> > > Subject: AW: HttpRequest.getCookies() causes NPE in
> > > XSPCookieHelper.getCookies()
> > >
> > >
> > > Thanks for reporting this Chris.
> > >
> > > I fixed it, please check if this is working now.
> > >
> > >
> > > Carsten
> > >
> > > Open Source Group                        sunShine - b:Integrated
> > > ================================================================
> > > Carsten Ziegeler, S&N AG, Klingenderstrasse 5, D-33100 Paderborn
> > > www.sundn.de                          mailto: cziegeler@sundn.de
> > > ================================================================
> > >
> > >
> > > > -----Ursprüngliche Nachricht-----
> > > > Von: Chris Newland [mailto:chris.newland@emorphia.com]
> > > > Gesendet: Donnerstag, 16. August 2001 13:58
> > > > An: Cocoon Dev
> > > > Betreff: HttpRequest.getCookies() causes NPE in
> > > > XSPCookieHelper.getCookies()
> > > >
> > > >
> > > > Hi All,
> > > >
> > > > I think there is an unhandled case in
> > > > org.apache.cocoon.environment.http.HttpRequest (CVS 1.1.1.1.2.4)
> > > > that causes
> > > > a null pointer exception in
> > > >
> org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper (CVS
> > > > 1.2.2.1).
> >
> >
> > <snip>
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> > For additional commands, email: cocoon-dev-help@xml.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


AW: HttpRequest.getCookies() causes NPE in XSPCookieHelper.getCookies()

Posted by Carsten Ziegeler <cz...@sundn.de>.
Thanks again Chris, I just fixed these places (I hope at least so).

So you don't need to post a patch for this.

The usual patch format is making a diff with "cvs diff -u".

Carsten

> -----Ursprüngliche Nachricht-----
> Von: Chris Newland [mailto:chris.newland@emorphia.com]
> Gesendet: Donnerstag, 16. August 2001 15:28
> An: cocoon-dev@xml.apache.org
> Betreff: RE: HttpRequest.getCookies() causes NPE in
> XSPCookieHelper.getCookies()
>
>
> Hi Carsten,
>
> That fixed the first problem but I'm afraid that cookies.length is called
> without testing for null in 5 more places (sorry for not spotting this
> earlier):
>
> XSPCookieHelper.getCookie(Map objectModel , String cookieName ,int
> cookieIndex):
>
>
>         if (retrieveByName)
>         {
>             for(count=0; count<cookies.length; count++)      <---- *here*
>             {
>                 currentCookie = cookies[count];
>
>                 if (currentCookie.getName().equals(cookieName))
>                     matchFound = true;
>             }
>         }
>         else if(retrieveByIndex)
>         {
>             if(cookies.length > cookieIndex)   <---- *here*
>             {
>                 currentCookie = cookies[cookieIndex];
>                 matchFound = true;
>             }
>         }
>
> XSPCookieHelper.returnCookieProperty(Map objectModel , String cookieName ,
> int cookieIndex , String propertyPrefix):
>
>         Cookie[] cookies = request.getCookies();
>
>         if (cookies.length > 0)  <---- *here*
>         {
>             if (retrieveByName)
>             {
>                 for(count=0; count<cookies.length; count++)   <---- *here*
>                 {
>                     currentCookie = cookies[count];
>
>                     if (currentCookie.getName().equals(cookieName))
>                         matchFound = true;
>                 }
>             }
>             else if (retrieveByIndex)
>             {
>                 if(cookies.length > cookieIndex) <---- *here*
>                 {
>                     currentCookie = cookies[cookieIndex];
>                     matchFound = true;
>                 }
>             }
>
> Do you want me to fix these? If so, what format do you want the
> patches in?
>
> Best Regards,
>
> Chris
>
>
> > -----Original Message-----
> > From: Carsten Ziegeler [mailto:cziegeler@sundn.de]
> > Sent: 16 August 2001 13:23
> > To: cocoon-dev@xml.apache.org
> > Subject: AW: HttpRequest.getCookies() causes NPE in
> > XSPCookieHelper.getCookies()
> >
> >
> > Thanks for reporting this Chris.
> >
> > I fixed it, please check if this is working now.
> >
> >
> > Carsten
> >
> > Open Source Group                        sunShine - b:Integrated
> > ================================================================
> > Carsten Ziegeler, S&N AG, Klingenderstrasse 5, D-33100 Paderborn
> > www.sundn.de                          mailto: cziegeler@sundn.de
> > ================================================================
> >
> >
> > > -----Ursprüngliche Nachricht-----
> > > Von: Chris Newland [mailto:chris.newland@emorphia.com]
> > > Gesendet: Donnerstag, 16. August 2001 13:58
> > > An: Cocoon Dev
> > > Betreff: HttpRequest.getCookies() causes NPE in
> > > XSPCookieHelper.getCookies()
> > >
> > >
> > > Hi All,
> > >
> > > I think there is an unhandled case in
> > > org.apache.cocoon.environment.http.HttpRequest (CVS 1.1.1.1.2.4)
> > > that causes
> > > a null pointer exception in
> > > org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper (CVS
> > > 1.2.2.1).
>
>
> <snip>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


RE: HttpRequest.getCookies() causes NPE in XSPCookieHelper.getCookies()

Posted by Chris Newland <ch...@emorphia.com>.
Hi Carsten,

That fixed the first problem but I'm afraid that cookies.length is called
without testing for null in 5 more places (sorry for not spotting this
earlier):

XSPCookieHelper.getCookie(Map objectModel , String cookieName ,int
cookieIndex):


        if (retrieveByName)
        {
            for(count=0; count<cookies.length; count++)      <---- *here*
            {
                currentCookie = cookies[count];

                if (currentCookie.getName().equals(cookieName))
                    matchFound = true;
            }
        }
        else if(retrieveByIndex)
        {
            if(cookies.length > cookieIndex)   <---- *here*
            {
                currentCookie = cookies[cookieIndex];
                matchFound = true;
            }
        }

XSPCookieHelper.returnCookieProperty(Map objectModel , String cookieName ,
int cookieIndex , String propertyPrefix):

        Cookie[] cookies = request.getCookies();

        if (cookies.length > 0)  <---- *here*
        {
            if (retrieveByName)
            {
                for(count=0; count<cookies.length; count++)   <---- *here*
                {
                    currentCookie = cookies[count];

                    if (currentCookie.getName().equals(cookieName))
                        matchFound = true;
                }
            }
            else if (retrieveByIndex)
            {
                if(cookies.length > cookieIndex) <---- *here*
                {
                    currentCookie = cookies[cookieIndex];
                    matchFound = true;
                }
            }

Do you want me to fix these? If so, what format do you want the patches in?

Best Regards,

Chris


> -----Original Message-----
> From: Carsten Ziegeler [mailto:cziegeler@sundn.de]
> Sent: 16 August 2001 13:23
> To: cocoon-dev@xml.apache.org
> Subject: AW: HttpRequest.getCookies() causes NPE in
> XSPCookieHelper.getCookies()
>
>
> Thanks for reporting this Chris.
>
> I fixed it, please check if this is working now.
>
>
> Carsten
>
> Open Source Group                        sunShine - b:Integrated
> ================================================================
> Carsten Ziegeler, S&N AG, Klingenderstrasse 5, D-33100 Paderborn
> www.sundn.de                          mailto: cziegeler@sundn.de
> ================================================================
>
>
> > -----Ursprüngliche Nachricht-----
> > Von: Chris Newland [mailto:chris.newland@emorphia.com]
> > Gesendet: Donnerstag, 16. August 2001 13:58
> > An: Cocoon Dev
> > Betreff: HttpRequest.getCookies() causes NPE in
> > XSPCookieHelper.getCookies()
> >
> >
> > Hi All,
> >
> > I think there is an unhandled case in
> > org.apache.cocoon.environment.http.HttpRequest (CVS 1.1.1.1.2.4)
> > that causes
> > a null pointer exception in
> > org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper (CVS
> > 1.2.2.1).


<snip>



---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


AW: HttpRequest.getCookies() causes NPE in XSPCookieHelper.getCookies()

Posted by Carsten Ziegeler <cz...@sundn.de>.
Thanks for reporting this Chris.

I fixed it, please check if this is working now.


Carsten

Open Source Group                        sunShine - b:Integrated
================================================================
Carsten Ziegeler, S&N AG, Klingenderstrasse 5, D-33100 Paderborn
www.sundn.de                          mailto: cziegeler@sundn.de
================================================================


> -----Ursprüngliche Nachricht-----
> Von: Chris Newland [mailto:chris.newland@emorphia.com]
> Gesendet: Donnerstag, 16. August 2001 13:58
> An: Cocoon Dev
> Betreff: HttpRequest.getCookies() causes NPE in
> XSPCookieHelper.getCookies()
>
>
> Hi All,
>
> I think there is an unhandled case in
> org.apache.cocoon.environment.http.HttpRequest (CVS 1.1.1.1.2.4)
> that causes
> a null pointer exception in
> org.apache.cocoon.components.language.markup.xsp.XSPCookieHelper (CVS
> 1.2.2.1).
>
> XSPCookieHelper.getCookie(Map objectModel ,ContentHandler contentHandler):
>
>         Request request =
> (Request)objectModel.get(Constants.REQUEST_OBJECT);
>
>         Cookie[] cookies = request.getCookies(); <-- can be null
>
>         int count  = 0;
>
>         String tempStr = null;
>
>         Hashtable nodeTable = new Hashtable();
>
>         if(cookies.length > 0) <-- causes NPE (XSPCookieHelper line 171)
>
> This seems to be caused by (in HttpRequest ):
>
>     public Cookie[] getCookies() {
>         if (this.wrappedCookies == null) {
>             this.wrappedCookieMap = new HashMap();
>             javax.servlet.http.Cookie[] cookies = this.req.getCookies();
>             if (cookies != null) {
>                 this.wrappedCookies = new Cookie[cookies.length];
>                 for(int i=0; i<cookies.length;i++) {
>                     HttpCookie cookie = new HttpCookie(cookies[i]);
>                     this.wrappedCookies[i] = cookie;
>                     this.wrappedCookieMap.put(cookie.getName(),cookie);
>                 }
>             }
>         }
>         return this.wrappedCookies;
>     }
>
> If
>
> this.wrappedCookies == null
> AND
> cookies == null
>
> then the array wrappedCookies will be return null.
>
> Is this the desired behaviour when no cookies exist?
>
> One solution is to test for null in XSPCookieHelper, another is to change
> HttpRequest.getCookies() so something like:
>
>     public Cookie[] getCookies() {
>         if (this.wrappedCookies == null) {
>             this.wrappedCookieMap = new HashMap();
>             javax.servlet.http.Cookie[] cookies = this.req.getCookies();
>             if (cookies != null) {
>                 this.wrappedCookies = new Cookie[cookies.length];
>                 for(int i=0; i<cookies.length;i++) {
>                     HttpCookie cookie = new HttpCookie(cookies[i]);
>                     this.wrappedCookies[i] = cookie;
>                     this.wrappedCookieMap.put(cookie.getName(),cookie);
>                 }
>             }
> 		else {
> 			this.wrappedCookies = new Cookie[0];
> 		}
>
>         }
>         return this.wrappedCookies;
>     }
>
>
> This is my first post to Cocoon-dev so I'm unsure of what to do
> about this.
> Should I submit it as a bug to BugZilla or would you like me to make the
> change to HttpRequest and submit a patch?
>
> Thanks for your time (and patience :),
>
> Best Regards,
>
> Chris
>
> --
> Chris Newland
> Software Research Engineer
>
> Emorphia Ltd
> Registered in England.  4133002
> Mill House, Station Approach, Harlow Mill, Harlow, Essex, CM20 2EL, UK
>
> Email: chris.newland@emorphia.com
> Tel: +44 (0)1279 450100
> Fax: +44 (0)1279 450102
>
> Check out FIPA-OS at http://fipa-os.sourceforge.net/
>
> This message may contain information proprietary to Emorphia so any
> unauthorised disclosure, copying or distribution of its contents
> is strictly
> prohibited.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org