tag:blogger.com,1999:blog-40741415626001829402024-02-20T18:47:28.661+01:00bioinformatics programming, java, eclipse rcp, gwt, grailsBlog about cool new technologies programming.Programminghttp://www.blogger.com/profile/06893571938614656263noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4074141562600182940.post-39276132923678376632006-12-12T23:53:00.000+01:002006-12-12T23:54:19.768+01:00gwt open source<strong>wow! </strong>I wait for that.Programminghttp://www.blogger.com/profile/06893571938614656263noreply@blogger.com0tag:blogger.com,1999:blog-4074141562600182940.post-33628761823789663872006-12-12T00:34:00.001+01:002010-04-08T18:51:44.464+02:00Lazy loading FCKeditor in GWT applicationPrinciple is <a href="http://ajaxpatterns.org/On-Demand_Javascript">On-demand script</a>.
My sample utils <b>(not tested!)</b> class for create <a href="http://www.fckeditor.net/">FCKeditor</a> when is neaded.
This class is used in <a href="http://code.google.com/webtoolkit/">google GWT</a> application, and it is not tested but show
principle.
<br />
<pre><span style="color: maroon; font-weight: bold;">package</span><span style="color: #004a43;"> net</span><span style="color: #808030;">.</span><span style="color: #004a43;">ct</span><span style="color: #808030;">.</span><span style="color: #004a43;">sajt</span><span style="color: #808030;">.</span><span style="color: #004a43;">client</span><span style="color: #808030;">.</span><span style="color: #004a43;">ui</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">util</span><span style="color: #808030;">.</span><span style="color: #004a43;">ArrayList</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">util</span><span style="color: #808030;">.</span><span style="color: #004a43;">List</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> net</span><span style="color: #808030;">.</span><span style="color: #004a43;">ct</span><span style="color: #808030;">.</span><span style="color: #004a43;">sajt</span><span style="color: #808030;">.</span><span style="color: #004a43;">client</span><span style="color: #808030;">.</span><span style="color: #004a43;">Log</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">google</span><span style="color: #808030;">.</span><span style="color: #004a43;">gwt</span><span style="color: #808030;">.</span><span style="color: #004a43;">user</span><span style="color: #808030;">.</span><span style="color: #004a43;">client</span><span style="color: #808030;">.</span><span style="color: #004a43;">Random</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">google</span><span style="color: #808030;">.</span><span style="color: #004a43;">gwt</span><span style="color: #808030;">.</span><span style="color: #004a43;">user</span><span style="color: #808030;">.</span><span style="color: #004a43;">client</span><span style="color: #808030;">.</span><span style="color: #004a43;">Timer</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">google</span><span style="color: #808030;">.</span><span style="color: #004a43;">gwt</span><span style="color: #808030;">.</span><span style="color: #004a43;">user</span><span style="color: #808030;">.</span><span style="color: #004a43;">client</span><span style="color: #808030;">.</span><span style="color: #004a43;">ui</span><span style="color: #808030;">.</span><span style="color: #004a43;">HTML</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Sampl class for load FCK editor.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Usage</span><span style="color: #7f9fbf; font-weight: bold;">:</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">* </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> ... new HTML("<textarea id="\te\" name="\te\" textarea>...</span><span style="color: #3f5fbf;">")</span><span style="color: #7f9fbf; font-weight: bold;">;</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> FCKEditorUtil</span><span style="color: #008c00;">.</span><span style="color: #3f5fbf;">createScript("</span><span style="color: #5555dd;">http://localhost:8080/web/js/FCKeditor/fckeditor.js</span><span style="color: #3f5fbf;">"</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> "</span><span style="color: #008c00;">34534</span><span style="color: #3f5fbf;">"</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> true)</span><span style="color: #7f9fbf; font-weight: bold;">;</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> final Timer timer = new Timer() {</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> public void run() {</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> if(FCKEditorNews</span><span style="color: #008c00;">.</span><span style="color: #3f5fbf;">show()) { // while browser load script</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> cancel()</span><span style="color: #7f9fbf; font-weight: bold;">;</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> return</span><span style="color: #7f9fbf; font-weight: bold;">;</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> } </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> } </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> }</span><span style="color: #7f9fbf; font-weight: bold;">;</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> timer</span><span style="color: #008c00;">.</span><span style="color: #3f5fbf;">scheduleRepeating(</span><span style="color: #008c00;">100</span><span style="color: #3f5fbf;">)</span><span style="color: #7f9fbf; font-weight: bold;">;</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">class</span> FCKEditorUtil <span style="color: purple;">{</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Return html content from texarea.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> editorInstance</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> name of FCKEditor instance.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> html content</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: maroon; font-weight: bold;">native</span> <span style="color: #bb7977; font-weight: bold;">String</span> getContent<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> editorInstance<span style="color: #808030;">)</span> <span style="color: dimgrey;">/*-{ </span>
<span style="color: dimgrey;"> return $wnd.FCKeditorAPI.GetInstance(editorInstance).GetXHTML();</span>
<span style="color: dimgrey;"> }-*/</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Show editor if is loaded and return true</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> otherwise false.</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: maroon; font-weight: bold;">native</span> <span style="color: #bb7977;">boolean</span> show<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: dimgrey;">/*-{</span>
<span style="color: dimgrey;"> if($wnd.FCKeditor) {</span>
<span style="color: dimgrey;"> var oFCKeditor = new $wnd.FCKeditor( 'te' ) ;</span>
<span style="color: dimgrey;"> oFCKeditor.BasePath = "</span><span style="color: #5555dd;">http://localhost:8080/web/js/FCKeditor/</span><span style="color: dimgrey;">" ;</span>
<span style="color: dimgrey;"> oFCKeditor.ToolbarSet = 'Basic';</span>
<span style="color: dimgrey;"> oFCKeditor.Config["CustomConfigurationsPath"] = "</span><span style="color: #5555dd;"><a href="http://localhost:8080/web/js/FCKeditor/news_config.js">http://localhost:8080/web/js/FCKeditor/news_config.js</a>?</span><span style="color: dimgrey;">" + ( new Date() * 1 ) ; // not cache while develop </span>
<span style="color: dimgrey;"> oFCKeditor.ReplaceTextarea() ;</span>
<span style="color: dimgrey;"> $wnd.oFCKeditor = oFCKeditor;</span>
<span style="color: dimgrey;"> return true;</span>
<span style="color: dimgrey;"> }else {</span>
<span style="color: dimgrey;"> return false;</span>
<span style="color: dimgrey;"> }</span>
<span style="color: dimgrey;"> }-*/</span><span style="color: purple;">;</span>
<span style="color: dimgrey;">// contein script urls for prevent that not two same JS script load twice.</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: #bb7977; font-weight: bold;">List</span> cacheScript <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">new</span> ArrayList<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Create < script src=.... > if not exist alredy. </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> src javascript url</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> id any ID for <script id=...</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: #bb7977;">void</span> createScript<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> src<span style="color: #808030;">,</span> <span style="color: #bb7977; font-weight: bold;">String</span> id<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>cacheScript<span style="color: #808030;">.</span>contains<span style="color: #808030;">(</span>src<span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
createSrc<span style="color: #808030;">(</span>src<span style="color: #808030;">,</span> id<span style="color: #808030;">)</span><span style="color: purple;">;</span>
cacheScript<span style="color: #808030;">.</span>add<span style="color: #808030;">(</span>src<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: maroon; font-weight: bold;">native</span> <span style="color: #bb7977;">void</span> createSrc<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> src<span style="color: #808030;">,</span> <span style="color: #bb7977; font-weight: bold;">String</span> id<span style="color: #808030;">)</span> <span style="color: dimgrey;">/*-{ </span>
<span style="color: dimgrey;"> var head = $doc.getElementsByTagName("head")[0];</span>
<span style="color: dimgrey;"> var script = $doc.createElement('script'); </span>
<span style="color: dimgrey;"> script.id = id;</span>
<span style="color: dimgrey;"> script.type = 'text/javascript';</span>
<span style="color: dimgrey;"> script.src = src;</span>
<span style="color: dimgrey;"> </span>
<span style="color: dimgrey;"> //Indicates that the script is not going to generate any document content. The browser can continue parsing and drawing the page </span>
<span style="color: dimgrey;"> //script.defer = true;</span>
<span style="color: dimgrey;"> head.appendChild(script); </span>
<span style="color: dimgrey;"> }-*/</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Create <script></script> tag.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> src javascript url</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> id</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> noCache if true than add random number to end of script (e</span><span style="color: #008c00;">.</span><span style="color: #3f5fbf;">g. src </span><span style="color: #7f9fbf; font-weight: bold;">+</span><span style="color: #3f5fbf;"> "?</span><span style="color: #008c00;">234534</span><span style="color: #3f5fbf;">" )</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> because browser cache javascipt.</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">static</span> <span style="color: #bb7977;">void</span> createScript<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> src<span style="color: #808030;">,</span> <span style="color: #bb7977; font-weight: bold;">String</span> id<span style="color: #808030;">,</span> <span style="color: #bb7977;">boolean</span> noCache<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span>noCache<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
createSrc<span style="color: #808030;">(</span>src <span style="color: #808030;">+</span> <span style="color: #0000e6;">"?"</span> <span style="color: #808030;">+</span> <span style="color: #bb7977; font-weight: bold;">Random</span><span style="color: #808030;">.</span>nextInt<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span> id<span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span> </pre>
<pre> </pre>
<pre> </pre>
<pre> </pre>
<pre><a href="http://semgen.info/">SemGen</a> </pre>Programminghttp://www.blogger.com/profile/06893571938614656263noreply@blogger.com0tag:blogger.com,1999:blog-4074141562600182940.post-6489326758062530012006-12-11T19:44:00.001+01:002010-04-08T18:51:06.676+02:00How to access NCBI blast using java?<div class="MsoNormal">
</div>
<div class="MsoNormal">
<span lang="EN-US">Part I.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.ncbi.nlm.nih.gov/BLAST/">NCBI Blast</a> can be searched in two ways: using the web form available at their home page or by making our own custom interface.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">In this tutorial we will concentrate on the second approach, so we will implement http access, by using NCBI’s URL API.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">“The URL API is a standardized application program interface (API) for accessing the NCBI $QBl_{A}st$ system. It uses direct HTTP-encoded requests to NCBI web server.”</span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span></span></div>
<div class="MsoNormal">
<span lang="EN-US">In order to implement this access we must have the following prerequisite libraries available for use:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span>Jakarta Commons HttpClient</span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span>Castor</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">The quick outline of the steps required is as follows:</span></div>
<div class="MsoNormal">
<span lang="EN-US">First we query the NCBI blast query, after which we receive a reply containing the number of seconds left for processing this request, or an error message if the query was malformed.</span></div>
<div class="MsoNormal">
<span lang="EN-US">After that wait period is expired, we again query the server for the status of our request, and if the reply is positive we proceed to the final request results. Otherwise we wait for the request to complete, and then proceed.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Basically, our program emulates a browser accessing blast via ordinary web form.</span></div>
<div class="MsoNormal">
<span lang="EN-US">More accurately, our program creates HTTP GET query string , and sends a query to the</span></div>
<div class="MsoNormal">
<span lang="EN-US">http://www.ncbi.nlm.nih.gov/blast/Blast.cgi.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">For every URL , query must specify CMD value which indicates that we are sending a query (CMD=Put) , or we are requesting the query results (CMD=Get).</span></div>
<div class="MsoNormal">
<span lang="EN-US">There are also other CMD values (INFO and WEB), but we wont look at them now, as they are not essential for our access interface.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">In addition to the CMD=Put command, we also need to select a database using a DATABASE variable (e.g. DATABASE=nr), then specify a sequence using QUERY</span></div>
<div class="MsoNormal">
<span lang="EN-US">(e.g. QUERY=SSWWAHVEMGPPDPILGVTEAY) and select a PROGRAM using </span></div>
<div class="MsoNormal">
<span lang="EN-US">PROGRAM variable (e.g. PROGRAM=blastp).</span></div>
<div class="MsoNormal">
<span lang="EN-US">Other values are not mandatory, and if not specified, the defaults will be used.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">Jakarta Commons HttpClient library contains a class for constructing queries:</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">UrlQuery putQuery = new UrlQuery();</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">putQuery.setNameValue("CMD", "Put");</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">putQuery.setNameValue("DATABASE", "nr");</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">putQuery.setNameValue("QUERY", "SSWWAHVEMGPPDPILGVTEAY");</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">putQuery.setNameValue("PROGRAM", "blastp");</span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">After query has been specified, we continue with:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">HttpClient http = new HttpClient();</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">GetMethod getMethod = new GetMethod("http://www.ncbi.nlm.nih.gov/blast/Blast.cgi")</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">getMethod.setQueryString(putQuery);</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">And finally, we send the request:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">int statusCode = http.executeMethod(getMethod);</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Then we check for the results using:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: courier new;">
<span lang="EN-US"><span style="color: #3366ff;">if (statusCode != HttpStatus.SC_OK)</span> </span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">If all goes ok, then we extract the result to String:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span><span style="color: #3366ff; font-family: courier new;">InputStreamReader inputStream = new InputStreamReader(</span></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>getMethod.getResponseBodyAsStream(),</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>getMethod.getResponseCharSet());</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>BufferedReader buffer = new BufferedReader(inputStream);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>StringBuilder resultBuffer = new StringBuilder(2048);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>String line;</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>while ((line = buffer.readLine()) != null) {</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>result.append(line);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>}</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="color: #3366ff;"></span>Strigin result = resultBuffer.toString(); </span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">It is also important to close all opened connections:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">getMethod.releaseConnection();</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">inputStream.close();</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">The result contains HTML, and it is not the final data-set we are trying to retrieve. </span></div>
<div class="MsoNormal">
<span lang="EN-US">NCBI returned two values commented in the HTML code, the rest we ignore:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span><!-- QBlastInfoBegin </span></div>
<div class="MsoNormal">
<span lang="EN-US"> RID = 954517067-8610-1647 </span></div>
<div class="MsoNormal">
<span lang="EN-US"> RTOE = 207 </span></div>
<div class="MsoNormal">
<span lang="EN-US"> QBlastInfoEnd </span></div>
<div class="MsoNormal">
<span lang="EN-US"> --></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">RID (Request Identifier) Value is something similar to a sessionID, which lasts for 24h</span></div>
<div class="MsoNormal">
<span lang="EN-US">That is the number we will return to NCBI when we request the result, and </span></div>
<div class="MsoNormal">
<span lang="EN-US">RTOE (Request Time of Execution ) is time of seconds to wait until NCBI has finished processing our request. Now we need to extract RID and RTOE values.</span></div>
<div class="MsoNormal">
<span lang="EN-US">We will do that using regular expressions:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">public static final String RTOE_RID_PATTERN = </span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">"<!--\\s*QBlastInfoBegin\\s*RID = (.*)\\s*RTOE = (.*)\\s*QBlastInfoEnd\\s*-->";</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
</div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">Pattern pattern = Pattern.compile(RTOE_RID_PATTERN);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">Matcher matcher = pattern.matcher(result);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">String resultRID;</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">String resultRTOE;</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">if (matcher.find()) {</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>if (matcher.groupCount() != 2) {</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span><span style="font-size: 0pt;"></span><span style="font-size: 0pt;"></span>return false;</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>}</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">}</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">resultRID = matcher.group(1);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: arial;">
<span lang="EN-US">resultRTOE = matcher.group(2);</span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Now that we know how much longer we need to wait, we call:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span><span style="color: #3366ff; font-family: courier new;">long sleepMillis = TimeUnit.SECONDS.toMillis((long) resultRTOE);</span></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US"><span style="font-size: 0pt;"></span>Thread.sleep(sleepMillis);</span></div>
<div class="MsoNormal">
<span lang="EN-US"><span style="font-size: 0pt;"></span></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">After waiting we are making another query to check if our request has been processed:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">UrlQuery getQuery = new UrlQuery();</span></div>
<div class="MsoNormal" face="courier new" style="color: #3366ff;">
<span lang="EN-US">getQuery.setNameValue("RID", rid);</span></div>
<div class="MsoNormal" style="color: #3366ff; font-family: courier new;">
<span lang="EN-US">getQuery.setNameValue("CMD", "GET");</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">After which we get another HTML code containing something like this:</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">Now we need to retrieve the Status value which can be either WAITING, READY, UNKNOWN or ERROR.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">WAITING - we need to wait a few more seconds</span></div>
<div class="MsoNormal">
<span lang="EN-US">READY - request has been processed</span></div>
<div class="MsoNormal">
<span lang="EN-US">UNKNOWN - request has been lost</span></div>
<div class="MsoNormal">
<span lang="EN-US">ERROR - our request was malformed, we made an error during issuing our request</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">If we got Status=WAITING, then we will wait for couple of more seconds, and resend the query, and if the Status=READY<span style="font-size: 0pt;"> </span>we can create a query for retrieving the final result data in a form of an XML document.</span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US"><o:p></o:p></span></div>
<div class="MsoNormal">
<span lang="EN-US">To be continued in part II of this tutorial.</span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span lang="EN-US"><a href="http://www.blogger.com/semgen.info">SemGen</a> </span></div>Programminghttp://www.blogger.com/profile/06893571938614656263noreply@blogger.com1