<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Technotrance, Illusions and Perspectives &#187; yahoo-rest</title>
	<atom:link href="http://myblog.shriharisc.com/tag/yahoo-rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://myblog.shriharisc.com</link>
	<description>A dose of everyday bruises with Java/JEE</description>
	<lastBuildDate>Mon, 02 Aug 2010 17:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Playing with Yahoo Maps on PrimeFaces</title>
		<link>http://myblog.shriharisc.com/2009/08/04/playing-with-yahoo-maps-on-primefaces/</link>
		<comments>http://myblog.shriharisc.com/2009/08/04/playing-with-yahoo-maps-on-primefaces/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 12:22:52 +0000</pubDate>
		<dc:creator>Shrihari</dc:creator>
				<category><![CDATA[jee-light]]></category>
		<category><![CDATA[primefaces]]></category>
		<category><![CDATA[yahoo-rest]]></category>

		<guid isPermaLink="false">http://schakrap.wordpress.com/?p=90</guid>
		<description><![CDATA[Yahoo Map services expose REST web-services API which aid the consumer to retreive maps given a locations based on address (street, city, state or zip) or coordinates (longitude, latitude). This cool tip explores an option to render a Yahoo hosted map for a PrimeFaces driven application.
1) Login to Yahoo Developer Network and generate an Yahoo [...]]]></description>
			<content:encoded><![CDATA[<p>Yahoo Map services expose <a href="http://developer.yahoo.com/maps/rest/V1/">REST web-services API </a>which aid the consumer to retreive maps given a locations based on address (street, city, state or zip) or coordinates (longitude, latitude). This cool tip explores an option to render a Yahoo hosted map for a <a href="http://primefaces.prime.com.tr/">PrimeFaces</a> driven application.</p>
<p>1) Login to Yahoo Developer Network and generate an <a href="http://developer.yahoo.com/wsregapp/">Yahoo Developer Application ID</a>.</p>
<p>2) Define and implement a PrimeFaces Controller which defines a MapMetadata POJO (containing address based attributes or coordinate based attributes) and defines a method to render the map accordingly.</p>
<pre class="brush: java">
@Controller(name = &quot;mapController&quot;, scope = REQUEST)
public class YahooMapController
{   private String mapURL = new String();
    private boolean showMap = false;
    private MapMetadata map = new MapMetadata();
    //setters and accessors
    public void renderMapByAddress()
    {   StringBuilder query = new StringBuilder();
        String details = map.getStreet();
        query.append((details!=null)?&quot;street=&quot; + details + &quot;&amp;amp;&quot;:&quot;&quot;);
        details = map.getCity();

        query.append((details!=null)?&quot;city=&quot; + details + &quot;&amp;amp;&quot;:&quot;&quot;);
        details = map.getState();

        query.append((details!=null)?&quot;state=&quot; + details + &quot;&amp;amp;&quot;:&quot;&quot;);
        details = map.getZip();

        query.append((details!=null)?&quot;zip=&quot; + details + &quot;&amp;amp;&quot;:&quot;&quot;);
        details = map.getLatitude();

        query.append((details1!=null)?&quot;latitude=&quot; + details1 + &quot;&amp;amp;&quot;:&quot;&quot;);
        details = map.getLongitude();

        query.append((details!=null)?&quot;longitude=&quot; + details + &quot;&amp;amp;&quot;:&quot;&quot;);
       renderMap(query.toString());
    }

    private void renderMap(String query)
    {   if (query.length() &gt; 0)
        {   try {
                query = query.replace(&quot; &quot;, &quot;+&quot;);
                String requestquery = &quot;http://local.yahooapis.com/MapsService/V1/mapImage?appid=[my-app-id]&quot; + query;
                HttpClient client = new HttpClient();
                GetMethod method = new GetMethod(requestquery);
                int statusCode = client.executeMethod(method);
                if (statusCode != HttpStatus.SC_OK)
                {   FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERTY_ERROR, &quot;Unable to find map based on details!&quot;, &quot;Unable to find map based on details!&quot;));
                    showMap = false;
                    return;
                }
                InputStream rstream = method.getResponseBodyAsStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(rstream));
		String line;
		int i = 0;
		while ((line = br.readLine()) != null)
                {   if (i == 1 &amp;amp;&amp;amp; line.contains(&quot;Result&quot;))
                    {   mapURL = line.substring(62, line.length() - 9);
                        showMap = true;
                    }
                    i++;
		}
            } catch (IOException ex)
            {   ex.printStackTrace();
            }
        }
    }
}
</pre>
<p>The REST Get request is being placed using the Apache Commons <a href="http://hc.apache.org/httpclient-3.x/">HTTPClient</a> library. The response from REST Get call is striped to fetch only the URL which displays the actual image of the Map.</p>
<p>3) Define and implement a XHTML page which defines a the view for accepting the location details and a rendering panel which is displayed if the map is successfully retrieved.</p>
<pre class="brush: xml">
&lt;ui:define name=&quot;content&quot;&gt;
    &lt;h:form&gt;
        &lt;h:messages/&gt;
        &lt;p:panel header=&quot;View Map&quot;&gt;
          &lt;h:panelGrid columns=&quot;2&quot;&gt;
             &lt;h:outputLabel value=&quot;Street :&quot; for=&quot;street&quot;&gt;&lt;/h:outputLabel&gt;
             &lt;h:inputText id=&quot;street&quot; value=&quot;#{mapController.map.street}&quot;/&gt;

             &lt;!-- Other input fields with labels accordingly --&gt;

            &lt;h:panelGroup&gt;
             &lt;h:commandButton action=&quot;#{mapController.renderMapByAddress}&quot; value=&quot;Show Map&quot; /&gt;
            &lt;/h:panelGroup&gt;
          &lt;/h:panelGrid&gt;
        &lt;/p:panel&gt;
        &lt;p:panel header=&quot;Map&quot; rendered=&quot;#{mapController.showMap}&quot;&gt;
           &lt;h:panelGrid columns=&quot;1&quot;&gt;
              &lt;h:graphicImage value=&quot;#{mapController.mapURL}&quot;/&gt;
           &lt;/h:panelGrid&gt;
        &lt;/p:panel&gt;
     &lt;/h:form&gt;
 &lt;/ui:define&gt;
</pre>
<p>The graphicImage from JSF HTML taglib displays the URL generated image. One can also use the PrimeFaces <a href="http://97.107.138.40:8080/prime-showcase/ui/dynamicImage.jsf">dynamicImage</a> Streaming (with StreamedContent)</p>
]]></content:encoded>
			<wfw:commentRss>http://myblog.shriharisc.com/2009/08/04/playing-with-yahoo-maps-on-primefaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
