<?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>Tara Andrei &#187; java</title>
	<atom:link href="http://andreitara.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://andreitara.com</link>
	<description>I could change the world if they would let my allocate more instances of myself.</description>
	<lastBuildDate>Mon, 13 Jun 2011 21:32:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Social developing</title>
		<link>http://andreitara.com/2011/02/social-developing/</link>
		<comments>http://andreitara.com/2011/02/social-developing/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 20:51:22 +0000</pubDate>
		<dc:creator>andreiT</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[facebook java API]]></category>
		<category><![CDATA[facebook java login]]></category>
		<category><![CDATA[social application]]></category>

		<guid isPermaLink="false">http://www.andreitara.com/?p=342</guid>
		<description><![CDATA[At the beginning the socialization networks were for me just a childish thing but I have figure  out the power of social networks, that can be used not only to promote out applications but also to increase the number of users. Well when I am specking about social networks I am thinking first of all [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.andreitara.com/wp-content/uploads/2011/02/facebook.jpg"><img class="alignleft size-medium wp-image-345" style="margin: 10px;" title="facebook" src="http://www.andreitara.com/wp-content/uploads/2011/02/facebook-300x112.jpg" alt="" width="180" height="67" /></a>At the beginning the <strong>socialization networks</strong> were for me just a childish thing but I have figure  out the power of social networks, that can be used not only to promote out applications but also to increase the number of users. Well when I am specking about social networks I am thinking first of all on <strong><span id="more-342"></span>Facebook</strong>. Facebook is &#8220;<strong>the social network </strong>&#8221; of our days used by millions of user so try to imagine how cool wold be that this users would use and share our application. They are providing to developers a very powerful API that can be used to  develop social application. OK but what&#8217;s a social application you may  say ? well in big therms a social application is noting more than a  application that you used together with you&#8217;re friends. Facebook expose services through they&#8217;re API that is available for JavaScript and PHP, but they don&#8217;t offer any support for Java. There is a community effort to implement a API for Java (see more on http://restfb.com/) but there are a lot of things to be implemented. One of the missing part for this is the login. So I will show you how to implement authentication for Facebook application wrote in Java but before that let&#8217;s say just two words about OAuth 2.0.</p>
<p>OAuth 2.0 protocol was made in 2006 and is used for authentication and authorization  in the most of the web services from today ( in special REST service ) in two words you can authenticate users in Web applications via redirects, in Javascript or in desktop and mobile applications. Let&#8217;s take a look at the following schema (see more on http://www.ibm.com/developerworks/web/library/wa-oauthsupport/?ca=drs-) this schema may be referenced as tree legged OAuth dance. This protocol ensure a simple way to access some private resource with a access token.<strong> <a name="N100D0"></a></strong><a href="http://www.andreitara.com/wp-content/uploads/2011/02/ThreeLeggedOAuthDance.gif"><img class="size-full wp-image-343 alignnone" title="ThreeLeggedOAuthDance" src="http://www.andreitara.com/wp-content/uploads/2011/02/ThreeLeggedOAuthDance.gif" alt="" width="568" height="469" /></a> So enough with the theory now is time for practice:</p>
<blockquote><p>/**<br />
* Authenticate on Facebook as user<br />
*<br />
* @param request<br />
*            Servlet request<br />
* @param response<br />
*            Servlet response<br />
* @param scope<br />
*            Comma separated permissions<br />
* @see http://developers.facebook.com/docs/authentication/permissions<br />
* @throws IOException<br />
*/<br />
public void authenticateAsUser(String scope) throws IOException {</p>
<p>log.info(&#8220;Executing FB AUTH&#8221;);</p>
<p>// &#8216;extract&#8217; HTTP request and response</p>
<p>HttpServletResponse httpResponse = (HttpServletResponse) response;</p>
<p>// Check if the user is authenticated already<br />
if (getFacebookToken() != null) {<br />
log.info(&#8220;User is already autheticated&#8221;);<br />
return;<br />
}</p>
<p>// Get the code parameter posted by Facebook<br />
String code = request.getParameter(&#8220;code&#8221;);</p>
<p>// The callback URL<br />
String nextURL = fbAppCanvasURL;</p>
<p>log.info(nextURL);</p>
<p>// OAuth step 1<br />
if (code == null) {</p>
<p>log.info(&#8220;Auth steph 1&#8243;);</p>
<p>@SuppressWarnings(&#8220;deprecation&#8221;)<br />
String requestAdress = String<br />
.format(&#8220;https://graph.facebook.com/oauth/authorize?client_id=%s&amp;canvas=true&amp;display=page&amp;redirect_uri=%s&amp;scope=%s&#8221;,<br />
fbClientAppId, URLEncoder.encode(nextURL), scope);</p>
<p>log.info(requestAdress);</p>
<p>// Make a call to Facebook to invoke authentication<br />
// httpResponse.sendRedirect(requestAdress);<br />
String out = String<br />
.format(&#8220;&lt;script type=\&#8221;text/javascript\&#8221;&gt;\ntop.location.href = \&#8221;%s\&#8221;;\n&lt;/script&gt;&#8221;,<br />
requestAdress);<br />
httpResponse.getWriter().print(out);<br />
httpResponse.flushBuffer();</p>
<p>log.info(&#8220;End of steph 1&#8243;);</p>
<p>}<br />
// OAuth step 2<br />
// the &#8216;code&#8217; parameter is sent in the 2&#8242;nd authentication phase<br />
// if we have the code param the we are in the 2&#8242;nd phase<br />
else {<br />
log.info(&#8220;Auth steph 2&#8243;);</p>
<p>// request URL to make the 2&#8242;nd call to Facebook<br />
@SuppressWarnings(&#8220;deprecation&#8221;)<br />
String requestAdress = String<br />
.format(&#8220;https://graph.facebook.com/oauth/access_token?client_secret=%s&amp;code=%s&amp;client_id=%s&amp;redirect_uri=%s&#8221;,<br />
fbClientAppSecret, URLEncoder.encode(code),<br />
fbClientAppId, URLEncoder.encode(nextURL));</p>
<p>log.info(requestAdress);</p>
<p>// The 2&#8242;nd phase doesen&#8217;t involve the user interaction<br />
// so we can read the result directly on server with a stream reader<br />
URL requestURL = new URL(requestAdress);</p>
<p>// Open a connection to Facebook<br />
BufferedReader in = new BufferedReader(new InputStreamReader(<br />
requestURL.openStream()));</p>
<p>// Read the response and store the result in<br />
// resultString buffer<br />
String inputLine;<br />
StringBuilder resultString = new StringBuilder();</p>
<p>while ((inputLine = in.readLine()) != null)<br />
resultString.append(inputLine);</p>
<p>// Close the connection stream<br />
in.close();</p>
<p>log.info(&#8220;Response string:&#8221;);<br />
log.info(resultString.toString());</p>
<p>// fetch access token &amp; expiration time from response string<br />
String[] params = resultString.toString().split(&#8220;&amp;&#8221;);<br />
params[0] = params[0].split(&#8220;access_token=&#8221;)[1];<br />
// params[1] = params[1].split(&#8220;expires=&#8221;)[1];</p>
<p>// Store them in cookies<br />
log.info(&#8220;Store them in cookies&#8221;);<br />
log.info(params[0]);</p>
<p>httpResponse.addCookie(new Cookie(&#8220;FB_ACCESS_TOKEN&#8221;, params[0]));</p>
<p>// httpResponse.addCookie(new Cookie(&#8220;FB_EXPIRE&#8221;,<br />
// params[1]));</p>
<p>log.info(&#8220;End of steph 2&#8243;);<br />
log.info(&#8220;Exiting Facebook authentication&#8221;);</p>
<p>}</p>
<p>}</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://andreitara.com/2011/02/social-developing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML serialization of objects in C# and Java</title>
		<link>http://andreitara.com/2010/10/xml-serialization-of-objects-in-c-and-java/</link>
		<comments>http://andreitara.com/2010/10/xml-serialization-of-objects-in-c-and-java/#comments</comments>
		<pubDate>Sun, 24 Oct 2010 19:04:19 +0000</pubDate>
		<dc:creator>andreiT</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.andreitara.com/?p=294</guid>
		<description><![CDATA[In this post I will explain some basic methods for creating XML serialization of objects in C# and Java but before let&#8217;s take a look and see what actually XML is and when should we use it. XML or eXtended Markup Language is as it says his name nothing more the a markup language same [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.andreitara.com/wp-content/uploads/2010/10/gateau-geek-xml.jpg"><img class="alignleft size-full wp-image-300" title="gateau-geek-xml" src="http://www.andreitara.com/wp-content/uploads/2010/10/gateau-geek-xml.jpg" alt="" width="287" height="215" /></a>In this post I will explain some basic methods for creating XML serialization of objects in C# and Java but before let&#8217;s take a look and see what actually XML is and when should we use it.<br />
XML or eXtended Markup Language is as it says his name nothing more the a markup language same as HTML, but the main difference beside the possibility of creating you&#8217;re own tags, is that XML is used to structure information.<span id="more-294"></span> The posibility of structuring information made from XML a really useful tool to transport data across application that can even reside on different machines across internet, here we can remember the REST  and OData protocol. So  XML can be used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.</p>
<p>Now that we know what is XML good for is time to get in to the business and write out first class that cam be serialized as XML.</p>
<p>C#</p>
<blockquote>
<pre escaped="true" lang="c#" line="1">using System.Xml.Serialization;
[XmlRoot(ElementName = "PersonalInformations")]

public class PersonalInformations
{
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }

[XmlAttribute(AttributeName="age")]
public uint Age { get; set; }
[XmlAttribute(AttributeName="birth")]
public DateTime DateOfBirth { get; set; }

public string Adress{get;set;}

public static void StoreObject(Stream outStream, PersonalInformations obj)
{
XmlSerializer x = new XmlSerializer(typeof(PersonalInformations));
x.Serialize(outStream, obj);
}
}
public static PersonalInformations LoadObject(Stream inStream)
{
XmlSerializer x = new XmlSerializer(typeof(PersonalInformations));
return  x.Deserialize(inStream) as PersonalInformations;
}
}</pre>
</blockquote>
<p>Java</p>
<blockquote>
<pre escaped="true" lang="java" line="1">@Root(name="PersonalInformations")
class PersonalInformations {

@Attribute(name="name")
private String name;

@Attribute(name="age")
private int age;

@Attribute(name="birth")
private Date dateOfBirth;

@Element(name="birth")
private Date dateOfBirth;

public static void storeObject(Stream outStream, PersonalInformations obj)

{
      Serializer serializer = new Persister();
      serializer.write(obj, outStream);
}

}

public static PersonalInformations loadObject(Stream inStream)

{
      Serializer serializer = new Persister();
      return (PersonalInformations) serializer.read (PersonalInformations.class,inStream);
}

}</pre>
</blockquote>
<p>In the case  of C# we made use of the classes from System.Xml.Serialization namespace, that offers a simple way to serialize classes. In case of Java we use a third part library that can be downloaded from http://simple.sourceforge.net. In both cases we annote the class fields to inform the serialization service about the way we want to create the XML.<br />
As you maybe have already noticed from this sample, XML has become more then a way to store structured information, it is actually a way to express an object state. State that can be stored or sent across applications, applications that can be write in different programming languages.</p>
<p>The goal of this post is not informing about how to serialize classes to XML but to show the simplicity and beauty of XML in communication between applications.</p>
]]></content:encoded>
			<wfw:commentRss>http://andreitara.com/2010/10/xml-serialization-of-objects-in-c-and-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web browser in 2 pasi</title>
		<link>http://andreitara.com/2009/01/web-browser-in-2-pasi/</link>
		<comments>http://andreitara.com/2009/01/web-browser-in-2-pasi/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 21:26:22 +0000</pubDate>
		<dc:creator>andreiT</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[others]]></category>
		<category><![CDATA[java web browser]]></category>
		<category><![CDATA[web broser]]></category>

		<guid isPermaLink="false">http://www.andreitara.com/?p=79</guid>
		<description><![CDATA[Recent intr-un proiect am avut nevoie de un web browser propriu. In loc sa folosesc un activeX de Internet Explo(r/d)er am ales sa imi constriuesc propria componenta din mai multe considerente: in primul rand proiectul era in Java si nu doream sa folosesc JNI (java native interface- pentru cei care nu sunt familiarizati cu termenul [...]]]></description>
			<content:encoded><![CDATA[<p>Recent intr-un proiect am avut nevoie de un web browser propriu. In loc sa folosesc un activeX de Internet Explo(r/d)er am ales sa imi constriuesc propria componenta din mai multe considerente:  in primul rand proiectul era in Java si  nu doream sa folosesc JNI (java native interface- pentru cei care nu sunt familiarizati cu termenul in doua cuvinte este vorba de o tehnologie ce va permite sa accesati cod nativ din programele de java.)  si al doilea motiv era ca nu aveam nevoie de un web browser foarte complex care sa suporte &#8216;nspe mii de plugin-uri.<br />
Iata un exemplu minimal :</p>
<p>import javax.swing.event.HyperlinkEvent;<br />
import javax.swing.*;<br />
import java.io.*;<br />
import javax.swing.event.HyperlinkListener;</p>
<p>public class SimpleWebBrowser {<br />
static JEditorPane jep = new JEditorPane();<br />
public static void main(String[] args) {</p>
<p>String initialPage = &#8220;http://www.google.ro/&#8221;;<br />
if (args.length &gt; 0) initialPage = args[0];</p>
<p>jep.setEditable(false);</p>
<p>jep.addHyperlinkListener(new HyperlinkListener() {</p>
<p>public void hyperlinkUpdate(HyperlinkEvent e) {<br />
try {<br />
jep.setPage(e.getURL());<br />
} catch (IOException ex) {<br />
Logger.getLogger(SimpleWebBrowser.class.getName()).log(Level.SEVERE, null, ex);<br />
}<br />
}<br />
});</p>
<p>try {<br />
jep.setPage(initialPage);<br />
}<br />
catch (IOException e) {<br />
System.err.println(&#8220;Usage: java SimpleWebBrowser url&#8221;);<br />
System.err.println(e);<br />
System.exit(-1);<br />
}</p>
<p>JScrollPane scrollPane = new JScrollPane(jep);<br />
JFrame f = new JFrame(&#8220;Simple Web Browser&#8221;);<br />
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);<br />
f.getContentPane().add(scrollPane);<br />
f.setSize(512, 342);<br />
f.show();</p>
<p>}</p>
<p>}</p>
<p>Si iata cum ar arata &#8220;miniborwser-ul&#8221;</p>
<p><a href="http://www.andreitara.com/wp-content/uploads/2009/01/untitled2.jpg"><img title="untitled2" src="http://www.andreitara.com/wp-content/uploads/2009/01/untitled2-300x186.jpg" alt="" width="300" height="186" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://andreitara.com/2009/01/web-browser-in-2-pasi/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

