Exemplo n.º 1
0
  @POST
  @Path("/login")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  public Response authenticate(
      @FormDataParam("username") String userId, @FormDataParam("password") String password) {

    Session session = databaseManager.getSession();
    ResultSet user =
        session.execute("SELECT * FROM righteous.user where user_id = '" + userId + "'");

    Row row = null;
    if (user.isExhausted()) {
      session.close();
      return Response.status(200).entity("Invalid Username or Password").build();
    } else {
      row = user.one();
    }

    if (row.getString("user_id").equals(userId)
        && row.getString("user_password").equals(password)) {

      session.close();
      return Response.status(200).entity("success").build();
    } else {
      session.close();
      return Response.status(200).entity("Invalid Username or Password").build();
    }
  }
Exemplo n.º 2
0
  public java.util.LinkedList<Pic> getPicsForUser(String User) {
    java.util.LinkedList<Pic> Pics = new java.util.LinkedList<>();
    Session session = cluster.connect("instagrAndrew");
    PreparedStatement ps =
        session.prepare("select picid, hashtag, pic_added from userpiclist where user =?");
    ResultSet rs = null;
    BoundStatement boundStatement = new BoundStatement(ps);
    rs =
        session.execute( // this is where the query is executed
            boundStatement.bind( // here you are binding the 'boundStatement'
                User));
    if (rs.isExhausted()) {
      System.out.println("No Images returned");
      return null;
    } else {
      for (Row row : rs) {
        Pic pic = new Pic();
        java.util.UUID UUID = row.getUUID("picid");

        Date d = row.getDate("pic_added");
        java.sql.Timestamp tmp = new java.sql.Timestamp(d.getTime());

        pic.setUUID(UUID);
        pic.setDate(tmp);

        String ht = row.getString("hashtag");
        if (ht != null) {
          pic.setHashtag(ht);
        }
        Pics.add(pic);
      }
    }
    return Pics;
  }
Exemplo n.º 3
0
  public boolean validateUser(String username, String password) {
    AeSimpleSHA1 sha1handler = new AeSimpleSHA1();
    String EncodedPassword = null;
    try {
      EncodedPassword = sha1handler.SHA1(password);
    } catch (UnsupportedEncodingException | NoSuchAlgorithmException et) {
      System.out.println("Can't check your password");
      return false;
    }

    Session session = cluster.connect("ducquak");
    PreparedStatement pS = session.prepare("SELECT * FROM users");
    ResultSet rs = null;
    BoundStatement boundStatement = new BoundStatement(pS);
    rs =
        session.execute( // this is where the query is executed
            boundStatement // here you are binding the 'boundStatement'
            );
    if (rs.isExhausted()) {
      System.out.println("Nothing returned");
      return false;
    } else {
      for (Row row : rs) {

        String userName = row.getString("userName");
        if (userName.equals(username)) {
          String StoredPass = row.getString("password");
          if (StoredPass.compareTo(EncodedPassword) == 0) return true;
        }
      }
    }
    return false;
  }
Exemplo n.º 4
0
 /** Test simple statement inserts for all collection data types */
 public void collectionInsertTest() throws Throwable {
   ResultSet rs;
   for (String execute_string : COLLECTION_INSERT_STATEMENTS) {
     rs = session.execute(execute_string);
     assertTrue(rs.isExhausted());
   }
   assertEquals(SAMPLE_COLLECTIONS.size(), 255);
   assertEquals(COLLECTION_INSERT_STATEMENTS.size(), SAMPLE_COLLECTIONS.size());
 }
Exemplo n.º 5
0
 /** Test simple statement inserts for all primitive data types */
 public void primitiveInsertTest() throws Throwable {
   ResultSet rs;
   for (String execute_string : PRIMITIVE_INSERT_STATEMENTS) {
     rs = session.execute(execute_string);
     assertTrue(rs.isExhausted());
   }
   assertEquals(SAMPLE_DATA.size(), 15);
   assertEquals(PRIMITIVE_INSERT_STATEMENTS.size(), SAMPLE_DATA.size());
 }
  /**
   * @param resultSet
   * @param type
   * @return
   * @deprecated as of 1.5, {@link
   *     org.springframework.data.cassandra.mapping.CassandraMappingContext} handles type
   *     conversion.
   */
  @Deprecated
  public Object getSingleEntity(ResultSet resultSet, Class<?> type) {

    Object result =
        (resultSet.isExhausted() ? null : template.getConverter().read(type, resultSet.one()));

    warnIfMoreResults(resultSet);

    return result;
  }
  private void warnIfMoreResults(ResultSet resultSet) {

    if (log.isWarnEnabled() && !resultSet.isExhausted()) {
      int count = 0;

      while (resultSet.one() != null) {
        count++;
      }

      log.warn("ignoring extra {} row{}", count, count == 1 ? "" : "s");
    }
  }
Exemplo n.º 8
0
  public java.util.LinkedList<Pic> getMatchingPics(String searched) {
    java.util.LinkedList<Pic> picList = new java.util.LinkedList<Pic>();

    Session session = cluster.connect("instagrAndrew");
    PreparedStatement ps = session.prepare("select * from userpicList");
    ResultSet rs = null;
    BoundStatement boundStatement = new BoundStatement(ps);
    rs = session.execute(boundStatement);
    if (rs.isExhausted()) {
      System.out.println("No Images returned");
      return new java.util.LinkedList<Pic>();
    } else {
      for (Row row : rs) {

        String fullString = row.getString("hashtag");

        UUID uuid = row.getUUID("picId");
        String us = row.getString("user");
        Date d = row.getDate("pic_added");
        java.sql.Timestamp tmp = new java.sql.Timestamp(d.getTime());

        String[] tags;
        try {
          tags = fullString.split(",");
        } catch (Exception ex) {
          tags = null;
        }

        if (tags != null) {
          for (int i = 0; i < tags.length; i++) {

            if (tags[i].toLowerCase().equals(searched.toLowerCase())) {
              Pic toAdd = new Pic();
              toAdd.setUUID(uuid);
              toAdd.setDate(tmp);
              toAdd.setUser(us);
              toAdd.setHashtag(fullString);
              picList.add(toAdd);
              break;
            }
          }
        }
      }
    }

    return picList;
  }
Exemplo n.º 9
0
  private void initScheme(Session session, String keyspace) throws IOException {

    if (keyspace == null) {
      keyspace = AlertProperties.getProperty(ALERTS_CASSANDRA_KEYSPACE, "hawkular_alerts");
    }

    if (log.isDebugEnabled()) {
      log.debug("Checking Schema existence for keyspace: " + keyspace);
    }

    ResultSet resultSet =
        session.execute(
            "SELECT * FROM system.schema_keyspaces WHERE keyspace_name = '" + keyspace + "'");
    if (!resultSet.isExhausted()) {
      log.debug("Schema already exist. Skipping schema creation.");
      initialized = true;
      return;
    }

    log.infof("Creating Schema for keyspace %s", keyspace);

    ImmutableMap<String, String> schemaVars = ImmutableMap.of("keyspace", keyspace);

    String updatedCQL = null;
    try (InputStream inputStream =
            CassCluster.class.getResourceAsStream("/hawkular-alerts-schema.cql");
        InputStreamReader reader = new InputStreamReader(inputStream)) {
      String content = CharStreams.toString(reader);

      for (String cql : content.split("(?m)^-- #.*$")) {
        if (!cql.startsWith("--")) {
          updatedCQL = substituteVars(cql.trim(), schemaVars);
          if (log.isDebugEnabled()) {
            log.debug("Executing CQL:\n" + updatedCQL + "\n");
          }
          session.execute(updatedCQL);
        }
      }
    } catch (Exception e) {
      log.errorf("Failed schema creation: %s\nEXECUTING CQL:\n%s", e, updatedCQL);
    }
    initialized = true;

    log.infof("Done creating Schema for keyspace: " + keyspace);
  }
Exemplo n.º 10
0
  public UUID getProfilePic(String User) {
    Session session = cluster.connect("instagrAndrew");
    PreparedStatement ps = session.prepare("select profilepic from userprofiles where login =?");
    ResultSet rs = null;
    BoundStatement boundStatement = new BoundStatement(ps);
    rs =
        session.execute( // this is where the query is executed
            boundStatement.bind( // here you are binding the 'boundStatement'
                User));
    if (rs.isExhausted()) {
      System.out.println("No Images returned");
      return null;
    } else {
      for (Row row : rs) {
        java.util.UUID UUID = row.getUUID("profilepic");
        return UUID;
      }

      return null;
    }
  }
Exemplo n.º 11
0
  public Pic getPic(int image_type, java.util.UUID picid) {
    Session session = cluster.connect("instagrAndrew");
    ByteBuffer bImage = null;
    String type = null;
    int length = 0;
    String date = "";
    try {
      Convertors convertor = new Convertors();
      ResultSet rs = null;
      PreparedStatement ps = null;

      if (image_type == Convertors.DISPLAY_IMAGE) {
        ps =
            session.prepare(
                "select image,imagelength,type,interaction_time from pics where picid =?");
      } else if (image_type == Convertors.DISPLAY_THUMB) {
        ps =
            session.prepare(
                "select thumb,imagelength,thumblength,type,interaction_time from pics where picid =?");
      } else if (image_type == Convertors.DISPLAY_PROCESSED) {
        ps =
            session.prepare(
                "select processed,processedlength,type,interaction_time from pics where picid =?");
      }
      BoundStatement boundStatement = new BoundStatement(ps);
      rs =
          session.execute( // this is where the query is executed
              boundStatement.bind( // here you are binding the 'boundStatement'
                  picid));

      if (rs.isExhausted()) {
        System.out.println("No Images returned");
        return null;
      } else {
        for (Row row : rs) {
          if (image_type == Convertors.DISPLAY_IMAGE) {
            bImage = row.getBytes("image");
            length = row.getInt("imagelength");
            // date = row.getString("interaction_time");
            // hashtag = row.getString("hashtag");
          } else if (image_type == Convertors.DISPLAY_THUMB) {
            bImage = row.getBytes("thumb");
            length = row.getInt("thumblength");
            // date = row.getString("interaction_time");
            // hashtag = row.getString("hashtag");
          } else if (image_type == Convertors.DISPLAY_PROCESSED) {
            bImage = row.getBytes("processed");
            length = row.getInt("processedlength");
            // date = row.getString("interaction_time");
            // hashtag = row.getString("hashtag");
          }

          type = row.getString("type");
        }
      }
    } catch (Exception et) {
      System.out.println("Can't get Pic - " + et);
      return null;
    }
    session.close();
    Pic p = new Pic();
    p.setPic(bImage, length, type, null);

    return p;
  }
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    Cluster cluster = Cluster.builder().addContactPoint("localhost").build();

    Session session = cluster.connect();

    String vehicleId = request.getParameter("veh_id");
    String trackDate = request.getParameter("date_val");
    String queryString =
        "SELECT time, latitude, longitude FROM vehicle_tracker.location WHERE vehicle_id = '"
            + vehicleId
            + "' AND date = '"
            + trackDate
            + "' LIMIT 1";
    ResultSet result = session.execute(queryString);

    PrintWriter out = response.getWriter();

    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"><html>");
    out.println("<head><title>Track a Vehicle</title></head>");
    out.println("<body>");
    out.println("<h1>Track a Vehicle</h1>");
    out.println("Enter the track date and id of the vehicle you want to track");
    out.println("<p>&nbsp;</p>");
    out.println("<form id=\"form1\" name=\"form1\" method=\"get\" action=\"\">");
    out.println("<table>");
    out.println("<tr><td>Date (e.g. 2014-05-19):</td>");
    out.println("<td><input type=\"text\" name=\"date_val\" id=\"date_val\"/></td></tr>");
    out.println("<tr><td>Vehicle id (e.g. FLN78197):</td>");
    out.println("<td><input type=\"text\" name=\"veh_id\" id=\"veh_id\" /></td></tr>");
    out.println(
        "<tr><td></td><td><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\"/></td></tr>");
    out.println("</table>");
    out.println("</form>");
    out.println("<p>&nbsp;</p>");

    if (request.getParameter("veh_id") == null) {
      // blank
    } else if (result.isExhausted()) {
      out.println("<hr/>");
      out.println("<p>&nbsp;</p>");
      out.println(
          "Sorry, no results for vehicle id "
              + request.getParameter("veh_id")
              + " for "
              + request.getParameter("date_val"));
    } else {
      out.println("<hr/>");
      out.println("<table cellpadding=\"4\">");
      out.println(
          "<tr><td colspan=\"3\"><h2>" + request.getParameter("veh_id") + "</h2></td></tr>");
      out.println(
          "<tr><td><b>Date and Time</b></td><td><b>Latitude</b></td><td><b>Longitude</b></td></tr>");
      for (Row row : result) {
        out.println("<tr>");
        out.println("<td>" + row.getDate("time") + "</td>");
        out.println("<td>" + row.getDouble("latitude") + "</td>");
        out.println("<td>" + row.getDouble("longitude") + "</td>");
        out.println("</tr>");
      }
      out.println("</table>");
    }

    out.println("</body></html>");
  }