Skip to Content
Author's profile photo Former Member

Create a weather magnet using xml feed from weather.com

Introduction

In this weblog i am going to present code to build a nice weather magnet for you portal. The code uses xml feed from weather.com and transforms the feed into a nice display using XSLT.

image

Required Jars

contentconverterapi.jar,contentconversionapi.jar

Step1:

Get an account from weather.com for your xml feed. Weather.com will send you a partner id and account key. You will see this info to retrieve the xml feed.

[http://www.weather.com/services/xmloap.html | http://www.weather.com/services/xmloap.html]

Step2:

Create a portal project(com.ust.weather) in NWDS or eclipse. Add a Dynpage class called

“Display”

image

Step3:

Save the following image to your dist->images folder by right clicking on it. Make sure you don’t rename the image.

!http://image.weather.com/web/common/wxicons/31/0.gif!

!http://image.weather.com/web/common/wxicons/31/1.gif!

!http://image.weather.com/web/common/wxicons/31/2.gif!

!http://image.weather.com/web/common/wxicons/31/3.gif!

!http://image.weather.com/web/common/wxicons/31/5.gif!

!http://image.weather.com/web/common/wxicons/31/5.gif!

!http://image.weather.com/web/common/wxicons/31/6.gif!

!http://image.weather.com/web/common/wxicons/31/7.gif!

!http://image.weather.com/web/common/wxicons/31/8.gif!

!http://image.weather.com/web/common/wxicons/31/9.gif!

!http://image.weather.com/web/common/wxicons/31/10.gif!

!http://image.weather.com/web/common/wxicons/31/11.gif!

!http://image.weather.com/web/common/wxicons/31/12.gif!

!http://image.weather.com/web/common/wxicons/31/13.gif!

!http://image.weather.com/web/common/wxicons/31/14.gif!

!http://image.weather.com/web/common/wxicons/31/15.gif!

!http://image.weather.com/web/common/wxicons/31/16.gif!

!http://image.weather.com/web/common/wxicons/31/17.gif!

!http://image.weather.com/web/common/wxicons/31/18.gif!

!http://image.weather.com/web/common/wxicons/31/19.gif!

!http://image.weather.com/web/common/wxicons/31/20.gif!

!http://image.weather.com/web/common/wxicons/31/21.gif!

!http://image.weather.com/web/common/wxicons/31/22.gif!

!http://image.weather.com/web/common/wxicons/31/23.gif!

!http://image.weather.com/web/common/wxicons/31/24.gif!

!http://image.weather.com/web/common/wxicons/31/25.gif!

!http://image.weather.com/web/common/wxicons/31/26.gif!

!http://image.weather.com/web/common/wxicons/31/27.gif!

!http://image.weather.com/web/common/wxicons/31/28.gif!

!http://image.weather.com/web/common/wxicons/31/29.gif!

!http://image.weather.com/web/common/wxicons/31/30.gif!

!http://image.weather.com/web/common/wxicons/31/31.gif!

!http://image.weather.com/web/common/wxicons/31/32.gif!

!http://image.weather.com/web/common/wxicons/31/33.gif!

!http://image.weather.com/web/common/wxicons/31/34.gif!

!http://image.weather.com/web/common/wxicons/31/35.gif!

!http://image.weather.com/web/common/wxicons/31/36.gif!

!http://image.weather.com/web/common/wxicons/31/37.gif!

!http://image.weather.com/web/common/wxicons/31/38.gif!

!http://image.weather.com/web/common/wxicons/31/39.gif!

!http://image.weather.com/web/common/wxicons/31/40.gif!

!http://image.weather.com/web/common/wxicons/31/41.gif!

!http://image.weather.com/web/common/wxicons/31/42.gif!

!http://image.weather.com/web/common/wxicons/31/43.gif!

!http://image.weather.com/web/common/wxicons/31/44.gif!

!http://image.weather.com/web/common/wxicons/31/45.gif!

!http://image.weather.com/web/common/wxicons/31/46.gif!

!http://image.weather.com/web/common/wxicons/31/47.gif!

!http://image.weather.com/web/common/wxicons/31/na.gif!

!https://weblogs.sdn.sap.com/weblogs/images/11062/transparent_orange_go.gif|height=19|alt=image|width=28|src=https://weblogs.sdn.sap.com/weblogs/images/11062/transparent_orange_go.gif|border=0!

!https://weblogs.sdn.sap.com/weblogs/images/11062/TWClogo_32px.gif|height=32|alt=image|width=43|src=https://weblogs.sdn.sap.com/weblogs/images/11062/TWClogo_32px.gif|border=0!

image

Step4:

Modify your Display.java to add the following code.

Make sure you change the code to use your partner id and key sent to you by weather.com. This code will not work till you update your information in it.

/* Built By: Prakash Singh

  • Technical Consultant

  • Universal System Technologies, Inc

  • 2500 W. Lake Mary Blvd., Ste 212-A

  • Lake Mary, FL 32746 USA

  • M 407-474-2216

*/

package com.ust.weather;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import com.sapportals.htmlb.DropdownListBox;

import com.sapportals.htmlb.Form;

import com.sapportals.htmlb.GridLayout;

import com.sapportals.htmlb.GridLayoutCell;

import com.sapportals.htmlb.Link;

import com.sapportals.htmlb.enum.CellHAlign;

import com.sapportals.htmlb.enum.DropdownListBoxDesign;

import com.sapportals.htmlb.event.Event;

import com.sapportals.htmlb.htmlcontainer.NonIsolatedHtmlContainer;

import com.sapportals.htmlb.page.DynPage;

import com.sapportals.htmlb.page.PageException;

import com.sapportals.portal.htmlb.page.PageProcessorComponent;

import com.sapportals.portal.prt.component.IPortalComponentInitContext;

import com.sapportals.portal.prt.component.IPortalComponentRequest;

import com.sapportals.portal.prt.component.PortalComponentException;

import com.sapportals.portal.prt.contentconversion.XSLConverter;

import com.sapportals.portal.prt.logger.ILogger;

import com.sapportals.portal.prt.runtime.IPortalConstants;

import com.sapportals.portal.prt.runtime.PortalRuntime;

/**

  • @author Prakash Singh

*/

public class display extends PageProcessorComponent {

     /* (non-Javadoc)

     

  • @see com.sapportals.portal.htmlb.page.PageProcessorComponent#getPage()

      */

     public DynPage getPage() {

          return new RSSDynPage();

     }

     /* (non-Javadoc)

     

  • @see com.sapportals.portal.prt.component.IPortalComponentInit#init(com.sapportals.portal.prt.component.IPortalComponentInitContext)

      */

     public void init(IPortalComponentInitContext arg0)

          throws PortalComponentException {

          // TODO Auto-generated method stub

          super.init(arg0);

     }

     public class RSSDynPage extends DynPage {

          /* (non-Javadoc)

          

  • @see com.sapportals.htmlb.page.DynPage#doInitialization()

          */

          private String location = “”;

          private String center = “”;

          private String tendayurl = “”;

          public void doInitialization() throws PageException {

               // TODO Auto-generated method stub

               location = “”;

          }

          /* (non-Javadoc)

          

  • @see com.sapportals.htmlb.page.DynPage#doProcessAfterInput()

           */

          public void doProcessAfterInput() throws PageException {

               // TODO Auto-generated method stub

          }

          

          public void onSelect(Event vent) throws PageException {

               DropdownListBox centerfld =

                                   (DropdownListBox) getComponentByName(“DDLocation”);

             location = centerfld.getValueAsDataType().toString();

                    }

          /* (non-Javadoc)

          

  • @see com.sapportals.htmlb.page.DynPage#doProcessBeforeOutput()

           */

          public void doProcessBeforeOutput() throws PageException {

               ILogger logger =

                    PortalRuntime.getLogger(IPortalConstants.IVIEW_LOGGER);

               BufferedReader reader = null;

               try {

                    IPortalComponentRequest request =

                         (IPortalComponentRequest) this.getRequest();

                    URL url = getURL();

                    reader =

                         new BufferedReader(

                              new InputStreamReader(

                                   new BufferedInputStream(url.openStream())));

                    StringBuffer buff = new StringBuffer();

                    for (int i = reader.read(); i != -1; i = reader.read()) {

                         buff.append((char) i);

                    }

                    reader.close();

                    String rssString = buff.toString();

                    //get rss stylesheet

                    File file = getRssStyleSheet(request);

                    XSLConverter converter = new XSLConverter(file, null);

                    String result = converter.convert(rssString, request);

                    NonIsolatedHtmlContainer container =

                         new NonIsolatedHtmlContainer(

                              new ByteArrayInputStream(result.getBytes()));

                    Form form = this.getForm();

                    form.addRawText(“

Deploy your component and run it. BAM! you got yourself a nice weather magnet.

image

Assigned Tags

      27 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Prakesh

      Excellent. This is what I am looking for. Where can I get contentconverterapi.jar,contentconversionapi.jar files. I would appreciate if you can email them to r_basis@yahoo.com

      Thanks
      Raghu

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Thank you Raghu. I have sent you the jar files.
      Author's profile photo Former Member
      Former Member
      Hi Prakash,
        That was an interseting work.I just wanted to try the same.How to find the XSLConverter in the display program.Where can i find the contentconverterapi.jar.

      regards
      Gopi

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Thank you Gopi. If you email me at psingh@ust.net then i will send you both the jar files. This will solve your XSLConverter class.
      Author's profile photo Former Member
      Former Member
      Your blog is perhaps one of the most useful blogs for content developers.  Lots of good stuff!

      Where can I find the two jar files in my Portals distribution?  I saw a note on SDN where you pointed someone to the portal for the files, but for the life of me, I can't find them.  Or, if it's easier, you can mail them to 'ken.miller at huskyenergy.com'.

      Keep up the good work!

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Thank you Ken. I have sent you the jar files. Sorry i haven't had a chance to respond to your email about message broadcaster. Did you finally figure it out? Let me know if you have still have a question on it.

      Prakash.

      Author's profile photo Thomas Jung
      Thomas Jung
      I was going to recreate your example in BSP.  I have ran into a little problem and I thought I might post it here in case anyone else tries. 

      The problem is that I can't get the call to xoap.weather.com come to return correctly either with an RFC HTTP Connection or by coding the Client call itself with cl_http_client.  It appears that my request keeps being sent to www.weather.com instead.

      Doing some research on line it appears that weather.com is using a Name-Based Virtual Hosting scheme.  They are running on Apache with HTTP version 1.0.  It appears that with this setup, the client must make sure that the host request header field is properly formed because that is what the server will use to direct to the correct virtual host.  I verified that this was the problem using a website called web-sniffer.net.  I was able to recreate the same problem by sending a HTTP/1.0 request wihtout the host header to the xoap.weather.com address with this website. 

      So I did a trace on the ICM call out of SAP to see what the problem was.  SAP properly included the host header, except they always include the service port number.  So what I saw in the trace was host:xoap.weather.com:80.  I checked on the www.w3.org and it doesn't say you shouldn't include the port if it is the default.  It mearly says if you don't include the port, the server will assume the default port:
      "A "host" without any trailing port information implies the default port for the service requested (e.g., "80" for an HTTP URL). "

      However Weather.com's server can't seem to handle port 80 being on the end of the host header.  To verify this I debugged down into the HTTP client and changed the value of the port in memory before it makes the call. Sure enough, the proper XML was returned.

      However there apprears to be no way to specify no Port in the ABAP coding of the HTTP Client.  The following code shows that No Port leads to 80 being defaulted in.
      if service is initial.
          if scheme = schemetype_http.
            service = '80'.
          else.
            service = '443'.
          endif.
        else.

      I'm afraid that without changing SAP's ABAP code in the HTTP Client Class, there isn't a way to easily replicate this example in BSP. 

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Thomas,
           I am glad you tried it out but It should still work with port number specified in the url. If you try the following url in IE it returns XML data.
      http://xoap.weather.com:80/weather/local/32779?cc=*&prod=xoap&par=1006383251&key=c1e3c6d7d69bcc41
      Maybe somebody will try this in PHP. Thanks again.

      Regards,
      Prakash Singh

      Author's profile photo Thomas Jung
      Thomas Jung
      Yes, but just because you put the port in the URL in IE doesn't mean that IE is inserting that into the host field of the HTTP header.  You would need a tool that snifs the HTTP header request to see that.  When I tested with my tool, IE does not include the port in the header field value if it is port 80.
      Author's profile photo Former Member
      Former Member
      Blog Post Author
      You are correct. I just tried it on my firefox and it does goes to weather.com. Looks like IE erases the port 80 and weather.com can't handle port 80 in the url( i am repeating your words).
      Author's profile photo Thomas Jung
      Thomas Jung
      I went ahead and bit the bullet so to speak.  I changed the SAP ABAP HTTP Client code ever so slightly to allow for compatibility with Weather.Com.  That way I was able to complete the port to BSP:
      BSP: Create a weather magnet using xml feed from weather.com

      I think the results are rather intersting in that JAVA and ABAP are similar in approach and functionality in creating this solution.

      Author's profile photo Thomas Jung
      Thomas Jung
      The HTTP Client problem with the ports should soon be offically fixed.  Have a look at OSS note 858970 for the details.  Thanks goes to Brian McKellar for keeping on top of this.
      Author's profile photo Former Member
      Former Member
      This is a great weblog! I was looking for something like this for quite some time now.

      I also need those two *.jar files, could you email them to me (gesche.raabe@stud.fh-wilhelmshaven.de)?

      Thanks a lot!

      Regards,
      Gesche

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Than you Gesche, I will send you the jar files.
      Author's profile photo Former Member
      Former Member
      So the only SAP UI technology left behind now is WebDynpro?
      Sounds like a challenge for me 😉

      VS

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Cool! We would love to see it.
      Author's profile photo Former Member
      Former Member
      Prakesh

      Very nice job. I am eager to get this on our Portal.  I would appreciate if you could email the files to rrios@watsonpharm.com
      (contentconverterapi.jar,contentconversionapi.jar)

      Thanks
      Rudy

      Author's profile photo Former Member
      Former Member
      Blog Post Author
      Hi Rudy,
         Thank you for your kind feeback. I have sent you the jars.

      Prakash

      Author's profile photo Former Member
      Former Member
      HI Prakash,

      Can you please send me those 2 jar files to sathya.berigai@gmail.com.

      Thanks,
      Sathya

      Author's profile photo Former Member
      Former Member
      Hi Parkash,
      Could you kindly email me the two jars required for this iView, please?

      contentconverterapi.jar,contentconversionapi.jar

      Thank you,
      umair.salam@gmail.com

      Author's profile photo Former Member
      Former Member
      That was an interesting work.I too have the same requirement.so i need the two contentconverterapi.jar,contentconversionapi.jar files. could you email them to me? My email id is yogalakshmi.sathyanarayanan@cognizant.com
      Author's profile photo Former Member
      Former Member
      Hi Parkash,

      Could you please email me the two jars required

      for this iView, please?

      contentconverterapi.jar,contentconversionapi.jar

      Thank you,
      evgenykl@gmail.com

      Author's profile photo Former Member
      Former Member
      It is a good article. I had done similar one by integrating with web serivces. But I would like to integrate with weather.com. Can u plz send the 2 jars to dheeram.kallem@gmail.com that are used in this article, so that I can work on this.
      Thanx,
      Dheeram
      Author's profile photo Former Member
      Former Member

      Hi guys,Joerg Loechner

      Author's profile photo Former Member
      Former Member
      Hi Prakash,

      Great job ! Very usefull blog to many professional. Thanks.

      But, when I deploy the application I get foll error :

      Portal Runtime Error

      An exception occurred while processing a request for :
      iView : com.ust.weather.display
      Component Name : com.ust.weather.display

      xoap.weather.com.
      Exception id: 03:27_03/03/07_0766_1198650
      See the details for the exception ID in the log file

            Please, kindly let me know where iam going wrong.

      Appreciate your valuable inputs.

      Thanks in advance,
      Ravi Kumar

      Author's profile photo Former Member
      Former Member
      Hi Parkash,

      Your blog is very nice and cleary explain.
      Please, Could you kindly email me the two jars required for this iView?
      (contentconverterapi.jar,contentconversionapi.jar)

      my email:mathiue2002@hotmail.com

      Thank you,
      Rasim

      Author's profile photo Former Member
      Former Member
      Hello Prakash,

      I need the two jars mailme to : mythu.vthluv@gmail.com.

      Very interesting blog.

      Regards,
      Aksiti.