List of usage examples for twitter4j StatusUpdate setMedia
public void setMedia(File file)
From source file:social.controller.PostToSocial.java
@Override public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.processRequest(request, response); boolean face = false; boolean twit = false; try {//w w w . j a v a 2s.c om getSqlMethodsInstance().session = request.getSession(); Integer user_id = (Integer) getSqlMethodsInstance().session.getAttribute("UID"); String htmlString = (String) getSqlMethodsInstance().session.getAttribute("htmlString"); String isFacebook = request.getParameter("isFacebook"); String isTwitter = request.getParameter("isTwitter"); String getImageFile = request.getParameter("imageToPost"); String getFile = request.getParameter("imagePost"); String url = request.getParameter("url"); String file_image_path = AppConstants.LAYOUT_IMAGES_HOME + File.separator + getImageFile; // String file_image_path = getServletContext().getRealPath("") + "/temp/"+getImageFile; String imagePostURL = ServletUtil.getServerName(request.getServletContext()); //String imagePostURL = AppConstants.LAYOUT_IMAGES_HOME + getImageFile; if (isFacebook.equalsIgnoreCase("true")) { String accessToken = request.getParameter("accesstoken"); String posttext = request.getParameter("postText"); String title = request.getParameter("title"); String description = request.getParameter("description"); String url1 = request.getParameter("url"); facebook = new FacebookFactory().getInstance(); facebook.setOAuthAppId("592852577521569", "a87cc0c30d792fa5dd0aaef6b43994ef"); facebook.setOAuthPermissions("publish_actions, publish_pages,manage_pages"); // File file = new File(file_image_path); facebook.setOAuthAccessToken(new AccessToken(accessToken)); if (title == "") { Media media = new Media(new File(file_image_path)); PhotoUpdate update = new PhotoUpdate(media); update.message(posttext); facebook.postPhoto(update); } else { logger.info(title); PostUpdate post = new PostUpdate(posttext).picture(new URL( imagePostURL + "DownloadImage?image_type=LAYOUT_IMAGES&image_name=" + getImageFile)) .name(title).link(new URL(url1)).description(description); facebook.postFeed(post); } try { getSqlMethodsInstance().setSocialPostHistory(user_id, htmlString, false, true, getImageFile); } catch (Exception ex) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getMessage()); } } if (isTwitter.equalsIgnoreCase("true")) { try { AccessToken accTok = null; String shortUrl = ""; ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder(); twitterConfigBuilder.setDebugEnabled(true); twitterConfigBuilder.setOAuthConsumerKey("K7TJ3va8cyAeh6oN3Hia91S2o"); twitterConfigBuilder .setOAuthConsumerSecret("IWUt2aDVTHgUc8N0qI0cF1Z1dTAEQ7CSgnBymZNr3BPSmtkNHL"); twitterConfigBuilder.setOAuthAccessToken(request.getParameter("twittweraccestoken")); twitterConfigBuilder.setOAuthAccessTokenSecret(request.getParameter("twitterTokenSecret")); Twitter twitter = new TwitterFactory(twitterConfigBuilder.build()).getInstance(); String statusMessage = request.getParameter("text").replace("bit.ly/1XOkJo", ""); shortUrl = request.getParameter("shorturl"); if (shortUrl.length() > 0) { String StatusMessageWithoutUrl = statusMessage.substring(0, statusMessage.length()); if (StatusMessageWithoutUrl.length() + shortUrl.length() < 140) { statusMessage = StatusMessageWithoutUrl + " " + shortUrl; } else { int urlLength = shortUrl.length() + 1; int statusLength = 115 - urlLength; statusMessage = StatusMessageWithoutUrl.substring(0, statusLength); statusMessage = statusMessage + " " + shortUrl; } } File file = new File(file_image_path); int count = statusMessage.length(); StatusUpdate status = new StatusUpdate(statusMessage); // set the image to be uploaded here. status.setMedia(file); twitter.updateStatus(status); try { getSqlMethodsInstance().setSocialPostHistory(user_id, htmlString, false, true, getImageFile); } catch (Exception ex) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, ex.getMessage()); } } catch (TwitterException te) { PrintWriter out1 = response.getWriter(); out1.println("Twitter Exception: " + te.getMessage()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, te.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, te.getMessage()); } catch (Exception e) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getMessage()); } } } catch (FacebookException e) { Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getCause()); Logger.getLogger(PostToSocial.class.getName()).log(Level.SEVERE, null, e.getMessage()); } }
From source file:srss.core.TweetDialog.java
License:MIT License
public TweetDialog(final File screenshot) throws IOException { super(SRSS.getFrame(), ""); this.setAlwaysOnTop(true); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setSize(600, 400); this.setResizable(false); this.setLocationRelativeTo(null); this.setLayout(null); ImageIcon image = new ImageIcon(ImageIO.read(screenshot)); this.pathl = new JLabel(image); JScrollPane imagePane = new JScrollPane(pathl); this.pathl.addKeyListener(this); this.pathl.addMouseListener(this); this.pathl.addMouseMotionListener(this); imagePane.setBounds(10, 10, 580, 300); this.getContentPane().add(imagePane); final JTextArea tweet = new JTextArea(1, 40); tweet.addKeyListener(this); JScrollPane scroll = new JScrollPane(tweet); scroll.setBounds(10, 320, 480, 40);//from w w w.j av a 2s. c o m this.getContentPane().add(scroll); // this.button = new JButton(""); this.button.setBounds(500, 320, 80, 40); this.button.addKeyListener(this); this.button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { StatusUpdate status = new StatusUpdate(tweet.getText()); status.setMedia(screenshot); try { SRSS.getTwitterInstance().updateStatus(status); TweetDialog.this.dispose(); } catch (TwitterException e1) { e1.printStackTrace(); } } }); this.getContentPane().add(button); //------ this.setVisible(true); }