/** This is the entry point method. */
  public void onModuleLoad() {
    Defaults.setServiceRoot(GWT.getModuleBaseURL().replaceFirst("[a-zA-Z0-9_]+/$", ""));
    Defaults.setDispatcher(DefaultDispatcherSingleton.INSTANCE);
    GWT.log("base url for restservices: " + Defaults.getServiceRoot());

    final RideboardGinjector injector = GWT.create(RideboardGinjector.class);

    // setup display
    injector.getApplication().run();

    // Goes to the place represented on URL else default place
    injector.getPlaceHistoryHandler().handleCurrentHistory();
  }
Пример #2
0
  /** This is the entry point method. */
  public void onModuleLoad() {

    final AuthRequest req =
        new AuthRequest(GOOGLE_AUTH_URL, GOOGLE_CLIENT_ID).withScopes(PLUS_ME_SCOPE);

    // Calling login() will display a popup to the user the first
    // time it is
    // called. Once the user has granted access to the application,
    // subsequent calls to login() will not display the popup, and
    // will
    // immediately result in the callback being given the token to
    // use.
    // Auth.get().login(req, new Callback<String, Throwable>() {
    // @Override
    // public void onSuccess(String token) {
    // MaterialToast.alert("Success");
    // }
    //
    // @Override
    // public void onFailure(Throwable caught) {
    // MaterialToast.alert("onFailure");
    // }
    // });
    Defaults.setServiceRoot(GWT.getModuleBaseURL().replace("web", "rest"));

    home();
    //		register();
    //		 emailLogin();
    //		oauth2Login();
  }
Пример #3
0
 public void send(final RequestCallback callback) throws RequestException {
   doSetTimeout();
   builder.setCallback(callback);
   // lazily load dispatcher from defaults, if one is not set yet.
   Dispatcher localDispatcher = dispatcher == null ? Defaults.getDispatcher() : dispatcher;
   localDispatcher.send(this, builder);
 }
 @Override
 public JSONValue encode(Date value) throws EncodingException {
   if (value == null) {
     return getNullType();
   }
   String format = Defaults.getDateFormat();
   if (format == null) {
     return new JSONNumber(value.getTime());
   }
   return new JSONString(DateTimeFormat.getFormat(format).format(value));
 }
 @Override
 public Date decode(JSONValue value) throws DecodingException {
   if (value == null || value.isNull() != null) {
     return null;
   }
   String format = Defaults.getDateFormat();
   if (format == null) {
     JSONNumber num = value.isNumber();
     if (num == null) {
       throw new DecodingException("Expected a json number, but was given: " + value);
     }
     return new Date((long) num.doubleValue());
   }
   JSONString str = value.isString();
   if (str == null) {
     throw new DecodingException("Expected a json string, but was given: " + value);
   }
   return DateTimeFormat.getFormat(format).parse(str.stringValue());
 }
 private static JSONNull getNullType() {
   return (Defaults.doesIgnoreJsonNulls()) ? null : JSONNull.getInstance();
 }
Пример #7
0
/** @author <a href="http://hiramchirino.com">Hiram Chirino</a> */
public class Method {

  private static Logger logger = Logger.getLogger(Method.class.getName());

  /**
   * GWT hides the full spectrum of methods because safari has a bug:
   * http://bugs.webkit.org/show_bug.cgi?id=3812
   *
   * <p>We extend assume the server side will also check the X-HTTP-Method-Override header.
   *
   * <p>TODO: add an option to support using this approach to bypass restrictive firewalls even if
   * the browser does support the setting all the method types.
   *
   * @author chirino
   */
  private static class MethodRequestBuilder extends RequestBuilder {
    public MethodRequestBuilder(String method, String url) {

      super(method, url);

      setHeader("X-HTTP-Method-Override", method);
    }
  }

  RequestBuilder builder;
  final Set<Integer> expectedStatuses;

  {
    expectedStatuses = new HashSet<Integer>();
    expectedStatuses.add(200);
    expectedStatuses.add(201);
    expectedStatuses.add(204);
  };

  boolean anyStatus;

  Request request;
  Response response;
  Dispatcher dispatcher = Defaults.getDispatcher();

  protected Method() {}

  public Method(Resource resource, String method) {
    builder = new MethodRequestBuilder(method, resource.getUri());
  }

  public Method user(String user) {
    builder.setUser(user);
    return this;
  }

  public Method password(String password) {
    builder.setPassword(password);
    return this;
  }

  public Method header(String header, String value) {
    builder.setHeader(header, value);
    return this;
  }

  public Method headers(Map<String, String> headers) {
    if (headers != null) {
      for (Entry<String, String> entry : headers.entrySet()) {
        builder.setHeader(entry.getKey(), entry.getValue());
      }
    }
    return this;
  }

  private void doSetTimeout() {
    if (Defaults.getRequestTimeout() > -1) {
      builder.setTimeoutMillis(Defaults.getRequestTimeout());
    }
  }

  public Method text(String data) {
    defaultContentType(Resource.CONTENT_TYPE_TEXT);
    builder.setRequestData(data);
    return this;
  }

  public Method json(JSONValue data) {
    defaultContentType(Resource.CONTENT_TYPE_JSON);
    builder.setRequestData(data.toString());

    return this;
  }

  public Method xml(Document data) {
    defaultContentType(Resource.CONTENT_TYPE_XML);
    builder.setRequestData(data.toString());
    return this;
  }

  public Method timeout(int timeout) {
    builder.setTimeoutMillis(timeout);
    return this;
  }

  /**
   * sets the expected response status code. If the response status code does not match any of the
   * values specified then the request is considered to have failed. Defaults to accepting
   * 200,201,204. If set to -1 then any status code is considered a success.
   */
  public Method expect(int... statuses) {
    if (statuses.length == 1 && statuses[0] < 0) {
      anyStatus = true;
    } else {
      anyStatus = false;
      this.expectedStatuses.clear();
      for (int status : statuses) {
        this.expectedStatuses.add(status);
      }
    }
    return this;
  }

  public boolean isExpected(int status) {
    if (anyStatus) {
      return true;
    } else {
      return this.expectedStatuses.contains(status);
    }
  }

  public void send(final RequestCallback callback) throws RequestException {
    doSetTimeout();
    builder.setCallback(callback);
    dispatcher.send(this, builder);
  }

  public void send(final TextCallback callback) {
    defaultAcceptType(Resource.CONTENT_TYPE_TEXT);
    try {
      send(
          new AbstractRequestCallback<String>(this, callback) {
            protected String parseResult() throws Exception {
              return response.getText();
            }
          });
    } catch (Throwable e) {
      GWT.log("Received http error for: " + builder.getHTTPMethod() + " " + builder.getUrl(), e);
      callback.onFailure(this, e);
    }
  }

  public void send(final JsonCallback callback) {
    defaultAcceptType(Resource.CONTENT_TYPE_JSON);

    try {
      send(
          new AbstractRequestCallback<JSONValue>(this, callback) {
            protected JSONValue parseResult() throws Exception {
              try {
                return JSONParser.parse(response.getText());
              } catch (Throwable e) {
                throw new ResponseFormatException("Response was NOT a valid JSON document", e);
              }
            }
          });
    } catch (Throwable e) {
      GWT.log("Received http error for: " + builder.getHTTPMethod() + " " + builder.getUrl(), e);
      callback.onFailure(this, e);
    }
  }

  public void send(final XmlCallback callback) {
    defaultAcceptType(Resource.CONTENT_TYPE_XML);
    try {
      send(
          new AbstractRequestCallback<Document>(this, callback) {
            protected Document parseResult() throws Exception {
              try {
                return XMLParser.parse(response.getText());
              } catch (Throwable e) {
                throw new ResponseFormatException("Response was NOT a valid XML document", e);
              }
            }
          });
    } catch (Throwable e) {
      GWT.log("Received http error for: " + builder.getHTTPMethod() + " " + builder.getUrl(), e);
      callback.onFailure(this, e);
    }
  }

  public <T extends JavaScriptObject> void send(final OverlayCallback<T> callback) {

    defaultAcceptType(Resource.CONTENT_TYPE_JSON);
    try {
      send(
          new AbstractRequestCallback<T>(this, callback) {
            protected T parseResult() throws Exception {
              try {
                JSONValue val = JSONParser.parse(response.getText());
                if (val.isObject() != null) {
                  return (T) val.isObject().getJavaScriptObject();
                } else if (val.isArray() != null) {
                  return (T) val.isArray().getJavaScriptObject();
                } else {
                  throw new ResponseFormatException("Response was NOT a JSON object");
                }
              } catch (JSONException e) {
                throw new ResponseFormatException("Response was NOT a valid JSON document", e);
              } catch (IllegalArgumentException e) {
                throw new ResponseFormatException("Response was NOT a valid JSON document", e);
              }
            }
          });
    } catch (Throwable e) {
      GWT.log("Received http error for: " + builder.getHTTPMethod() + " " + builder.getUrl(), e);
      callback.onFailure(this, e);
    }
  }

  public Request getRequest() {
    return request;
  }

  public Response getResponse() {
    return response;
  }

  protected void defaultContentType(String type) {
    if (builder.getHeader(Resource.HEADER_CONTENT_TYPE) == null) {
      header(Resource.HEADER_CONTENT_TYPE, type);
    }
  }

  protected void defaultAcceptType(String type) {
    if (builder.getHeader(Resource.HEADER_ACCEPT) == null) {
      header(Resource.HEADER_ACCEPT, type);
    }
  }

  public Dispatcher getDispatcher() {
    return dispatcher;
  }

  public void setDispatcher(Dispatcher dispatcher) {
    this.dispatcher = dispatcher;
  }
}
Пример #8
0
 private void doSetTimeout() {
   if (Defaults.getRequestTimeout() > -1) {
     builder.setTimeoutMillis(Defaults.getRequestTimeout());
   }
 }