{"id":13023,"date":"2014-04-18T20:00:11","date_gmt":"2014-04-18T14:30:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13023"},"modified":"2014-04-19T11:15:14","modified_gmt":"2014-04-19T05:45:14","slug":"apache-http-conditional-policy-requests-for-checking-whether-resource-is-modified-or-not-and-reduce-unnecessary-processing","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/apache-http-conditional-policy-requests-for-checking-whether-resource-is-modified-or-not-and-reduce-unnecessary-processing\/","title":{"rendered":"Apache HTTP Conditional Policy Requests for checking whether resource is modified or not and reduce unnecessary processing"},"content":{"rendered":"<p>Hi,<\/p>\n<p>Before writing this blog, I would first like to make you familiar with one quote <strong><em>&#8220;Be liberal in what you accept, and conservative in what you send&#8221;<\/em><\/strong>. Yes that&#8217;s what HTTP robustness protocol states. <\/p>\n<p><strong>Scenario :<\/strong><br \/>\nWe have a use case where our server need to read XML files that are large containing 400 outer element with extremely nested elements from provider server.<\/p>\n<p><strong>Tasks Involved :<\/strong><\/p>\n<ol>\n<li>Our server will run good amount quartz scheduling job for fetching XML file of different scenario&#8217;s from a server and process them differently.<\/li>\n<li>Each job will run per minute to monitor the content, as real time content may change or not, which has to be displayed in real time to the end users.<\/li>\n<\/ol>\n<p><strong>Problem :<\/strong><br \/>\n<strong>Reading, validating and processing large XML<\/strong> every minute to make them available to end user<em><strong>&#8220;even if file content is unmodified&#8221;<\/strong><\/em>. It will make unnecessary overhead for our server and consumption of IO operation even when data is not modified.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/04\/oct-cartoon1.jpg\" alt=\"Don&#039;t know what to show on board\" width=\"340\" height=\"268\" class=\"alignleft size-full\" \/><\/p>\n<p>It&#8217;s like our server not able to find relevant data and ending up reading same data every time <strong>polluting the server environment<\/strong> with processing of large responses.<\/p>\n<p><strong>How to clean our server environment, from large unnecessary processing? Solution : HTTP conditional policy request using mod_policy<\/strong><\/p>\n<p>Yes you can make conditional request <em><strong>&#8220;If-Modified-Since&#8221;<\/strong><\/em> to provider to determine whether the file is being modified or not.<\/p>\n<p><strong>Request Flow :<\/strong><\/p>\n<ol>\n<li>Our server will make request via adding conditional header, &#8220;If-Modified-Since&#8221;, Using ApacheHTTPClient 4.3\n<pre>\r\n      apacheHTTPClientAdapter.requestObject.setHeader(\"If-Modified-Since\",\"Fri, 17 Jan 2014 19:10:11 GMT\");\r\n      \/\/Note : I have used ApacheHttpClient 4.3 to make request\r\n<\/pre>\n<p>&nbsp;<br \/>&nbsp;\n<\/li>\n<li>Or Using HTTP URL Connection :\n<pre>\r\n     URL url = new URL({file});\r\n     HttpURLConnection http = (HttpURLConnection)url.openConnection();\r\n     Http. addRequestProperty (\"If-Modified-Since\", LastUpdate);\r\n     http.connect();\r\n<\/pre>\n<\/li>\n<li>Now, provider server mod_policy filter check for the GMT lastUpdated date you have specified in &#8220;If-Modified-Since&#8221; header value of request, so :\n<ul>\n<li>If the file last-modified is greater than lastUpdated than only provider server will pass original request via responding with 200 OK with the file content to our server.\n<\/li>\n<li> <em><strong>else if the file is not modified, <\/strong><\/em>provider server will respond with response code <strong>HTTP 1.1\/304 Not Modified<\/strong> without any file content to our server.<\/li>\n<\/ul>\n<\/li>\n<li>Isn&#8217;t it beautiful, We have saved our server for such large overhead of validating and processing unnecessary file and from polluting our server environment with unnecessary processing.<\/li>\n<\/ol>\n<p>&nbsp;<br \/>\nHope this help and make you familiar with conditional request policies if you don&#8217;t know about it yet. There are many other policies like &#8220;If-Match&#8221;, &#8220;If-None-Match&#8221;, &#8220;If-Unmodified-Since&#8221;, &#8220;If-Range&#8221;. For more details refer : <a href=\"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec14.html#sec14.24\" title=\"Conditional Policy Request\" target=\"_blank\">http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec14.html#sec14.24<\/a><br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, Before writing this blog, I would first like to make you familiar with one quote &#8220;Be liberal in what you accept, and conservative in what you send&#8221;. Yes that&#8217;s what HTTP robustness protocol states. Scenario : We have a use case where our server need to read XML files that are large containing 400 [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1],"tags":[18,1389,4840,9,1157,4844,1393,1390,1392,1391],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13023"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13023"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13023\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}