XMPP reply from GAE to Google+ doesn't work - java

I've noticed errors in my Google App Engine Log while parsing messages. The stack trace was unhelpful to diagnose the problem, so I wrote a small message dumper inspired by Google's implementation of InboundMessageParser.
public class ChatRequestParser extends HttpRequestParser {
public static Map<String, String> parseMessage(HttpServletRequest request)
throws IOException {
try {
Map<String, String> message = new HashMap<String, String>();
MimeMultipart multipart = parseMultipartRequest(request);
int parts = multipart.getCount();
for (int i = 0; i < parts; i++) {
BodyPart part = multipart.getBodyPart(i);
String fieldName = getFieldName(part);
String fieldValue = getTextContent(part);
message.put(fieldName, fieldValue);
}
return message;
} catch (MessagingException ex) {
throw new IOException("Could not parse incoming request.", ex);
}
}
}
I found out that Google+ sends two messages for each message, only one of which contains a body (Gmail Talk client sends only one message).
Here is the first message, without a body:
{to=xxx#appspot.com, stanza=<message to="xxx#appspot.com" type="chat"
from="yyy#gmail.com/TalkGadgetD9F45A83" xmlns="jabber:client">
<cha:composing xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/>
<arc:record otr="false" xmlns:arc="http://jabber.org/protocol/archive"/>
</message>, from=yyy#gmail.com/TalkGadgetD9F45A83}
And the second one is (my payload is many asterisks, mails changed):
{to=xxx#appspot.com, body=**********************************,
stanza=<message to="xxx#appspot.com" type="chat"
id="7279D79D0.17809585028724073_:sl" from="yyy#gmail.com/TalkGadgetD9F45A83"
xmlns="jabber:client"><body>**********************************</body>
<cha:active xmlns:cha="http://jabber.org/protocol/chatstates"/>
<nos:x value="disabled" xmlns:nos="google:nosave"/><arc:record otr="false"
xmlns:arc="http://jabber.org/protocol/archive"/></message>,
from=yyy#gmail.com/TalkGadgetD9F45A83}
Since the first message doesn't have a body calling parseMessage() on XMPPService throws exception. Has anyone noticed this problem?
Now I am catching the IllegalArgumentException and throwing away meaningless messages, but the real problem is, that the reply to the valid message doesn't arrive back to Google+ client, while works perfectly with Gmail and also with my Jabber client on Linux.
I've filed issue 6467.

I can reproduce the crash when no body is set and parseMessage is called, and I'm fixing it. Thanks for finding it!
However, I can't repro the "send reply doesn't work" bug. I have code like this:
XMPPService xmpp = XMPPServiceFactory.getXMPPService();
Message message = xmpp.parseMessage(req);
Message reply = new MessageBuilder().withFromJid(message.getRecipientJids()[0])
.withRecipientJids(message.getFromJid())
.withBody("Back at you!")
.build();
xmpp.sendMessage(reply);
And I receive the reply both in Google+ and in Gmail. What are you doing differently?

Related

Azure Queue trigger not working with Java

I have a spring boot application which will publish message on azure Queue. I have one more azure queueTrigger function written in Java which will listen to the same queue to which spring boot application has published a message. The queueTrigger function not able to detected messages published on queue.
Here is my publisher code
public static void addQueueMessage(String connectStr, String queueName, String message) {
try {
// Instantiate a QueueClient which will be
// used to create and manipulate the queue
QueueClient queueClient = new QueueClientBuilder()
.connectionString(connectStr)
.queueName(queueName)
.buildClient();
System.out.println("Adding message to the queue: " + message);
// Add a message to the queue
queueClient.sendMessage(message);
} catch (QueueStorageException e) {
// Output the exception message and stack trace
System.out.println(e.getMessage());
e.printStackTrace();
}
}
Here is my queueTrigger function app code
#FunctionName("queueprocessor")
public void run(
#QueueTrigger(name = "message",
queueName = "queuetest",
connection = "AzureWebJobsStorage") String message,
final ExecutionContext context
) {
context.getLogger().info(message);
}
I'm passing same connection-String and queueName, still doesn't work. If i run function on my local machine then it gets triggered but with error error image
As the official doc suggests,
Functions expect a base64 encoded string. Any adjustments to the encoding type (in order to prepare data as a base64 encoded string) need to be implemented in the calling service.
Update sender code to send base64 encoded message.
String encodedMsg = Base64.getEncoder().encodeToString(message.getBytes())
queueClient.sendMessage(encodedMsg);

How do I send a message directly to a parking lot queue, prevent requeue and exit the program flow?

I currently have 4 queues:
test-queue
test-queue-short-term-dead-letter
test-queue-long-term-dead-letter
test-queue-parking-lot
When a message comes into test-queue, I do a check to see if the message is in the correct format. If it isn't I want to send the message directly to the parking lot queue.
I can't use AmqpRejectAndDontRequeue() because it will automatically send the message to the configured DLQ (test-queue-short-term-dead-letter).
Using RabbitTemplate.convertAndSend() with another exception such as BadRequestException doesn't work. The message goes to the parking lot queue as expected, however the same message will stay in the test-queue
Using RabbitTemplate.convertAndSend() on it's own won't work as the program continues execution.
All queues are bound to a single direct exchange, each with unique routing keys. The test-queue is configured with the following arguments:
x-dead-letter-exchange: ""
x-dead-letter-routing-key: <shortTermDeadLetterKey>
Receiver:
#RabbitListener(queues = "test-queue")
public void receiveMessage(byte[] person) {
String personString = new String(person);
if (!personString.matches(desiredRegex)) {
rabbitTemplate.convertAndSend("test-exchange", "test-queue-parking-lot",
"invalid person");
log.info("Invalid person");
}
...some other code which I dont want to run as the message has arrived in the incorrect format
}
The problem was solved by manually acknowledging the message and returning from the method.
#RabbitListener(queues = "test-queue")
public void receiveMessage(byte[] person, Channel channel,
#Header(AmqpHeaders.DELIVERY_TAG) long tag) throws Exception) {
String personString = new String(person);
if (!personString.matches(desiredRegex)) {
rabbitTemplate.convertAndSend("test-exchange", "test-queue-parking-lot",
"invalid person");
log.info("Invalid person");
channel.basicAck(tag, false);
return;
}
...some other code which I dont want to run as the message has arrived in the incorrect format
}

CloudHopper Server sending MO to connected client

I have a project where we are using ArdanStudios smppclient to connect to an SMPPServer. We can send messages to the SMPP Server and they get delivered to the handset. When the hand set replies or sends to the shortcode we do get a received message event but the message text is blank.
We are using CloudHopper internally to simulate the SMPP Server, I want to confirm that there isn't a problem on our end when receiving messages but I can not figure out a way to simulate an MO (mobile originated) message sent from Cloudhopper server to our connected ArdanClient. Any ideas ?
You have two questions in one Pal. I suggest you break it up. For the first one basically you have to override the firePduRequestReceived methos of class DefaultSmppSessionHandler:
#Override
public PduResponse firePduRequestReceived(PduRequest pduRequest) {
PduResponse response = pduRequest.createResponse();
if (pduRequest.getCommandId() == SmppConstants.CMD_ID_DELIVER_SM) {
processMO(pduRequest);
}
return response;
}
private void processsMO(PduRequest request){
DeliverSm mo = (DeliverSm) request;
int length = mo.getShortMessageLength();
Address sourceAddr = mo.getSourceAddress();
Address destAddr = mo.getDestAddress();
byte[] shortMessage = mo.getShortMessage();
String sms= new String(shortMessage);
//Do Something....
}

Mail sending is failed using java

I have used java mail API for sending mail in my application using java and web driver.My requirement is to send a mail whenever a link/url is down.Even though mail is send when i give url incorrectly ,but at the same time if a url is not loading due any other issue (page not found), found that mail is not getting send.
public void SendMail(String url,String str)
{
try
{
Sheet mailsheet = w.getSheet("mail");
String from = mailsheet.getCell(0,1).getContents().toString().trim();
String toEmailID=mailsheet.getCell(1,1).getContents().toString().trim();
Properties props = new Properties();
String mailprotocol = mailsheet.getCell(2,1).getContents().toString().trim();
String mailprotocoltype = mailsheet.getCell(3,1).getContents().toString().trim();
String mailhost = mailsheet.getCell(4,1).getContents().toString().trim();
String mailhostip = mailsheet.getCell(5,1).getContents().toString().trim();
String mailport=mailsheet.getCell(6,1).getContents().toString().trim();
String mailportid=mailsheet.getCell(7,1).getContents().toString().trim();
props.put(mailprotocol,mailprotocoltype);
props.put(mailhost,mailhostip);
props.put(mailport,mailportid);
javax.mail.Session mailSession =javax.mail.Session.getInstance(props);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmailID));
msg.setSubject("Test Summary");
msg.setContent("<html><body>Dear Admin,<br> Website page "+ "<b><i>"+url + "</b></i>"+" cannot be loaded due to the following :<br> <br></body></html>"+str,"text/html");
Transport.send(msg);
System.out.println("Mail is successfully sent to Recipient address with Error information.");
}
catch(Exception e)
{
//System.out.println(e);
System.out.println("Mail cannot be send to Recipient address due to connection error");
}
}
public void x() {
SendMail(url,driver.getTitle());
}
The answer is probably in the bit of code you don't show us : the part where you test the URL.
The response code for a wrong domain name is different from the response code due to a page not found. Also, depending on the system you're targetting, it's possible that page not found are redirected to the index page, making detection even more difficult.

Not able to receive ACLMessage in android via JADE

I'm currently using Topic based communication using JADE. I'm able to register a JADE agent using jade.core.messaging.TopicManagementFEService thereby connecting to the main-container in the same platform.
The details are below:
Main-Container: a simple LAMP/WAMP Server that hosts the Main-Container.
Client: An Android Emulator(testing purpose) to connect to the main-container.
Currently,
Server starts the main-container
Android emulator connects to the Main-container successfully (Agent created along with Topic Mgmt Service enabled)
Server is sending messages based on a specific topic.
But my Android Client is not able to receive this message although the topic registered is the same on both ends!
You can see the code below:
Server Side:
TopicManagementHelper topicHelper = (TopicManagementHelper) getHelper(TopicManagementHelper.SERVICE_NAME);
final AID sensorTopic = topicHelper.createTopic("JADE");
topicHelper.register(sensorTopic);
addBehaviour(new TickerBehaviour(this, TIMER_VALUE_IN_MILLISECONDS) {
private static final long serialVersionUID = -2567778187494378326L;
public void onTick() {
ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
msg.addReceiver(eventTopic);
msg.setContent(eventValue);
myAgent.send(msg);
}
});
Android Side:
// Registering on Android Side as well
TopicManagementHelper topicHelper = (TopicManagementHelper) getHelper(TopicManagementHelper.SERVICE_NAME);
topic = topicHelper.createTopic("JADE"); // See, same topic!
topicHelper.register(topic);
behaviour = new myBehaviour(this, TIMER_VALUE_IN_MILLISECONDS, topic);
addBehaviour(behaviour);
private class myBehaviour extends TickerBehaviour {
private static final long serialVersionUID = 4782913834042415090L;
AID topic;
Agent agent;
MessageTemplate tpl;
public myBehaviour(Agent a, long period, AID topic) {
super(a, period);
this.agent = a;
this.topic = topic;
}
public void onTick() {
tpl = MessageTemplate.MatchTopic(topic);
ACLMessage msg = receive(tpl);
if (msg != null) {
logger.log(Level.INFO, "Agent "+ agent.getLocalName() +
": Message about topic "+ topic.getLocalName() +" received. \n" +
"Content is " + msg.getContent());
data = msg.getContent();
} else {
logger.log(Level.INFO, "In here..."); // Always executes only this code!
block();
}
}
}
Where am I going wrong here? It always executes the else part in the Android side which is obvious to say that message received is NULL!
Never mind. The logic was wrong. The Android-Agent was not identifying itself to the Central-Agent.
I set the Ontology so that the Central Agent is able to identify such message and sends the message accordingly. Now, it is receiving messages!
Self-help works sometimes! ;-)
Receiving topic messages doesn't work correctly with Android up to version 4.3.0 in JADE. Android can send out topic messages but can't receive them. I found this out through my own issues. I've posted more info about it in my own question on stack overflow.
Take a look. JADE Leap Android App unable to receive topic messages

Categories