Example usage for twitter4j StatusUpdate setPlaceId

List of usage examples for twitter4j StatusUpdate setPlaceId

Introduction

In this page you can find the example usage for twitter4j StatusUpdate setPlaceId.

Prototype

public void setPlaceId(String placeId) 

Source Link

Usage

From source file:br.com.porcelli.hornetq.integration.twitter.outgoing.impl.OutgoingTwitterHandler.java

License:Apache License

@Override
public HandleStatus handle(final MessageReference ref) {
    System.out.println("Entrando no handle()");
    synchronized (this) {
        ref.handled();//  ww w. j av  a 2 s  . co m
        final ServerMessage message = ref.getMessage();
        try {
            String text2publish;
            if (message.containsProperty(TwitterConstants.KEY_TEXT)) {
                text2publish = message.getStringProperty(TwitterConstants.KEY_TEXT);
            } else {
                text2publish = message.getBodyBuffer().readString();
            }

            if (text2publish == null || text2publish.trim().length() <= 0) {
                log.error(ERROR_MESSAGE_NOT_FILLED);
                throw new Exception(ERROR_MESSAGE_NOT_FILLED);
            } else if (text2publish.length() > 140) {
                log.warn(ERROR_MESSAGE_SIZE_EXCEED);
                text2publish = text2publish.substring(0, 139);
            }

            MessageType type = MessageType.TWEET;
            try {
                if (message.containsProperty(TwitterConstants.KEY_MSG_TYPE)) {
                    type = MessageType
                            .valueOf(message.getStringProperty(TwitterConstants.KEY_MSG_TYPE).toUpperCase());
                }
            } catch (Exception e) {
                log.warn(ERROR_INVALID_MESSAGE_TYPE);
            }

            if (type == MessageType.DM) {
                if (!message.containsProperty(TwitterConstants.KEY_TO_USER_ID)
                        && !message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) {
                    log.error(ERROR_DM_DESTINY_NOT_FOUND);
                    throw new Exception(ERROR_DM_DESTINY_NOT_FOUND);
                }

                DirectMessage sentMessage = null;
                if (message.containsProperty(TwitterConstants.KEY_TO_USER_ID)) {
                    int userId = -1;
                    try {
                        userId = message.getIntProperty(TwitterConstants.KEY_TO_USER_ID);
                    } catch (PropertyConversionException e) {
                        userId = Integer.valueOf(message.getStringProperty(TwitterConstants.KEY_TO_USER_ID));
                    }
                    sentMessage = twitter.sendDirectMessage(userId, text2publish);
                    dmSent.incrementAndGet();
                    totalSent.incrementAndGet();
                } else if (message.containsProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME)) {
                    String userScreenName = message.getStringProperty(TwitterConstants.KEY_TO_USER_SCREEN_NAME);
                    sentMessage = twitter.sendDirectMessage(userScreenName, text2publish);
                    dmSent.incrementAndGet();
                    totalSent.incrementAndGet();
                }
                if (sentMessage != null && sentQueue != null) {
                    final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage);
                    msg.setAddress(sentQueue.getName());
                    msg.setDurable(true);
                    postOffice.route(msg, false);
                }
            } else {
                final StatusUpdate status = new StatusUpdate(text2publish);

                if (message.containsProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)) {
                    long reply2StatusId = 0L;
                    try {
                        reply2StatusId = message.getLongProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID);
                    } catch (PropertyConversionException e) {
                        reply2StatusId = Long
                                .valueOf(message.getStringProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID));
                    }
                    status.setInReplyToStatusId(reply2StatusId);
                }

                if (message.containsProperty(TwitterConstants.KEY_GEO_LATITUDE)
                        && message.containsProperty(TwitterConstants.KEY_GEO_LONGITUDE)) {
                    double geolat = 0.0D;
                    double geolong = 0.0D;
                    try {
                        geolat = message.getDoubleProperty(TwitterConstants.KEY_GEO_LATITUDE);
                    } catch (PropertyConversionException e) {
                        geolat = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LATITUDE));
                    }
                    try {
                        geolong = message.getDoubleProperty(TwitterConstants.KEY_GEO_LONGITUDE);
                    } catch (PropertyConversionException e) {
                        geolong = Double.valueOf(message.getStringProperty(TwitterConstants.KEY_GEO_LONGITUDE));
                    }

                    status.setLocation(new GeoLocation(geolat, geolong));
                }

                if (message.containsProperty(TwitterConstants.KEY_PLACE_ID)) {
                    status.setPlaceId(message.getStringProperty(TwitterConstants.KEY_PLACE_ID));
                }

                if (message.containsProperty(TwitterConstants.KEY_DISPLAY_COODINATES)) {
                    boolean displayCoordinated = false;
                    try {
                        displayCoordinated = message
                                .getBooleanProperty(TwitterConstants.KEY_DISPLAY_COODINATES);
                    } catch (PropertyConversionException e) {
                        displayCoordinated = Boolean
                                .valueOf(message.getStringProperty(TwitterConstants.KEY_DISPLAY_COODINATES));
                    }
                    status.setDisplayCoordinates(displayCoordinated);
                }
                Status sentMessage = twitter.updateStatus(status);
                tweetSent.incrementAndGet();
                totalSent.incrementAndGet();
                if (sentMessage != null && sentQueue != null) {
                    final ServerMessage msg = buildMessage(sentQueue.getName().toString(), sentMessage);
                    msg.setAddress(sentQueue.getName());
                    msg.setDurable(true);
                    postOffice.route(msg, false);
                }
            }
        } catch (Exception e) {
            mbean.notifyException(e);
            log.error("Error sending message.", e);
            if (errorQueue != null) {
                final ServerMessage msg = message.copy();
                msg.setAddress(errorQueue.getName());
                msg.setDurable(true);
                try {
                    postOffice.route(msg, false);
                } catch (Exception e1) {
                    mbean.notifyException(e1);
                }
            }
        }
        try {
            queue.acknowledge(ref);
        } catch (Exception e) {
            mbean.notifyException(e);
            log.error("Error acknowledging message.", e);
        }
        return HandleStatus.HANDLED;
    }
}

From source file:org.hornetq.integration.twitter.impl.OutgoingTweetsHandler.java

License:Apache License

public HandleStatus handle(final MessageReference ref) throws Exception {
    if (filter != null && !filter.match(ref.getMessage())) {
        return HandleStatus.NO_MATCH;
    }/*from   w w  w .ja  va 2  s.  c  o  m*/

    synchronized (this) {
        ref.handled();

        ServerMessage message = ref.getMessage();

        StatusUpdate status = new StatusUpdate(message.getBodyBuffer().readString());

        // set optional property

        if (message.containsProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID)) {
            status.setInReplyToStatusId(message.getLongProperty(TwitterConstants.KEY_IN_REPLY_TO_STATUS_ID));
        }

        if (message.containsProperty(TwitterConstants.KEY_GEO_LOCATION_LATITUDE)) {
            double geolat = message.getDoubleProperty(TwitterConstants.KEY_GEO_LOCATION_LATITUDE);
            double geolong = message.getDoubleProperty(TwitterConstants.KEY_GEO_LOCATION_LONGITUDE);
            status.setLocation(new GeoLocation(geolat, geolong));
        }

        if (message.containsProperty(TwitterConstants.KEY_PLACE_ID)) {
            status.setPlaceId(message.getStringProperty(TwitterConstants.KEY_PLACE_ID));
        }

        if (message.containsProperty(TwitterConstants.KEY_DISPLAY_COODINATES)) {
            status.setDisplayCoordinates(message.getBooleanProperty(TwitterConstants.KEY_DISPLAY_COODINATES));
        }

        // send to Twitter
        try {
            this.twitter.updateStatus(status);
        } catch (TwitterException e) {
            if (e.getStatusCode() == 403) {
                // duplicated message
                HornetQTwitterLogger.LOGGER.error403(connectorName);
                queue.acknowledge(ref);

                return HandleStatus.HANDLED;
            } else {
                throw e;
            }
        }

        queue.acknowledge(ref);
        HornetQTwitterLogger.LOGGER.debug(connectorName + ": forwarded to twitter: " + message.getMessageID());
        return HandleStatus.HANDLED;
    }
}