Esempio n. 1
0
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws IOException {
   response.setContentType("text/plain");
   PrintWriter writer = response.getWriter();
   String q = request.getParameter("q");
   if (q == null) {
     writer.println("ERROR: q has no value.");
     writer.close();
     return;
   }
   ResultBundle[] results = Map.getResults(q.toLowerCase());
   writer.println("STATUS: " + results.length + " results for the query " + q + "!");
   for (ResultBundle rb : results) {
     JSONObject j = new JSONObject();
     j.put("thumbnail", rb.getThumbnail());
     j.put("title", rb.getTitle());
     j.put("description", rb.getDescription());
     j.put("url", rb.getUrl());
     writer.println(j.toString());
   }
   writer.close();
 }
Esempio n. 2
0
  /**
   * Parses a text file of JSONs into an array of tweets
   *
   * @param textFile The textfile containing the JSONs
   */
  public parseJSON() {

    // get folder paths
    String currentDir = System.getProperty("user.dir");
    String root = currentDir;
    String textFile = root + "/tweet_input/tweets.txt";
    String outputFolderF1 = root + "/tweet_output/f1.txt";
    String outputFolderF2 = root + "/tweet_output/f2.txt";

    int numTweets = 0;
    int numGoodTweets = 0;
    hashtagEdges hashEdges = new hashtagEdges();

    // try objects
    try {
      // create a reader object for tweet's textfile and read-in first line
      BufferedReader reader = new BufferedReader(new FileReader(textFile));
      String currentJSON = reader.readLine();
      // System.out.println(textFile);

      // initate a writer object with the outputFolder name
      PrintWriter writerF1 = new PrintWriter(outputFolderF1, "UTF-8");
      PrintWriter writerF2 = new PrintWriter(outputFolderF2, "UTF-8");

      // create an array list to save parsedTweets and declare problem variables
      List<parsedTweet> tweetList = new ArrayList<parsedTweet>();
      String tweetText, tweetTime;
      JSONObject objJSON;
      String[] hashArray;
      parsedTweet tweetObj;
      float currentAverage;

      while (currentJSON != null) {
        try {
          objJSON = new JSONObject(currentJSON);
          // if the JSON has a text and time stamp, process it
          numTweets = numTweets + 1;
          if (objJSON.has("created_at") && objJSON.has("text")) {

            // get timestamp and text from JSON object
            tweetTime = objJSON.getString("created_at");
            tweetText = objJSON.getString("text");

            // create a tweet object and add it to folder
            tweetObj = new parsedTweet(tweetTime, tweetText, currentJSON);
            tweetList.add(tweetObj);

            // update hashObjet
            hashArray = tweetObj.hashtags.toArray(new String[tweetObj.hashtags.size()]);
            currentAverage = hashEdges.updateEdges(hashArray, tweetTime);

            // write correctly-formated cleen-tweet to output folder
            writerF1.println(tweetObj.formatTweet());
            numGoodTweets = numGoodTweets + 1;
            writerF2.format("%.2f%n", currentAverage);
          }

        } catch (Exception e) {
          System.out.println("Error in parseJSON - 1");
          e.printStackTrace();
        }

        // read next line (which has the next JSON object)
        currentJSON = reader.readLine();
      }
      writerF1.close();
      writerF2.close();
    } catch (Exception e) {

      System.out.println("Error in parseJSON - 2");
      e.printStackTrace();
    }
  }
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    PrintWriter out = response.getWriter();
    String id = request.getParameter("hashID");
    JSONObject result = new JSONObject();

    if (id != null && id.trim().isEmpty()) {
      response.setContentType("text/plain");
      response.setStatus(400);
      out.println("Empty hash ID");
      return;
    }

    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    String password;
    try {
      // Read the SQL password from a file
      BufferedReader reader = null;
      try {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("SQLpw.txt");
        reader = new BufferedReader(new InputStreamReader(inputStream));
        password = reader.readLine();
      } catch (NullPointerException e) {
        e.getStackTrace();
        password = "";
      }

      // create a mysql database connection
      String myDriver = "com.mysql.jdbc.Driver";
      String myUrl = "jdbc:mysql://localhost/lodstories";
      Class.forName(myDriver);
      conn = DriverManager.getConnection(myUrl, "root", password);
      st = conn.createStatement();
      rs = st.executeQuery("SELECT hash,title,author FROM hash_objects where id='" + id + "'");

      if (!rs.next()) {
        response.setContentType("text/plain");
        response.setStatus(400);
        out.println("Error retrieving hash object");
        return;
      }

      result.put("hash", rs.getString("hash"));
      result.put("title", rs.getString("title"));
      result.put("author", rs.getString("author"));
      // result.put("path", rs.getString("path"));
      // result.put("rating", rs.getInt("rating"));

      // Update the lastAccessed field
      st.executeUpdate(
          "UPDATE hash_objects SET lastAccessed=CURRENT_TIMESTAMP() WHERE id='" + id + "'");

      response.setContentType("application/json");

      response.setCharacterEncoding("UTF-8");
      out.println(result);
    } catch (ClassNotFoundException e) {
      System.err.println("Could not connect to driver!");
      System.err.println(e.getMessage());

    } catch (SQLException ex) {
      System.err.println(
          "SQLException: "
              + ex.getMessage()
              + ", SQLState: "
              + ex.getSQLState()
              + "VendorError: "
              + ex.getErrorCode());
    } catch (JSONException ex) {
      ex.printStackTrace();
    } finally {
      if (conn != null) {
        try {
          conn.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
      if (st != null) {
        try {
          st.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
      if (rs != null) {
        try {
          rs.close();
        } catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    }
  }