2 Replies Latest reply on Jan 3, 2014 9:29 PM by Diyar Bakir

    twitter update, this is my code but no update

    Diyar Bakir Newbie

      package com.sample;

      import java.io.BufferedReader;

      import java.io.DataOutputStream;

      import java.io.IOException;

      import java.io.InputStreamReader;

      import java.io.UnsupportedEncodingException;

      import java.net.MalformedURLException;

      import java.net.URL;

      import java.net.URLConnection;

      import java.net.URLEncoder;

      import java.util.Date;

      import java.util.logging.Level;

      import java.util.logging.Logger;

      import org.drools.process.instance.WorkItemHandler;

      import org.drools.runtime.process.WorkItem;

      import org.drools.runtime.process.WorkItemManager;

      import com.sun.org.apache.xml.internal.security.utils.Base64;

       

      public class HelloTwitter implements WorkItemHandler {

        private String username;

        private String pass;

        private String response;

       

        private void login(String username, String pass) {

        setCredentials(username, pass);

        }

       

        public void statusesUpdate(String status) {

        try {

        URL url = new URL("http://twitter.com/statuses/update.xml");

        status = URLEncoder.encode(status, "UTF-8");

        String parametros = "status=" + status;

        doTwitterRequest(url, parametros);

        } catch (MalformedURLException ex) {

        ex.printStackTrace();

        Logger.getLogger(HelloTwitter.class.getName()).log(Level.SEVERE,

        null, ex);

        } catch (UnsupportedEncodingException ex) {

        Logger.getLogger(HelloTwitter.class.getName()).log(Level.SEVERE,

        null, ex);

       

        ex.printStackTrace();

        }

        }

       

        private void doTwitterRequest(URL url, String parametros) {

        response = "";

       

        try {

        URLConnection conn;

        conn = url.openConnection();

        conn.setAllowUserInteraction(false);

        conn.setDoOutput(true);

        conn.setRequestProperty("Authorization", "Basic "

        + getBasicCredentials());

        conn.setRequestProperty("Content-Type",

        "application/x-www-form-urlencoded");

        DataOutputStream out = new DataOutputStream(conn.getOutputStream());

        out.writeBytes(parametros.toString());

        out.flush();

        out.close();

        BufferedReader input = new BufferedReader(new InputStreamReader(

        conn.getInputStream()));

        String l = "";

        while ((l = input.readLine()) != null) {

        response += l + "\n";

        }

        } catch (IOException ex) {

        Logger.getLogger(HelloTwitter.class.getName()).log(Level.SEVERE,

        null, ex);

        }

        }

       

        public void setCredentials(String username, String pass) {

        this.username = username;

        this.pass = pass;

        }

       

        public String getBasicCredentials() {

        byte[] credentialsBytes = (username + ":" + pass).getBytes();

        String encodedBytes = Base64.encode(credentialsBytes);

        return new String(encodedBytes);

        }

       

        public String getResponse() {

        return response;

        }

       

        public void executeWorkItem(WorkItem workitem, WorkItemManager manager) {

        System.out.println("Hola Twitter");

        login("i in twitter", "my pass");

        statusesUpdate("JBPM en Twitter " + new Date());

        System.out.println(getResponse());

        manager.completeWorkItem(workitem.getId(), null);

        }

       

        public void abortWorkItem(WorkItem workitem, WorkItemManager manaager) {

        // TODO Auto-generated method stub

        }

      }