{"id":184,"date":"2007-09-10T16:29:18","date_gmt":"2007-09-10T19:29:18","guid":{"rendered":"http:\/\/www.fernandoquadro.com.br\/html\/2007\/09\/10\/recarregando-o-geoserver-catalog\/"},"modified":"2009-10-06T00:10:58","modified_gmt":"2009-10-06T03:10:58","slug":"recarregando-o-geoserver-catalog","status":"publish","type":"post","link":"https:\/\/www.fernandoquadro.com.br\/html\/2007\/09\/10\/recarregando-o-geoserver-catalog\/","title":{"rendered":"Recarregando o GeoServer Catalog"},"content":{"rendered":"<p>A alguns dias atr\u00e1s na <a href=\"http:\/\/tech.groups.yahoo.com\/group\/geoserver\" target=\"_blank\">lista GeoServer-BR<\/a>, um usu\u00e1rio precisou recarregar o cat\u00e1logo de Geoserver sem precisar reiniciar o servi\u00e7o. \u00c9 poss\u00edvel recarregar o cat\u00e1logo usando o m\u00f3dulo de LWP no Perl, sempre que uma nova layer for adicionada ao Geoserver. Se voc\u00ea tiver o Perl, o seguinte c\u00f3dio deve servir para voc\u00ea tamb\u00e9m:<\/p>\n<p>[source language=&#8221;:Java&#8221;]<br \/>\n#!\/usr\/bin\/perl<\/p>\n<p>use HTTP::Request::Common;<br \/>\nuse HTTP::Cookies;<br \/>\nuse LWP 5.64;<br \/>\nuse strict;<\/p>\n<p>reload_geoserver_catalog();<\/p>\n<p>sub reload_geoserver_catalog {<br \/>\n        my $browser = LWP::UserAgent-&gt;new;<br \/>\n        my $cookie_jar = HTTP::Cookies-&gt;new;<\/p>\n<p>        #Perform geoserver authentication:<br \/>\n        my $url =<br \/>\n        &#8216;http:\/\/localhost:8080\/geoserver\/admin\/loginSubmit.do&#8217;;<br \/>\n        my $response = $browser-&gt;request(POST $url,<br \/>\n                [&#8216;username&#8217; =&gt; &#8216;someusername&#8217;,<br \/>\n                 &#8216;password&#8217; =&gt; &#8216;somepassword&#8217;,<br \/>\n                 &#8216;submit&#8217; =&gt; &#8216;Submit&#8217;]);<\/p>\n<p>        die &quot;Error: &quot;, $response-&gt;header(&#8216;WWW-Authenticate&#8217;) ||<br \/>\n        &#8216;Error accessing&#8217;,<br \/>\n        # (&#8216;WWW-Authenticate&#8217; is the realm-name)<br \/>\n        &quot;\\n &quot;, $response-&gt;status_line, &quot;\\n at $url\\n Aborting&quot;<br \/>\n        unless $response-&gt;is_success;<\/p>\n<p>        $cookie_jar-&gt;extract_cookies($response);<\/p>\n<p>        #Load the new catalog:<br \/>\n        $url =<br \/>\n        &#8216;http:\/\/localhost:8080\/geoserver\/admin\/loadFromXML.do&#8217;;<br \/>\n        my $req = new HTTP::Request POST =&gt; $url;<br \/>\n        $cookie_jar-&gt;add_cookie_header($req);<br \/>\n        $response = $browser-&gt;request($req);<\/p>\n<p>        #Finally, logout:<br \/>\n        $url =<br \/>\n        &#8216;http:\/\/localhost:8080\/geoserver\/admin\/logout.do&#8217;;<br \/>\n        $req = new HTTP::Request POST =&gt; $url;<br \/>\n        $cookie_jar-&gt;add_cookie_header($req);<br \/>\n        $response = $browser-&gt;request($req);<br \/>\n}<br \/>\n[\/source]<\/p>\n<p>Se voc\u00ea estiver usando Linux, voc\u00ea poderia tamb\u00e9m usar o comando <strong>curl<\/strong>, que pode realizar o mesmo objetivo. Veja como utilizar o curl, <a href=\"http:\/\/curl.planetmirror.com\/docs\/httpscripting.html\" target=\"_blank\">neste link<\/a>.<br \/>\nPara utilizar Java, segue o c\u00f3digo bastando apenas alterar o username\/password:<\/p>\n<p>[source language=&#8221;:Java&#8221;]<br \/>\n\/\/ do login<br \/>\n       URL url=new URL(&quot;http:\/\/localhost:8080\/geoserver\/admin\/loginSubmit.do?username=goeserverUser&amp;password=geoserverPassword&amp;submit=Submit&quot;);<br \/>\n       URLConnection conn = url.openConnection();<br \/>\n       InputStream inStream = conn.getInputStream();<br \/>\n       String responseString=new String();<br \/>\n       BufferedReader in = new BufferedReader(new InputStreamReader(inStream));<br \/>\n       while(in.ready())<br \/>\n       {   responseString+= in.readLine();  }<br \/>\n       System.out.println(&quot;&#8212;&#8212;&#8212;\\nresponseString:&quot;+responseString);<\/p>\n<p>        \/\/ reload<br \/>\n       String cookie=conn.getHeaderField(&quot;Set-Cookie&quot;);<br \/>\n       System.out.println(&quot;cookie-text:&quot;+cookie);<br \/>\n       cookie = cookie.substring(0, cookie.indexOf(&quot;;&quot;));<br \/>\n       String cookieName = cookie.substring(0, cookie.indexOf(&quot;=&quot;));<br \/>\n       String cookieValue = cookie.substring(cookie.indexOf(&quot;=&quot;) + 1, cookie.length());<br \/>\n       String cookieString=cookieName+&quot;=&quot;+cookieValue;<br \/>\n       URL url2=new URL(&quot;http:\/\/localhost:8080\/geoserver\/admin\/loadFromXML.do&quot;);<br \/>\n       URLConnection conn2 = url2.openConnection();<br \/>\n       conn2.setRequestProperty(&quot;Cookie&quot;,cookieString);<br \/>\n       \/\/ set the Cookie for request<br \/>\n       conn2.connect();<br \/>\n       inStream = conn2.getInputStream();<br \/>\n       in = new BufferedReader(new InputStreamReader(inStream));<br \/>\n       responseString=new String();<br \/>\n       while(in.ready())<br \/>\n       {   responseString+= in.readLine();  }<br \/>\n       System.out.println(&quot;&#8212;&#8212;&#8212;&#8211;\\nresponseString:&quot;+responseString);<\/p>\n<p>        \/\/logout<br \/>\n       URL url3=new URL(&quot;http:\/\/localhost:8080\/geoserver\/admin\/logout.do&quot;);<br \/>\n       URLConnection conn3 = url3.openConnection();<br \/>\n       conn3.setRequestProperty(&quot;Cookie&quot;,cookieString);<br \/>\n       conn3.connect();<br \/>\n        inStream = conn3.getInputStream();<br \/>\n       in = new BufferedReader(new InputStreamReader(inStream));<br \/>\n       responseString=new String();<br \/>\n       while(in.ready())<br \/>\n       {   responseString+= in.readLine();  }<br \/>\n      System.out.println(&quot;&#8212;&#8212;-\\nresponseString:&quot;+responseString);<br \/>\n[\/source]<\/p>\n<p>O c\u00f3digo seguinte \u00e9 em PHP, ele permite cadastrar uma FeatureType nova e ent\u00e3o recarregar o cat\u00e1logo. Para isso voc\u00ea necessitar\u00e1 incluir a classe HttpClient.class.php (<a href=\"http:\/\/scripts.incutio.com\/httpclient\/index.php\" target=\"_blank\">http:\/\/scripts.incutio.com\/httpclient\/index.php<\/a>). Este c\u00f3digo \u00e9 uma parte de um c\u00f3digo que recebe apenas XML, cria uma tabela tempor\u00e1ria no PostGIS, insere o dado e finalmente registra o novo layer no GeoServer.<\/p>\n<p>[source language=&#8221;:php&#8221;]<br \/>\n&lt;?php<\/p>\n<p>\/\/Register the new data type<br \/>\n\/\/using geoserver configuration tool<br \/>\nrequire_once(&#8216;HttpClient.class.php&#8217;);<\/p>\n<p>\/\/Generate a ramdom featureType name (layer)<br \/>\nsrand(time());<br \/>\n$layerName = (rand()%99999999);<\/p>\n<p>\/\/Datastore from where the FeatureType<br \/>\n\/\/will be created (database in PostGIS)<br \/>\n$datastore=&quot;localhost_postgis&quot;;<\/p>\n<p>$client = new HttpClient(&#8216;localhost&#8217;, 8080);<br \/>\n$client-&gt;setDebug(false);<\/p>\n<p>\/\/Authenticate in Geoserver<br \/>\n$client-&gt;post(&#8216;\/geoserver\/admin\/loginSubmit.do&#8217;,<br \/>\n             array (<br \/>\n\t&#8216;username&#8217; =&gt;&quot;admin&quot;;,<br \/>\n\t&#8216;password&#8217; =&gt;&quot;geoserver&quot;,<br \/>\n\t&#8216;submit&#8217; =&gt; &#8216;Submit&#8217;<br \/>\n));<\/p>\n<p>$headers = $client-&gt;getHeaders();<br \/>\n\/\/Sorry, not very nice but I did not<br \/>\n\/\/manage to make it work other way&#8230;<br \/>\n$foo= explode(&quot;;&quot;,$headers[&#8216;set-cookie&#8217;]);<br \/>\n$foo= explode(&quot;=&quot;,$foo[0]);<br \/>\n$sessionArray = array(&#8216;JSESSIONID&#8217; =&gt; $foo[1]);<br \/>\n$client-&gt;setCookies($sessionArray);<\/p>\n<p>\/\/Create a new Feature Type<br \/>\n$client-&gt;post(&#8216;\/geoserver\/config\/data\/typeNewSubmit.do&#8217;,<br \/>\narray (&#8216;selectedNewFeatureType&#8217; =&gt;<br \/>\n$datastore.&#8217;:::&#8217;.$layerName));<\/p>\n<p>\/\/edit feature details<br \/>\n$client-&gt;post(&#8216;\/geov2\/config\/data\/typeEditorSubmit.do&#8217;,<br \/>\n             array (<br \/>\n\t&#8216;styleId&#8217; =&gt; &#8216;poi&#8217;,<br \/>\n\t&#8216;SRS&#8217; =&gt; &#8216;4326&#8217;,<br \/>\n\t&#8216;title&#8217; =&gt; &#8216;temporary dataset:&#8217; . $layerName,<br \/>\n\t&#8216;minX&#8217; =&gt; &#8216;-180&#8217;,<br \/>\n\t&#8216;minY&#8217; =&gt; &#8216;-90&#8217;,<br \/>\n\t&#8216;maxX&#8217; =&gt; &#8216;180&#8217;,<br \/>\n\t&#8216;maxY&#8217; =&gt; &#8217;90&#8217;,<br \/>\n\t&#8216;keywords&#8217; =&gt; &#8216; &#8216;,<br \/>\n\t&#8216;abstract&#8217; =&gt; &#8216;temporary dataset:&#8217; . $layerName,<br \/>\n\t&#8216;schemaBase&#8217; =&gt; &#8216;&#8211;&#8216;,<br \/>\n\t&#8216;action&#8217; =&gt; &#8216;Submit&#8217;<br \/>\n));<\/p>\n<p>\/\/Click Apply<br \/>\n$client-&gt;post(&#8216;\/geov2\/admin\/saveToGeoServer.do&#8217;,<br \/>\narray (&#8216;submit&#8217; =&gt; &#8216;Apply&#8217;));<\/p>\n<p>\/\/Click Save<br \/>\n$client-&gt;post(&#8216;\/geov2\/admin\/saveToXML.do&#8217;,<br \/>\narray (&#8216;submit&#8217; =&gt; &#8216;Save&#8217;));<br \/>\n?&gt;<br \/>\n[\/source]<\/p>\n<p>Fonte: <a href=\"http:\/\/docs.codehaus.org\/display\/GEOSDOC\/Alternative+for+reloading+the+Geoserver+catalog\" target=\"_blank\">GeoServer<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A alguns dias atr\u00e1s na lista GeoServer-BR, um usu\u00e1rio precisou recarregar o cat\u00e1logo de Geoserver sem precisar reiniciar o servi\u00e7o. \u00c9 poss\u00edvel recarregar o cat\u00e1logo usando o m\u00f3dulo de LWP no Perl, sempre que uma nova layer for adicionada ao&#8230; <a class=\"more-link\" href=\"https:\/\/www.fernandoquadro.com.br\/html\/2007\/09\/10\/recarregando-o-geoserver-catalog\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":275,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,24,6],"tags":[208,223],"class_list":["post-184","post","type-post","status-publish","format-standard","hentry","category-geoserver","category-gis","category-tecnologia","tag-geoserver","tag-gis"],"_links":{"self":[{"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/posts\/184","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/users\/275"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/comments?post=184"}],"version-history":[{"count":1,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/posts\/184\/revisions"}],"predecessor-version":[{"id":2992,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/posts\/184\/revisions\/2992"}],"wp:attachment":[{"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/media?parent=184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/categories?post=184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fernandoquadro.com.br\/html\/wp-json\/wp\/v2\/tags?post=184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}