コード例 #1
0
    /**
     * Riceve la notifica quando è disponibile una nuova posizione.
     *
     * <p>Se &egrave; migliore della precedente, aggiorna il server.
     *
     * @param Location la nuova posizione rilevata
     */
    public void onLocationChanged(Location location) {
      Identity me = Identity.get();
      if (me == null) {
        stopSelf();
        return;
      }

      if (location != null && isBetterLocation(location, last)) {
        last = location;

        final Position position =
            ModelFactory.getInstance()
                .createPosition(location.getLatitude(), location.getLongitude());

        me.setPosition(position);

        try {
          (new HttpTask<Void>("POST", Setup.BACKEND_POSITION_URL, me.getId()) {

                @Override
                protected Void read(InputStream in) throws Exception {
                  return null;
                }

                @Override
                protected void write(OutputStream out) throws Exception {
                  Position.SERIALIZER.write(position, out);
                }
              })
              .call();
        } catch (Exception e) {
          Log.w("PositionTrackingService", "Http Error", e);
        }
      }
    }
コード例 #2
0
  @Override
  public void setUp() {
    try {
      helper.setUp();

      ModelFactory.setInstance(new ServerModelFactory());

      try {
        ObjectifyService.register(UserImpl.class);
        ObjectifyService.register(InterestImpl.class);
        ObjectifyService.register(C2DMConfig.class);
      } catch (IllegalArgumentException e) {
      }

      /*
       * Viene utilizzato Jetty v7.5.4 come Servlet Container
       * http://www.eclipse.org/jetty/
       */
      server = new Server(0);
      ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
      context.setContextPath("/");
      server.setHandler(context);
      context.addFilter(new FilterHolder(new FilterForTesting()), "/*", 0);
      context.addFilter(new FilterHolder(new ServletFilter()), "/*", 0);
      context.addServlet(new ServletHolder(new MockServlet()), "/*");

      server.start();
      port = server.getConnectors()[0].getLocalPort();

      // User utilizzato col solo scopo di ottenere un X-AccessToken valido per effettuare le
      // richieste
      UserImpl user1 =
          (UserImpl) ModelFactory.getInstance().createUser(12345, "Michele Piccirillo", null);
      fbAccessToken = "accessToken";
      user1.setAuthToken(fbAccessToken);
      ObjectifyService.begin().put(user1);

      Thread.sleep(
          15); // Ulizzato per assicurare che il tempo necessario per la persistenza sul Datatore
               // sia passato

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
コード例 #3
0
 /**
  * Testa che il filtro blocchi le richieste PUT verso /user/* che NON contengono tra gli header un
  * X-AccessToken
  */
 public void testAccessDeniedPutUser() {
   try {
     User user = ModelFactory.getInstance().createUser(123, "Danilo Iannelli", null);
     HttpURLConnection conn =
         (HttpURLConnection) new URL("HTTP", "127.0.0.1", port, "/user/").openConnection();
     conn.setUseCaches(false);
     conn.setDoOutput(true);
     conn.setRequestMethod("PUT");
     OutputStream out = conn.getOutputStream();
     User.SERIALIZER.write(user, out);
     out.flush();
     out.close();
     assertEquals(403, conn.getResponseCode());
   } catch (IOException e) {
     e.printStackTrace();
   } catch (TransformerException e) {
     e.printStackTrace();
   }
 }