/*
 * 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 it.rest.utility;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import org.json.JSONObject;

/**
 *
 * @author paolo
 */
public class Sparql {
    private String query ;
    private String endpoint;
    
    public  Sparql(String query, String endpoint)
    {
        this.query = query;
        this.endpoint = endpoint;
    }
    
    public String returnJSON(){
        String res = "";
        try{
               URL url = new URL(endpoint);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
 
		// optional default is GET
               con.setDoOutput(true);
               con.setRequestMethod("GET");
               con.addRequestProperty("Accept", "application/json");
            String data = URLEncoder.encode("query", "UTF-8") + "=" + URLEncoder.encode(query, "UTF-8");               
               
                OutputStreamWriter osw = new OutputStreamWriter(
                con.getOutputStream());
                osw.write(data);
                osw.flush();



                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String in = "";
                StringBuffer sb = null;
                sb = new StringBuffer();

                while ((in = br.readLine()) != null) {
                    sb.append(in + "\n");
                }

                osw.close();
                br.close();   
           res = sb.toString();
         return res;
        }
        catch(Exception e){e.printStackTrace();}
        
       return res;
        
    }
    
     public String returnHTML(){
        String res = "";
        try{
               URL url = new URL(endpoint);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
 
		// optional default is GET
               con.setDoOutput(true);
               con.setRequestMethod("GET");
            String data = URLEncoder.encode("query", "UTF-8") + "=" + URLEncoder.encode(query, "UTF-8");               
               
                OutputStreamWriter osw = new OutputStreamWriter(
                con.getOutputStream());
                osw.write(data);
                osw.flush();



                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

                String in = "";
                StringBuffer sb = null;
                sb = new StringBuffer();

                while ((in = br.readLine()) != null) {
                    sb.append(in + "\n");
                }

                osw.close();
                br.close();   
           res = sb.toString();
         return res;
        }
        catch(Exception e){e.printStackTrace();}
        
       return res;
        
    }
    
}

@@ -1,189 +1,4 @@
-/*%0A * To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.%0A */%0A
 pack
@@ -236,36 +236,8 @@
 t;%0A%0A
-/**%0A *%0A * @author paolo%0A */%0A
 publ
@@ -518,35 +518,33 @@
 ry%7B%0A            
-   
+%09
 URL url = new UR
@@ -549,32 +549,36 @@
 URL(endpoint);%0A%09
+    
 %09HttpURLConnecti
@@ -623,32 +623,35 @@
 onnection();%0A %0A%09
+   
 %09// optional def
@@ -669,32 +669,33 @@
 %0A               
+%09
 con.setDoOutput(
@@ -701,38 +701,33 @@
 (true);%0A        
-    
-  
+%09
  con.setRequestM
@@ -751,24 +751,25 @@
             
+%09
 con.addReque
@@ -814,32 +814,33 @@
 %22);%0A            
+%09
 String data = UR
@@ -1107,34 +1107,32 @@
   osw.flush();%0A%0A
-%0A%0A
                 
@@ -1438,33 +1438,32 @@
               %7D%0A
-%0A
                 
@@ -1614,33 +1614,24 @@
 ckTrace();%7D%0A
-        %0A
        retur
@@ -1633,33 +1633,24 @@
 return res;%0A
-        %0A
     %7D%0A    %0A 
@@ -1734,16 +1734,17 @@
         
+%09
 URL url 
@@ -1874,24 +1874,25 @@
             
+%09
 con.setDoOut
@@ -1913,24 +1913,25 @@
             
+%09
 con.setReque
@@ -1955,24 +1955,25 @@
             
+%09
 String data 
@@ -2252,18 +2252,16 @@
 ush();%0A%0A
-%0A%0A
         

Update Sparql.java