{"id":2205,"date":"2017-05-03T22:08:23","date_gmt":"2017-05-03T22:08:23","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=2205"},"modified":"2017-05-03T22:08:23","modified_gmt":"2017-05-03T22:08:23","slug":"restful-services-in-java-360-degree-of-restful-client","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/05\/03\/restful-services-in-java-360-degree-of-restful-client\/","title":{"rendered":"Restful services in Java &#8211; 360 degree of restful client"},"content":{"rendered":"<p>Always try and use the most appropriate tool for the task. \u00a0The Apache HTTP server or web server is a program for serving the html web pages to the client who requests them. Tomcat is a application server that is\u00a0actually used to for serving up Java Servlets or JSPs that are packaged up in a Web Archive file (war).<\/p>\n<p>That isn&#8217;t to say that Apache cannot serve up Java Servlets nor does it mean that Tomcat cannot serve up static html pages. \u00a0Both programs exist because each is specialized for their particular task.<\/p>\n<p>However, in order to create my web client I am going to have Tomcat serve up both the application as well as the static html web page that calls this service. \u00a0In order to do that, I will need to make a small change to my Tomcat configuration so it can also serve up these static pages.<\/p>\n<h2>Static Setup<\/h2>\n<p><span style=\"color: #000000;\">Tomcat is configured by default for Java Servlets but by adding a tiny bit of extra configuration it will then serve up the static pages. <\/span><\/p>\n<p>In the Tomcat configuration directory \/opt\/tomcat\/conf is the server.xml file which contains a lot of the configuration for Tomcat. \u00a0Simply adding the following line to the Host block will define the location for the static web pages.<\/p>\n<pre><span style=\"color: #ff0000;\"><strong>&lt;Context docBase=\"\/opt\/tomcat\/webapps\/test\" path=\"\/test\" \/&gt;<\/strong><\/span><\/pre>\n<p>The docBase attribute points to the path where the static html will be located while the path attribute is the prefix for the web page. \u00a0With the above setup, the following would be the url to access the index.html page.<\/p>\n<pre>http:\/\/localhost:8080\/test\/index.html<\/pre>\n<p>This block actually contains more interesting configuration that should probably be explained.<\/p>\n<pre> &lt;Host name=\"localhost\" appBase=\"webapps\"\r\n             unpackWARs=\"true\" autoDeploy=\"true\"&gt;\r\n\r\n    &lt;!-- SingleSignOn valve, share authentication between web applications\r\n         Documentation at: \/docs\/config\/valve.html --&gt;\r\n    &lt;!--\r\n         &lt;Valve className=\"org.apache.catalina.authenticator.SingleSignOn\" \/&gt;\r\n    --&gt;\r\n\r\n    &lt;!-- Access log processes all example.\r\n         Documentation at: \/docs\/config\/valve.html\r\n         Note: The pattern used is equivalent to using pattern=\"common\" --&gt;\r\n    &lt;Valve className=\"org.apache.catalina.valves.AccessLogValve\" directory=\"logs\"\r\n           prefix=\"localhost_access_log\" suffix=\".txt\"\r\n           pattern=\"%h %l %u %t &amp;quot;%r&amp;quot; %s %b\" \/&gt;\r\n\r\n    <span style=\"color: #ff0000;\"><strong>&lt;Context docBase=\"\/opt\/tomcat\/webapps\/test\" path=\"\/test\" \/&gt;<\/strong><\/span>\r\n &lt;\/Host&gt;<\/pre>\n<p>The autoDeploy attribute gives the flexibility of deploying a war file while Tomcat is running. \u00a0This is actually pretty neat, you don&#8217;t need to stop and restart the server when installing the new war files. \u00a0Simply copy the war file to the webapp directory.<\/p>\n<p>While doing some of my testing I simply would remove the war file and the directory that was unpacked from it. \u00a0After that I would copy over the new war file and wait for a few seconds. \u00a0At this point I could then call my restful service again. \u00a0Tomcat would unpack the new war file and then run the application.<\/p>\n<h2>Web Client<\/h2>\n<p>My client is really lightweight. \u00a0It simply queries the starting index and count of how many items should be retrieved.<\/p>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-empty.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2270\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-empty.png\" alt=\"\" width=\"842\" height=\"638\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-empty.png 842w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-empty-300x227.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-empty-768x582.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><\/p>\n<p>Simply enter the values that you are interested in and click on the &#8220;fetch data&#8221; button.<\/p>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-full.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2271\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-full.png\" alt=\"\" width=\"842\" height=\"638\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-full.png 842w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-full-300x227.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-html-full-768x582.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><\/p>\n<p>The actual html for the webpage itself is trivial. \u00a0It simply makes the restful call and then parses the return list and displays each item.<\/p>\n<p>The tricky part is not displaying the fields, gather the input or the button but actually the java script behind the webpage to make the AJAX\u00a0call. \u00a0I am not a web developer so I will just try and point out the interesting things for the AJAX call.<\/p>\n<h3>360 Web Client Code<\/h3>\n<pre>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n &lt;head&gt;\r\n &lt;meta charset=\"UTF-8\"&gt;\r\n &lt;title&gt;JavaScript &amp;amp; jQuery - Chapter 8: Ajax &amp;amp; JSON - Loading HTML&lt;\/title&gt;\r\n &lt;script src=\"js\/jquery-1.12.1.js\"&gt;&lt;\/script&gt; \r\n\r\n &lt;script&gt;\r\n function displayname() {\r\n var start = document.getElementById(\"startidx\").value;\r\n var count = document.getElementById(\"count\").value;\r\n\r\n<strong>  var xhr = new XMLHttpRequest(); \/\/ Create XMLHttpRequest object<\/strong>\r\n var myurl = \"\";\r\n myurl = 'http:\/\/localhost:8080\/ThirdRestful\/price\/fetch?start=' + start + '&amp;count=' + count;\r\n\r\n<strong>  xhr.open('GET', myurl, false); \/\/ Prepare the request<\/strong>\r\n<strong>  xhr.send(null); \/\/ Send the request<\/strong>\r\n \r\n<strong>  responseObject = JSON.parse(xhr.responseText);<\/strong>\r\n \r\n var newContent = '';\r\n newContent += '&lt;table align=\"left\"&gt;';\r\n newContent += '&lt;tr&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Id&lt;\/th&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Symbol&lt;\/th&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Bank&lt;\/th&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Price&lt;\/th&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Exchange&lt;\/th&gt;';\r\n newContent += ' &lt;th align=\"left\"&gt;Quote Timestamp&lt;\/th&gt;';\r\n newContent += '&lt;\/tr&gt;';\r\n\r\n for (i = 0; i &lt; responseObject.records.length; i++)\r\n {\r\n newContent += '&lt;tr&gt;'; \r\n newContent += '&lt;td width=\"55px\"&gt;'; newContent += responseObject.records[i].id; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;td width=\"95px\"&gt;'; newContent += responseObject.records[i].symbol; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;td width=\"95px\"&gt;'; newContent += responseObject.records[i].bank; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;td width=\"95px\"&gt;'; newContent += responseObject.records[i].price; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;td width=\"105px\"&gt;'; newContent += responseObject.records[i].exchange; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;td width=\"255px\"&gt;'; newContent += responseObject.records[i].quoteDateTime; newContent += '&lt;\/td&gt;';\r\n newContent += '&lt;\/tr&gt;'; \r\n }\r\n newContent += '&lt;\/table&gt;'; \r\n\r\n <strong>document.getElementById('content').innerHTML = newContent;<\/strong>\r\n }\r\n\r\n &lt;\/script&gt;\r\n\r\n &lt;\/head&gt;\r\n &lt;body&gt;\r\n\r\n &lt;header&gt;&lt;h1&gt;360 WebClient &lt;\/h1&gt;&lt;\/header&gt;\r\n &lt;br \/&gt;\r\n &lt;label&gt;Start Index: &lt;\/label&gt; &lt;input type= text id=\"startidx\" value=\"0\" &gt;\r\n &lt;label&gt;Count: &lt;\/label&gt; &lt;input type= text id=\"count\" value=\"10\" &gt;\r\n &lt;input id=\"fetchdata\" type=\"button\" value=\"fetch data\" onclick=\"displayname();\" \/&gt;\r\n &lt;br \/&gt;\r\n\r\n &lt;p id=\"content\"&gt;&lt;\/p&gt;\r\n &lt;br \/&gt;\r\n\r\n &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>AJAX is Asynchronous JavaScript and XML which is how you can call your, or someone else&#8217;s web services. \u00a0This is neat because you can then create a fairly complicated page but by using AJAX you can pull in other information on demand to create a more interactive web experience.<\/p>\n<p>I am pulling in all of this via the JavaScript library JQuery. \u00a0JQuery is not the only JavaScript library but it is one of the most popular ones. \u00a0It is possible to get either a compressed or uncompressed version of this library from the JQuery site.<\/p>\n<p><a href=\"https:\/\/jquery.com\/download\/\" target=\"_blank\">https:\/\/jquery.com\/download\/<\/a><\/p>\n<p>My web client allows the user to enter things that interest them and submit that as parameters to the restful web service. \u00a0The web service could itself go to the internet, read information from a database or perform some calculations &#8211; when it was finished that information could be returned to the calling client.<\/p>\n<pre> myurl = 'http:\/\/localhost:8080\/ThirdRestful\/price\/fetch?start=' + start + '&amp;count=' + count;\r\n<\/pre>\n<p>More specifically, my example queries the values from the user and builds the URI that will call my service with the parameters in their proper locations.<\/p>\n<p>This code is actually very optimistic as it assumes that the user will be smart enough to enter valid values into both fields before pressing the fetch data button. \u00a0It also assumes that data will exist.<\/p>\n<p>This last assumption is a bit of a stretch as this is not a bullet proof production program but rather a proof of concept.<\/p>\n<h2>Creating test data<\/h2>\n<p>Rather than force myself to manually enter the data each time I start my server I have created a restful call to create some data. \u00a0This is done\u00a0by simply calling the following URI.<\/p>\n<pre>http:\/\/localhost:8080\/ThirdRestful\/price\/generate<\/pre>\n<p>This will generate some somewhat random data in order to better test my client. It is also possible to call this method with a parameter to generate more than the default 20 entries. It is possible to call this method any number of times. \u00a0The newly generated data will be added to the internal list.<\/p>\n<pre>http:\/\/localhost:8080\/ThirdRestful\/price\/generate?count=10<\/pre>\n<p><a href=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-generate.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2272\" src=\"http:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-generate.png\" alt=\"\" width=\"842\" height=\"576\" srcset=\"https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-generate.png 842w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-generate-300x205.png 300w, https:\/\/blog.paranoidprofessor.com\/wp-content\/uploads\/2017\/04\/client360-generate-768x525.png 768w\" sizes=\"(max-width: 842px) 100vw, 842px\" \/><\/a><\/p>\n<p>The generate method will return the count of the number of items that exist in the list after adding the new day.<\/p>\n<p>This is my last blog article about restful web services for the moment. \u00a0This is not the only way to create such services but it is a pretty easy way without having hundreds of additional java classes that make up some frameworks.<\/p>\n<p>I think that this is a superior way of generating web services but any critics could easily point to the fact that it is not easy to know what services exist for my particular application &#8211; and they are correct.<\/p>\n<p>Another way of producing web services that allow you to see what services are available is to use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Web_Services_Description_Language\">WSDL\u00a0<\/a>. \u00a0This will describe the web services and it also allows you to generate code which can be used to call each of the defined services.<\/p>\n<p>I did have an opportunity to do just this for connecting with SharePoint. \u00a0I wasn&#8217;t really a big fan of the technology I was given. \u00a0There were hundreds of lines of auto-generated code and hundreds of files of odd framework code. \u00a0I suspect that some of the other frameworks out there (ie Spring) also have hundreds of new files but probably\u00a0in a much better organized way.<\/p>\n<p>I may end up checking out one of these other frameworks at some point, but for right now I will enjoy both the power and simplicity of the small Jersey powered restful server technology.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Always try and use the most appropriate tool for the task. \u00a0The Apache HTTP server or web server is a program for serving the html web pages to the client who requests them. Tomcat is a application server that is\u00a0actually &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2017\/05\/03\/restful-services-in-java-360-degree-of-restful-client\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,3],"tags":[12,36,88],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2205"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2205"}],"version-history":[{"count":19,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2205\/revisions"}],"predecessor-version":[{"id":3443,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2205\/revisions\/3443"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}