Subversion Repositories SmartDukaan

Rev

Blame | Last modification | View Log | RSS feed

package in.shop2020.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.ResourceBundle;

public class MyNotesGAEClient {
        public MyNotesGAEClient() {
        }

    private static ResourceBundle properties = ResourceBundle.getBundle(DataLogger.class.getName());
    private static String googleAppUrl = "http://" + properties.getString("googleappurl") + "/my-notes";
        
    public static void putNote(long userId, long entityId, String slide, String note)   {
        final Long time = (new Date()).getTime();
        //URLEncoder.encode("time", "UTF-8")
        try {
                String data = "time=" + URLEncoder.encode(time.toString(), "UTF-8") + "&user=" + URLEncoder.encode(Long.toString(userId), "UTF-8");
                        data += "&entity=" + URLEncoder.encode(Long.toString(entityId), "UTF-8") + "&slide=" + URLEncoder.encode(slide, "UTF-8");
                        data += "&note=" + URLEncoder.encode(note, "UTF-8");

            // POST data
            URL url = new URL(googleAppUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            wr.close();
            
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while (in.readLine() != null);
            in.close();
            
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                        
                } catch (IOException e) {
                        e.printStackTrace();
                }
    }
    
    public static String getNotes(long userId, long entityId)   {
                try {
                        //GET data

            URL url = new URL(googleAppUrl + "?user=" + URLEncoder.encode(Long.toString(userId), "UTF-8")
                        + "&entity=" + URLEncoder.encode(Long.toString(entityId), "UTF-8"));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            
            StringBuffer res = new StringBuffer();
            String line;
            while ((line = rd.readLine()) != null)      {
                res.append(line);
                res.append('\r');
            }
            rd.close();
            return res.toString();
            
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                        
                } catch (ProtocolException e) {
                        e.printStackTrace();
                        
                } catch (IOException e) {
                        e.printStackTrace();
                }
        return "";
    }
}