/** * This file Copyright (c) 2010-2011 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.sandbox; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.lang.StringUtils; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterFactory; import info.magnolia.cms.beans.config.URI2RepositoryManager; import info.magnolia.commands.MgnlCommand; import info.magnolia.context.Context; import info.magnolia.objectfactory.Components; /** * @author had * @version $Id:$ */ public class TweetCommand extends MgnlCommand { private String twitterPassword; private String twitterUser; private String path; private String publicBaseURL; private String tweet; private TwitterFactory factory; private URI2RepositoryManager uri2repoMan; public TweetCommand() { // initialize stateless variables like connectors here to avoid having to do it every time command gets invoked. factory = new TwitterFactory(); uri2repoMan = Components.getSingleton(URI2RepositoryManager.class); } @Override public boolean execute(Context context) throws Exception { final Twitter twitter = factory.getInstance(this.twitterUser, this.twitterPassword); // set the message if (this.tweet == null) { this.tweet = generateTweet(context); } // publish the message ... Status status = twitter.updateStatus(this.tweet); log.info("Successfully updated the status to [{}].", status.getText()); return true; } private String generateTweet(Context context) { if (StringUtils.isNotBlank(this.publicBaseURL)) { // of course you can have also variable for the repository name and have it pre-set automatically, // but just to show that you can read values from context directly should you want/need to: final String repository = (String) context.get(Context.ATTRIBUTE_REPOSITORY); // now if you really wanted to be smart here, you would read value of publicBaseURL from the public instance // and also check the activation mapping in case the content is remapped to different path during publishing final String url = this.publicBaseURL + uri2repoMan.getURI(repository, path); // construct message and run url through the tinyurl to shorten it return "Check out our new content at " + shortenUrl(url); } else { // nothing smart just publish the path to the content - pretty useless really :) return "Activated " + this.path; } } private String shortenUrl(String shorten) { final HttpClient client = new HttpClient(); final GetMethod method = new GetMethod("http://tinyurl.com/api-create.php?url=" + shorten); try { final int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("Failed to shorten url {} with tinyurl response code: {}", shorten, statusCode); return shorten; } final byte[] responseBody = method.getResponseBody(); return new String(responseBody); } catch (IOException e) { log.error("Failed to shorten url {} with exception: {}", shorten, e.getLocalizedMessage()); } finally { method.releaseConnection(); } return shorten; } @Override public void release() { super.release(); // release all variables holding state this.twitterPassword = null; this.twitterUser = null; this.path = null; } public String getTwitterPassword() { return twitterPassword; } public void setTwitterPassword(String twitterPassword) { this.twitterPassword = twitterPassword; } public String getTwitterUser() { return twitterUser; } public void setTwitterUser(String twitterUser) { this.twitterUser = twitterUser; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getPublicBaseURL() { return publicBaseURL; } public void setPublicBaseURL(String publicBaseURL) { this.publicBaseURL = publicBaseURL; } public String getTweet() { return tweet; } public void setTweet(String tweet) { this.tweet = tweet; } }