{"id":655,"date":"2005-02-09T21:34:06","date_gmt":"2005-02-10T01:34:06","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=655"},"modified":"2005-02-09T21:34:06","modified_gmt":"2005-02-10T01:34:06","slug":"retrieving-an-rss-feed-protected-by-basic-authentication-using-rome","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2005\/02\/09\/retrieving-an-rss-feed-protected-by-basic-authentication-using-rome\/","title":{"rendered":"Retrieving an RSS feed protected by Basic Authentication using ROME"},"content":{"rendered":"<p>Today I worked on a feature in a Struts web application where I needed to retrieve an RSS feed that is protected by <a href=\"http:\/\/www.faqs.org\/rfcs\/rfc2617.html\">Basic Authentication<\/a> and then display the the results of the feed in a web page.  I&#8217;ve heard alot about <a href=\"https:\/\/rome.dev.java.net\/\">ROME<\/a> in the past couple weeks, so I decided to try it out.  It quickly passed the 10 minute test (downloaded <a href=\"http:\/\/wiki.java.net\/bin\/view\/Javawsxml\/Rome\">rome-0.5.jar<\/a>, downloaded <a href=\"http:\/\/www.jdom.org\/\">jdom.jar<\/a>, used the <a href=\"http:\/\/wiki.java.net\/bin\/view\/Javawsxml\/Rome05TutorialFeedReader\">sample code from the tutorial<\/a> on the wiki); I was able to retrieve, parse and display the results of my own feed in no time:<br \/>\n<code><br \/>\nString feed = \"http:\/\/cephas.net\/blog\/index.rdf\";<br \/>\nURL feedUrl = new URL(feed);<br \/>\nSyndFeedInput input = new SyndFeedInput();<br \/>\nSyndFeed feed = input.build(new XmlReader(feedUrl));<br \/>\nSystem.out.println(feed);<br \/>\n<\/code><br \/>\nEasy.  But that wasn&#8217;t my problem. I needed to be able to set the Basic Authentication header which is usually done like this:<br \/>\n<code><br \/>\nString feed = \"http:\/\/yoursite.com\/index.rdf\";<br \/>\nURL feedUrl = new URL(feed)<br \/>\nHttpURLConnection httpcon = (HttpURLConnection)feedUrl.openConnection();<br \/>\nString encoding = new sun.misc.BASE64Encoder().<br \/>\n&nbsp;&nbsp;encode(\"username:password\".getBytes());<br \/>\nhttpcon.setRequestProperty  (\"Authorization\", \"Basic \" + encoding);<br \/>\nhttpcon.connect();<br \/>\n.. \/\/ do stuff<br \/>\nhttpcon.disconnect();<br \/>\n<\/code><br \/>\nTurns out that the designers of the ROME library were pretty smart. In addition to including the <code><a href=\"https:\/\/rome.dev.java.net\/apidocs\/0_5\/com\/sun\/syndication\/io\/XmlReader.html\">XmlReader(URL url)<\/a><\/code> constructor, they also included a <code><a href=\"https:\/\/rome.dev.java.net\/apidocs\/0_5\/com\/sun\/syndication\/io\/XmlReader.html#XmlReader(java.net.URLConnection)\">XmlReader(URLConnection connection)<\/a><\/code> constructor, which allows you to combine the two blocks of code I wrote above to make this:<br \/>\n<code><br \/>\nString feed = \"http:\/\/yoursite.com\/index.rdf\";<br \/>\nURL feedUrl = new URL(feed)<br \/>\nHttpURLConnection httpcon = (HttpURLConnection)feedUrl.openConnection();<br \/>\nString encoding = new sun.misc.BASE64Encoder().<br \/>\n&nbsp;&nbsp;encode(\"username:password\".getBytes());<br \/>\nhttpcon.setRequestProperty  (\"Authorization\", \"Basic \" + encoding);<br \/>\nSyndFeedInput input = new SyndFeedInput();<br \/>\nSyndFeed feed = input.build(new XmlReader(httpcon));<br \/>\n<\/code><br \/>\nAdd this code to your <a href=\"http:\/\/struts.apache.org\/api\/org\/apache\/struts\/action\/Action.html\">Struts action<\/a>, put the resulting <code>SyndFeed<\/code> in the request scope (<code>request.setAttribute(\"feed\", feed);<\/code>) and then in the JSP:<br \/>\n<code><br \/>\n&lt;c:forEach var=\"entry\" items=\"${feed.entries}\"&gt;<br \/>\n&nbsp;&nbsp;&lt;strong&gt;${entry.title}&lt;\/strong&gt;&lt;br \/&gt;<br \/>\n&nbsp;&nbsp;${entry.description.value}&lt;br \/&gt;<br \/>\n&nbsp;&nbsp;&lt;fmt:formatDate value=\"${entry.publishedDate}\" type=\"both\" pattern=\"MMMM dd, yyyy\" \/&gt;<br \/>\n&nbsp;&nbsp;by ${entry.author} | &lt;a href=\"${entry.link}\"&gt;link&lt;\/a&gt;<br \/>\n&lt;\/c:forEach&gt;<br \/>\n<\/code><br \/>\nSo there you have it. I hope that makes it easier for someone else!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I worked on a feature in a Struts web application where I needed to retrieve an RSS feed that is protected by Basic Authentication and then display the the results of the feed in a web page. I&#8217;ve heard alot about ROME in the past couple weeks, so I decided to try it out. &hellip; <a href=\"https:\/\/cephas.net\/blog\/2005\/02\/09\/retrieving-an-rss-feed-protected-by-basic-authentication-using-rome\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Retrieving an RSS feed protected by Basic Authentication using ROME<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,2,22,10],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/655"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=655"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}