Work in progress. Do not rely on this commit
[pithos-web-client] / src / gr / grnet / pithos / web / client / rest / HeadRequest.java
index 9b44828..1148554 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2011 GRNET S.A. All rights reserved.
+ * Copyright 2011-2012 GRNET S.A. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or
  * without modification, are permitted provided that the following
@@ -35,7 +35,8 @@
 
 package gr.grnet.pithos.web.client.rest;
 
-import gr.grnet.pithos.web.client.foldertree.Resource;
+import gr.grnet.pithos.web.client.Pithos;
+import gr.grnet.pithos.web.client.Resource;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -49,6 +50,12 @@ import com.google.gwt.http.client.Response;
 
 public abstract class HeadRequest<T extends Resource> implements ScheduledCommand {
 
+       protected static final int MAX_RETRIES = 3; 
+
+    private final Pithos app;
+
+       protected int retries = 0; 
+
        protected Class<T> aClass;
 
     private String api;
@@ -69,7 +76,8 @@ public abstract class HeadRequest<T extends Resource> implements ScheduledComman
 
     public abstract void onError(Throwable t);
 
-    public HeadRequest(Class<T> aClass, String api, String owner, String path, int okCode, T result) {
+    public HeadRequest(Pithos app, Class<T> aClass, String api, String owner, String path, int okCode, T result) {
+        this.app = app;
         this.aClass = aClass;
         this.api = api;
         this.owner = owner;
@@ -78,18 +86,21 @@ public abstract class HeadRequest<T extends Resource> implements ScheduledComman
         this.result = result;
     }
 
-    public HeadRequest(Class<T> aClass, String api, String owner, String path) {
-        this(aClass, api, owner, path, Response.SC_NO_CONTENT, null);
+    public HeadRequest(Pithos app, Class<T> aClass, String api, String owner, String path) {
+        this(app, aClass, api, owner, path, Response.SC_NO_CONTENT, null);
     }
 
-    public HeadRequest(Class<T> aClass, String api, String owner, String path, T result) {
-        this(aClass, api, owner, path, Response.SC_NO_CONTENT, result);
+    public HeadRequest(Pithos app, Class<T> aClass, String api, String owner, String path, T result) {
+        this(app, aClass, api, owner, path, Response.SC_NO_CONTENT, result);
     }
 
     @Override
     public void execute() {
+       if (path.contains("?"))
+               path += "&t=" + System.currentTimeMillis();
+       else
+               path += "?t=" + System.currentTimeMillis();
         RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, api + owner + path);
-        builder.setHeader("If-Modified-Since", "0");
         for (String header : headers.keySet()) {
             builder.setHeader(header, headers.get(header));
         }
@@ -102,11 +113,11 @@ public abstract class HeadRequest<T extends Resource> implements ScheduledComman
 
                 @Override
                 public T deserialize(Response response) {
-                    return Resource.createFromResponse(aClass, owner, response, result);
+                    return Resource.createFromResponse(app, aClass, owner, response, result);
                 }
 
                 @Override
-                public void onError(@SuppressWarnings("unused") Request request, Throwable throwable) {
+                public void onError(Request request, Throwable throwable) {
                     if (throwable instanceof RestException) {
                         if (((RestException) throwable).getHttpStatusCode() == 304 && cached != null){
                             GWT.log("Using cache: " + cached.toString(), null);
@@ -116,13 +127,21 @@ public abstract class HeadRequest<T extends Resource> implements ScheduledComman
                     }
                     HeadRequest.this.onError(throwable);
                 }
+
+                               @Override
+                               public void onUnauthorized(Response response) {
+                                       HeadRequest.this.onUnauthorized(response);
+                               }
             });
+            retries++;
         }
         catch (RequestException e) {
         }
     }
 
-    public void setHeader(String header, String value) {
+    protected abstract void onUnauthorized(Response response);
+
+       public void setHeader(String header, String value) {
         headers.put(header, value);
     }
 }