boosta.artem.services.TaskQuery.java Source code

Java tutorial

Introduction

Here is the source code for boosta.artem.services.TaskQuery.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boosta.artem.services;

import boosta.artem.entities.AParserTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author artem
 */
public class TaskQuery {

    private StringBuffer query;

    private AParserTask task;

    public TaskQuery(AParserTask task) {
        this.task = task;
        this.query = new StringBuffer();
        this.query.append(this.task.getRequest());
    }

    public StringBuffer getQuery() {
        return query;
    }

    public void setQuery(StringBuffer query) {
        this.query = query;
    }

    public AParserTask getTask() {
        return task;
    }

    public void setTask(AParserTask task) {
        this.task = task;
    }

    // 
    public int addTask() throws UnsupportedEncodingException, IOException, ParseException {

        int task_id = 0;

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://62.210.82.210:9091/API");
        StringEntity requestEntity = new StringEntity(this.task.toJSON(), "UTF-8");
        requestEntity.setContentType("application/json");
        System.out.println(getStringFromInputStream(requestEntity.getContent()) + "\n");
        httpPost.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(httpPost);

        InputStream in = response.getEntity().getContent();

        String resp = getStringFromInputStream(in);
        //resp.getBytes("UTF-8");

        //resp = resp.getBytes(resp).toString();

        System.out.println(resp);

        JSONParser parser = new JSONParser();

        JSONObject json = (JSONObject) parser.parse(resp);
        //json.toJSONString();
        task_id = Integer.parseInt(json.get("data").toString());

        System.out.println(task_id);

        return task_id;
    }

    public String getStatus(int id) throws UnsupportedEncodingException, IOException, ParseException {

        String result = "{\n" + "  \"password\" : \"\",\n" + "  \"action\" : \"getTaskState\",\n"
                + "  \"data\" : {\n" + "      \"taskUid\" : \"" + String.valueOf(id) + "\"\n" + "  }\n" + "}";

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://62.210.82.210:9091/API");

        StringEntity requestEntity = new StringEntity(result);
        requestEntity.setContentType("application/json");
        httpPost.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(httpPost);

        InputStream in = response.getEntity().getContent();

        //System.out.println(getStringFromInputStream(in));

        String resp = getStringFromInputStream(in);

        JSONParser parser = new JSONParser();

        JSONObject json = (JSONObject) parser.parse(resp);
        json = (JSONObject) json.get("data");
        String status = json.get("status").toString();

        System.out.println(status);
        //return getStringFromInputStream(in);

        return status;
    }

    public int getResultCounts(int id) throws UnsupportedEncodingException, IOException, ParseException {

        String result = "{\n" + "  \"password\" : \"\",\n" + "  \"action\" : \"getTaskState\",\n"
                + "  \"data\" : {\n" + "      \"taskUid\" : \"" + String.valueOf(id) + "\"\n" + "  }\n" + "}";

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://62.210.82.210:9091/API");

        StringEntity requestEntity = new StringEntity(result);
        requestEntity.setContentType("application/json");
        httpPost.setEntity(requestEntity);
        HttpResponse response = httpClient.execute(httpPost);

        InputStream in = response.getEntity().getContent();

        //System.out.println(getStringFromInputStream(in));

        String resp = getStringFromInputStream(in);

        JSONParser parser = new JSONParser();

        JSONObject json = (JSONObject) parser.parse(resp);
        json = (JSONObject) json.get("data");
        json = (JSONObject) json.get("state");
        int counts = Integer.parseInt(json.get("resultsCount").toString());

        System.out.println(counts);
        //return getStringFromInputStream(in);

        return counts;
    }

    private static String getStringFromInputStream(InputStream is) {

        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }
}