Exemplo n.º 1
0
 @Override
 public void onError(final Request request, final Throwable exception) {
   if (exception instanceof StatusCodeException) {
     if (Storage.isSessionStorageSupported()) {
       final Storage storage = Storage.getSessionStorageIfSupported();
       if (storage.getItem(cacheKey()) != null) {
         callback.onResponseReceived(request, getOldResponse(storage));
       }
     } else if (Storage.isLocalStorageSupported()) {
       final Storage storage = Storage.getLocalStorageIfSupported();
       if (storage.getItem(cacheKey()) != null) {
         callback.onResponseReceived(request, getOldResponse(storage));
       }
     }
   } else {
     callback.onError(request, exception);
   }
 }
Exemplo n.º 2
0
  /** Sends a heartbeat to the server */
  public void send() {
    timer.cancel();

    final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);

    final RequestCallback callback =
        new RequestCallback() {

          @Override
          public void onResponseReceived(Request request, Response response) {
            int status = response.getStatusCode();

            if (status == Response.SC_OK) {
              connection.getConnectionStateHandler().heartbeatOk();
            } else {
              // Handler should stop the application if heartbeat should
              // no longer be sent
              connection.getConnectionStateHandler().heartbeatInvalidStatusCode(request, response);
            }

            schedule();
          }

          @Override
          public void onError(Request request, Throwable exception) {
            // Handler should stop the application if heartbeat should no
            // longer be sent
            connection.getConnectionStateHandler().heartbeatException(request, exception);
            schedule();
          }
        };

    rb.setCallback(callback);

    try {
      getLogger().fine("Sending heartbeat request...");
      rb.send();
    } catch (RequestException re) {
      callback.onError(null, re);
    }
  }
Exemplo n.º 3
0
 @Override
 public void onResponseReceived(final Request request, @Nonnull final Response response) {
   if (response.getStatusCode() == 304 && Storage.isSessionStorageSupported()) {
     final Storage storage = Storage.getSessionStorageIfSupported();
     if (storage.getItem(cacheKey()) != null) {
       callback.onResponseReceived(request, getOldResponse(storage));
       return;
     }
   }
   if (response.getStatusCode() == 304 && Storage.isLocalStorageSupported()) {
     final Storage storage = Storage.getLocalStorageIfSupported();
     if (storage.getItem(cacheKey()) != null) {
       callback.onResponseReceived(request, getOldResponse(storage));
       return;
     }
   }
   if (response.getStatusCode() == 200
       && response.getHeader(DataStoreService.X_VORTEX_CACHE_SCOPE) != null
       && response
           .getHeader(DataStoreService.X_VORTEX_CACHE_SCOPE)
           .equals(CachingScope.USER.name())) {
     serializedResponse = response.getText();
     if (Storage.isLocalStorageSupported()) {
       final Storage storage = Storage.getLocalStorageIfSupported();
       storage.setItem(cacheKey(), serializedResponse);
     }
   } else if (response.getStatusCode() == 200
       && response.getHeader(DataStoreService.X_VORTEX_CACHE_SCOPE) != null
       && response
           .getHeader(DataStoreService.X_VORTEX_CACHE_SCOPE)
           .equals(CachingScope.SESSION.name())) {
     serializedResponse = response.getText();
     if (Storage.isSessionStorageSupported()) {
       final Storage storage = Storage.getSessionStorageIfSupported();
       storage.setItem(cacheKey(), serializedResponse);
     }
   }
   callback.onResponseReceived(request, response);
 }
Exemplo n.º 4
0
  private void requestFile(final String fileName) {
    this.samplePageLayout.sourceCode.asWidget().setVisible(false);
    this.samplePageLayout.sourceCode.setText("");

    RequestCallback callBack =
        new RequestCallback() {

          @Override
          public void onResponseReceived(Request request, Response response) {
            if (fileName.endsWith("xml")) {
              SamplePage.this.samplePageLayout.sourceCode.setConfiguration(
                  XmlConfiguration.XML_CONFIGURATION);
            } else if (fileName.endsWith("java")) {
              SamplePage.this.samplePageLayout.sourceCode.setConfiguration(
                  JavaConfiguration.JAVA_CONFIGURATION);
            } else {
              SamplePage.this.displayError(new RuntimeException("Unknow file type"));
            }
            SamplePage.this.samplePageLayout.sourceCode.setText(response.getText());
            SamplePage.this.samplePageLayout.sourceCode.asWidget().setVisible(true);
          }

          @Override
          public void onError(Request request, Throwable exception) {
            SamplePage.this.displayError(exception);
          }
        };
    RequestBuilder builder =
        new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + "sample/" + fileName);
    builder.setCallback(callBack);
    try {
      builder.send();
    } catch (RequestException e) {
      callBack.onError(null, e);
    }
  }
  private void setPosition(
      ExtSensor positionSensor,
      final List<ExtDevice> devices,
      final int index,
      final String name,
      final int floors,
      final Polygon outline,
      final List<ExtSensor> sensors) {

    ExtDevice device = devices.get(index);
    LatLng latLng = device.<LatLng>get("latlng");

    // prepare request properties
    final Method method = RequestBuilder.POST;
    final UrlBuilder urlBuilder =
        new UrlBuilder()
            .setProtocol(CommonSenseClient.Urls.PROTOCOL)
            .setHost(CommonSenseClient.Urls.HOST);
    urlBuilder.setPath(Urls.PATH_SENSORS + "/" + positionSensor.getId() + "/data.json");
    final String url = urlBuilder.buildString();
    final String sessionId = SessionManager.getSessionId();

    String value =
        "{\\\"latitude\\\":"
            + latLng.getLatitude()
            + ",\\\"longitude\\\":"
            + latLng.getLongitude()
            + ",\\\"provider\\\":\\\"environment\\\"}";
    String body = "{\"data\":[";
    body +=
        "{\"value\":\""
            + value
            + "\",\"date\":"
            + NumberFormat.getFormat("#.#").format(System.currentTimeMillis() / 1000)
            + "}";
    body += "]}";

    // prepare request callback
    RequestCallback reqCallback =
        new RequestCallback() {

          @Override
          public void onError(Request request, Throwable exception) {
            LOG.warning("POST position onError callback: " + exception.getMessage());
            onSetPositionFailure();
          }

          @Override
          public void onResponseReceived(Request request, Response response) {
            LOG.finest("POST position response received: " + response.getStatusText());
            int statusCode = response.getStatusCode();
            if (Response.SC_CREATED == statusCode) {
              onSetPositionSuccess(
                  response.getText(), devices, index, name, floors, outline, sensors);
            } else {
              LOG.warning("POST position returned incorrect status: " + statusCode);
              onSetPositionFailure();
            }
          }
        };

    // send request
    try {
      RequestBuilder builder = new RequestBuilder(method, url);
      builder.setHeader("X-SESSION_ID", sessionId);
      builder.setHeader("Content-Type", "application/json");
      builder.sendRequest(body, reqCallback);
    } catch (Exception e) {
      LOG.warning("POST position request threw exception: " + e.getMessage());
      reqCallback.onError(null, e);
    }
  }
  private void addSensors(final ExtEnvironment environment, final List<ExtSensor> sensors) {

    if (false == sensors.isEmpty()) {

      // prepare body
      String sensorsArray = "[";
      for (ExtSensor sensor : sensors) {
        if (sensor.getAlias() == -1) {
          sensorsArray += "{\"id\":" + sensor.getId() + "},";
        }
      }
      sensorsArray = sensorsArray.substring(0, sensorsArray.length() - 1) + "]";
      String body = "{\"sensors\":" + sensorsArray + "}";

      // prepare request properties
      final Method method = RequestBuilder.POST;
      final UrlBuilder urlBuilder =
          new UrlBuilder()
              .setProtocol(CommonSenseClient.Urls.PROTOCOL)
              .setHost(CommonSenseClient.Urls.HOST);
      urlBuilder.setPath(Urls.PATH_ENVIRONMENTS + "/" + environment.getId() + "/sensors.json");
      final String url = urlBuilder.buildString();
      final String sessionId = SessionManager.getSessionId();

      // prepare request callback
      RequestCallback reqCallback =
          new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
              LOG.warning("POST environment sensors onError callback: " + exception.getMessage());
              onAddSensorsFailure(environment);
            }

            @Override
            public void onResponseReceived(Request request, Response response) {
              LOG.finest("POST environment sensors response received: " + response.getStatusText());
              int statusCode = response.getStatusCode();
              if (Response.SC_CREATED == statusCode) {
                onAddSensorSuccess(environment, sensors);
              } else {
                LOG.warning("POST environment sensors returned incorrect status: " + statusCode);
                onAddSensorsFailure(environment);
              }
            }
          };

      // send request
      try {
        RequestBuilder builder = new RequestBuilder(method, url);
        builder.setHeader("X-SESSION_ID", sessionId);
        builder.setHeader("Content-Type", "application/json");
        builder.sendRequest(body, reqCallback);
      } catch (Exception e) {
        LOG.warning("POST environment sensors request threw exception: " + e.getMessage());
        reqCallback.onError(null, e);
      }

    } else {
      onCreateComplete();
    }
  }
  private void createSensor(
      final List<ExtDevice> devices,
      final int index,
      final String name,
      final int floors,
      final Polygon outline,
      final List<ExtSensor> sensors) {

    // prepare body
    String dataStructure =
        "{\\\"latitude\\\":\\\"string\\\",\\\"longitude\\\":\\\"string\\\",\\\"altitude\\\":\\\"string\\\"}";
    String sensor = "{";
    sensor += "\"" + ExtSensor.NAME + "\":\"position\",";
    sensor += "\"" + ExtSensor.DISPLAY_NAME + "\":\"position\",";
    sensor += "\"" + ExtSensor.DESCRIPTION + "\":\"position\",";
    sensor += "\"" + ExtSensor.DATA_TYPE + "\":\"json\",";
    sensor += "\"" + ExtSensor.DATA_STRUCTURE + "\":\"" + dataStructure + "\"";
    sensor += "}";
    String body = "{\"sensor\":" + sensor + "}";

    // prepare request properties
    final Method method = RequestBuilder.POST;
    final UrlBuilder urlBuilder =
        new UrlBuilder()
            .setProtocol(CommonSenseClient.Urls.PROTOCOL)
            .setHost(CommonSenseClient.Urls.HOST);
    urlBuilder.setPath(Urls.PATH_SENSORS + ".json");
    final String url = urlBuilder.buildString();
    final String sessionId = SessionManager.getSessionId();

    // prepare request callback
    RequestCallback reqCallback =
        new RequestCallback() {

          @Override
          public void onError(Request request, Throwable exception) {
            LOG.warning("POST sensor onError callback: " + exception.getMessage());
            onCreateSensorFailure();
          }

          @Override
          public void onResponseReceived(Request request, Response response) {
            LOG.finest("POST sensor response received: " + response.getStatusText());
            int statusCode = response.getStatusCode();
            if (Response.SC_CREATED == statusCode) {
              onCreateSensorSuccess(
                  response.getText(), devices, index, name, floors, outline, sensors);
            } else {
              LOG.warning("POST sensor returned incorrect status: " + statusCode);
              onCreateSensorFailure();
            }
          }
        };

    // send request
    try {
      RequestBuilder builder = new RequestBuilder(method, url);
      builder.setHeader("X-SESSION_ID", sessionId);
      builder.setHeader("Content-Type", "application/json");
      builder.sendRequest(body, reqCallback);
    } catch (Exception e) {
      LOG.warning("POST sensor request threw exception: " + e.getMessage());
      reqCallback.onError(null, e);
    }
  }
  private void createEnvironment(
      String name, int floors, Polygon outline, final List<ExtSensor> sensors) {

    // create GPS outline String
    String gpsOutline = "";
    for (int i = 0; i < outline.getVertexCount(); i++) {
      LatLng vertex = outline.getVertex(i);
      gpsOutline += vertex.toUrlValue() + ";";
    }
    gpsOutline = gpsOutline.substring(0, gpsOutline.length() - 1);

    // create GPS position String
    String position = outline.getBounds().getCenter().toUrlValue();

    // prepare request properties
    final Method method = RequestBuilder.POST;
    final UrlBuilder urlBuilder =
        new UrlBuilder()
            .setProtocol(CommonSenseClient.Urls.PROTOCOL)
            .setHost(CommonSenseClient.Urls.HOST);
    urlBuilder.setPath(Urls.PATH_ENVIRONMENTS + ".json");
    final String url = urlBuilder.buildString();
    final String sessionId = SessionManager.getSessionId();

    String body = "{\"environment\":{";
    body += "\"" + ExtEnvironment.NAME + "\":\"" + name + "\",";
    body += "\"" + ExtEnvironment.FLOORS + "\":" + floors + ",";
    body += "\"" + ExtEnvironment.OUTLINE + "\":\"" + gpsOutline + "\",";
    body += "\"" + ExtEnvironment.POSITION + "\":\"" + position + "\"}";
    body += "}";

    // prepare request callback
    RequestCallback reqCallback =
        new RequestCallback() {

          @Override
          public void onError(Request request, Throwable exception) {
            LOG.warning("POST environments onError callback: " + exception.getMessage());
            onCreateEnvironmentFailure();
          }

          @Override
          public void onResponseReceived(Request request, Response response) {
            LOG.finest("POST environments response received: " + response.getStatusText());
            int statusCode = response.getStatusCode();
            if (Response.SC_CREATED == statusCode) {
              onCreateEnvironmentSuccess(response.getText(), sensors);
            } else {
              LOG.warning("POST environments returned incorrect status: " + statusCode);
              onCreateEnvironmentFailure();
            }
          }
        };

    // send request
    try {
      RequestBuilder builder = new RequestBuilder(method, url);
      builder.setHeader("X-SESSION_ID", sessionId);
      builder.setHeader("Content-Type", "application/json");
      builder.sendRequest(body, reqCallback);
    } catch (Exception e) {
      LOG.warning("POST environments request threw exception: " + e.getMessage());
      reqCallback.onError(null, e);
    }
  }
  private void addSensorToDevice(
      final ExtSensor sensor,
      final List<ExtDevice> devices,
      final int index,
      final String name,
      final int floors,
      final Polygon outline,
      final List<ExtSensor> sensors) {

    ExtDevice device = devices.get(index);

    // prepare body
    String body = "{\"device\":{";
    body += "\"" + ExtDevice.ID + "\":\"" + device.getId() + "\",";
    body += "\"" + ExtDevice.TYPE + "\":\"" + device.getType() + "\",";
    body += "\"" + ExtDevice.UUID + "\":\"" + device.getUuid() + "\"}";
    body += "}";

    // prepare request properties
    final Method method = RequestBuilder.POST;
    final UrlBuilder urlBuilder =
        new UrlBuilder()
            .setProtocol(CommonSenseClient.Urls.PROTOCOL)
            .setHost(CommonSenseClient.Urls.HOST);
    urlBuilder.setPath(Urls.PATH_SENSORS + "/" + sensor.getId() + "/device.json");
    final String url = urlBuilder.buildString();
    final String sessionId = SessionManager.getSessionId();

    // prepare request callback
    RequestCallback reqCallback =
        new RequestCallback() {

          @Override
          public void onError(Request request, Throwable exception) {
            LOG.warning("POST sensor device onError callback: " + exception.getMessage());
            onSensorToDeviceFailure();
          }

          @Override
          public void onResponseReceived(Request request, Response response) {
            LOG.finest("POST sensor device response received: " + response.getStatusText());
            int statusCode = response.getStatusCode();
            if (Response.SC_CREATED == statusCode) {
              onSensorToDeviceSuccess(
                  response.getText(), sensor, devices, index, name, floors, outline, sensors);
            } else {
              LOG.warning("POST sensor device returned incorrect status: " + statusCode);
              onSensorToDeviceFailure();
            }
          }
        };

    // send request
    try {
      RequestBuilder builder = new RequestBuilder(method, url);
      builder.setHeader("X-SESSION_ID", sessionId);
      builder.setHeader("Content-Type", "application/json");
      builder.sendRequest(body, reqCallback);
    } catch (Exception e) {
      LOG.warning("POST sensor device request threw exception: " + e.getMessage());
      reqCallback.onError(null, e);
    }
  }