users@glassfish.java.net

Re: OpenID for Web Apps?

From: <glassfish_at_javadesktop.org>
Date: Fri, 10 Oct 2008 13:35:26 PDT

hi,

this is the method I added to replace the XML Parser call in parseOpenIDPage
So I changed

        SAXParser parser = factory.newSAXParser();
         parser.parse(doc, new ParserHandler(queries));
with
        parseHTMLPage(doc, queries);


        /**
         * Parses manually a html page doc (i.e., the users openid page, to obtain
         * the values identified in the queries argument. returns values within the
         * queries arguument.
         *
         * The page must not be valid xml
         *
         * @param doc
         * @param queries
         * @throws javax.security.auth.message.AuthException
         */
        private void parseHTMLPage(InputStream doc, ElementQuery[] queries)
                        throws Exception {

                // first read inputstream into a String object
                String sHTML = "";
                BufferedReader in = new BufferedReader(new InputStreamReader(doc));
                StringBuffer buffer = new StringBuffer();
                String line;
                while ((line = in.readLine()) != null) {
                        buffer.append(line.toLowerCase());
                }
                sHTML = buffer.toString();

                // the following code parses the full html page for each defined
                // ElementQuery slow but secure..
                for (ElementQuery query : queries) {

                        // searching for the Tag defined by the QueryElement.eName (e.g.
                        // <link ....>)
                        String sTag = "<" + query.eName.toLowerCase();
                        int iStartPos = 0;
                        // now scan for each appearance of this tag and its
                        // attributes.....
                        while (sHTML.indexOf(sTag, iStartPos) > -1) {

                                iStartPos=sHTML.indexOf(sTag, iStartPos);
                                // a matching tag was found!
                                // so cut this into a single tag
                                int iEndPos = sHTML.indexOf(">", iStartPos + 1);
                                if (iEndPos > -1) {
                                        String sSingleTag = sHTML.substring(iStartPos, iEndPos);
                                        iStartPos = iEndPos + 1;

                                        /*
                                         * now check if the corresponding value pair/keys are
                                         *
                                         * found in this tag...
                                         *
                                         * one KeyPair will look like this:
                                         *
                                         * ElementQuery("link", new KeyValuePair[] { new
                                         * KeyValuePair("rel", "openid.server"), new
                                         * KeyValuePair("href", null)
                                         *
                                         * So the first Keypair must match and the second key pairs
                                         * value is what we search for
                                         */
                                        String sKey1 = query.keyPairs[0].key;
                                        String sValue1 = "\"" + query.keyPairs[0].value + "\"";
                                        String sKey2 = query.keyPairs[1].key;
                                        String sValue2 = "";

                                        if (sSingleTag.indexOf(sKey1) > -1
                                                        && sSingleTag.indexOf(sValue1) > -1
                                                        && sSingleTag.indexOf(sKey2) > -1

                                        ) {
                                                // YES! this tag matches the expected keypairs!
                                                // so now search the second value key and fill in
                                                // this attriubte value
                                                // e.g: ...href="www.openid.com/server"

                                                if (sSingleTag.indexOf(sKey2) > -1) {
                                                        int iHelpPosStart = sSingleTag.indexOf(sKey2)+sKey2.length()+1;
                                                        iHelpPosStart = sSingleTag.indexOf("\"",
                                                                        iHelpPosStart)+1;
                                                        int iHelpPosEnd = sSingleTag.indexOf("\"",
                                                                        iHelpPosStart + 1);
                                                        sValue2 = sSingleTag.substring(iHelpPosStart,
                                                                        iHelpPosEnd);

                                                        query.keyPairs[1].value = sValue2;
                                                        System.out.println("=== Found Attribute: "+sValue1+":" + sValue2);
                                                        // now we can break because scan of this
                                                        // queryElement is
                                                        // completed!
                                                        query.attributes = new HashMap();
                                                        query.attributes.put(sKey1, sValue1);
                                                        query.attributes.put(sKey2, sValue2);
                                                        break;
                                                }
                                        }
                                }
                        }
                }
        }
[Message sent by forum member 'rsoika' (rsoika)]

http://forums.java.net/jive/thread.jspa?messageID=304652