/*
- * 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
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;
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;
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;
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));
}
@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);
}
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);
}
}