Reset REST Cache via java code

1725
3
08-02-2010 05:54 AM
DavidLaiz
New Contributor
Hi all,

I'm looking for a way to reset the REST cache in a custom application with the following code in java, but it seems that it needs a cookie stored for the browser and the result after executing is the login page again.. if anyone knows or has done something similar please any suggestions are welcome.


      public Boolean cleanRestCache(String adminRestCacheUrl, String clearRestCacheUrl, String adminUserName, String adminPassword)
      {
            Boolean bOK = false;
          try
          {  
              // Construct data
              String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(adminUserName, "UTF-8");
              data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(adminPassword, "UTF-8");
              data += "&" + URLEncoder.encode("redirect", "UTF-8") + "=" + URLEncoder.encode(clearRestCacheUrl, "UTF-8");

              /*String data = "username=arcgismanager&password=arcgismanager&redirect=http%3A%2F%2F08cpc0301%3A8399%2Farcgis%2Frest%2Fadmin%2Fcache%2Fclear";*/
           
              // Send data
              URL url = new URL(adminRestCacheUrl);          URLConnection conn = url.openConnection();
              conn.setDoOutput(true);
              OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
              wr.write(data);
              wr.flush();

              // Get the response
              BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
              String line;
              String answer = "";
              while ((line = rd.readLine()) != null)
              {
                  answer += line;
              }
              wr.close();
              rd.close();
             
              System.out.println(answer);
           
              bOK = true;
          }
          catch(Exception ex)
          {
            System.out.println("Problems cleanning API Rest Cache");
            ex.printStackTrace();
            bOK = false;
          }
          return bOK;
      }

TIA
0 Kudos
3 Replies
RahulRavikumar
New Contributor
REST at 10.0 has a cookie-less cache clear mechanism. All you need to do is to generate a token and call the clear cache operation with that token. That way you do not need to "log-in" and generate a cookie.

For more information check out the REST API documentation. (http://help.arcgis.com/EN/arcgisserver/10.0/apis/rest/clearcache.html)
0 Kudos
Ranga_Tolapi
Occasional Contributor II
Hi david.laiz,
Any luck? Please do share the solution with me...
0 Kudos
Ranga_Tolapi
Occasional Contributor II
This java code works...

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

private static void clearRestCache(String server, String username, String password) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
         String loginUrl = "http://" + server + ":8399/arcgis/rest/admin/login";
         String cacheClearUrl = "http://" + server + ":8399/arcgis/rest/admin/cache/clear";
            HttpGet httpget = new HttpGet(loginUrl + "?username=" + username + "&password=" + password + "&redirect=" + cacheClearUrl);
            httpclient.execute(httpget);
        } catch (Exception e) {
         e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
}
0 Kudos