When accessing firebase via the Java client, I'm seeing the message "close called on closed connection". This is the exact printout I'm seeing when running the following code. It looks like the auth is working, but I'm curious where this message is coming from.
generateToken()...
generateToken() end
close called on closed connection
Succeeded!
using firebase-client-1.0.7.jar
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.security.token.TokenGenerator;
import com.firebase.security.token.TokenOptions;
import org.json.JSONObject;
public class TestInput {
public static void main(String[] args) throws Exception {
new Firebase(Constants.FIREBASE_URL).auth(generateToken(), new Firebase.AuthListener() {
public void onAuthError(FirebaseError error) {
System.out.println(" Failed! " + error.getMessage());
}
public void onAuthSuccess(Object authData) {
System.out.println("Succeeded!");
}
public void onAuthRevoked(FirebaseError firebaseError) {
System.out.println("Authentication status was cancelled! " + firebaseError.getMessage());
}
});
}
private static String generateToken() {
System.out.println("generateToken()...");
JSONObject arbitraryPayload = new JSONObject();
TokenGenerator tokenGenerator = new TokenGenerator(Constants.FIREBASE_SECRET);
TokenOptions to = new TokenOptions();
to.setAdmin(true);
to.setDebug(false);
String token = tokenGenerator.createToken(arbitraryPayload, to);
System.out.println("generateToken() end");
return token;
}
}
The message is from the Firebase SDK, if you upgrade to the latest JAR, it will not appear anymore.
Related
I'm using SignalR on ASP.NET Core 5 web server for Android device management. I can send messages from device (D2C), and receive messages with String parameters (C2D). But I can't receive messages with custom object parameters, the handler receives all object members as null. I develop an WPF client and it receives this object well.
I'm following ASP.NET Core SignalR Java client documentation. It explains how to use custom objects in Passing Class information in Java section.
In build.gradle file:
dependencies {
...
implementation 'com.microsoft.signalr:signalr:5.0.5'
implementation 'org.slf4j:slf4j-jdk14:1.7.25'
}
This is my custom class in Android project:
package com.mycompany.mayapp.signalr.models;
public class CustomClass
{
public String Param1;
public String Param2;
}
If this can help, this is my custom class in ASP.NET Core project (if I use fields instead of properties WPF client doesn't work, I don't know why):
namespace MyWebWithSignalRCore.SignalR.Models
{
public class CustomClass
{
public string Param1 { get; set; }
public string Param2 { get; set; }
}
}
And this is my android client class:
package com.mycompany.mayapp.signalr;
import android.util.Log;
import com.fagorelectronica.fagorconnectservice.signalr.models.UpdateAppParams;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.OnClosedCallback;
import com.microsoft.signalr.TypeReference;
import java.lang.reflect.Type;
public class SignalRClient
{
private static final String TAG = SignalRClient.class.getSimpleName();
HubConnection hubConnection;
public SignalRClient(String url)
{
this.hubConnection = HubConnectionBuilder.create(url).build();
this.handleIncomingMethods();
}
private void handleIncomingMethods()
{
this.hubConnection.on("ReceiveMessage", (user, message) -> { // OK
Log.d(TAG, user + ": " + message);
}, String.class, String.class);
this.hubConnection.on("Reset", () -> { // OK
Log.d(TAG, "Reset device");
});
Type customClassType = new TypeReference<CustomClass>() { }.getType();
this.hubConnection.<CustomClass>on("Custom", (params) -> { // NOK!!
Log.d(TAG, params.Param1 + ": " + params.Param2);
}, customClassType);
}
public void start()
{
this.hubConnection.start().blockingAwait();
this.hubConnection.send("Hello", "My device ID"); // OK
}
public void stop()
{
this.hubConnection.stop().blockingAwait();
}
}
This is the output I get in each handler:
D/SignalRClient: User: message
D/SignalRClient: Reset device
D/SignalRClient: null: null
Do you know what I'm doing wrong?
It seems that in the java client the custom object field names should be in lowercase. So changing the field names solves the problem.
Custom class in Android project:
package com.mycompany.mayapp.signalr.models;
public class CustomClass
{
public String param1;
public String param2;
}
Handling method:
private void handleIncomingMethods()
{
// ... other methods ...
Type customClassType = new TypeReference<CustomClass>() { }.getType();
this.hubConnection.<CustomClass>on("Custom", (params) -> { // OK!!
Log.d(TAG, params.param1 + ": " + params.param2);
}, customClassType);
}
I have two java classes communicating using a vert.x EventBus.
I have a Productor.java class:
package TP1;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
public class Productor extends AbstractVerticle
{
public void start() throws Exception
{
System.out.println("> Launching Productor...");
EventBus ebReady = vertx.eventBus();
//Send ready message
ebReady.send("canal-ready", "ready", messageReady -> {
//If Consumer received the ready message
if(messageReady.succeeded())
{
//Parse json response
JsonObject jsonObject = new JsonObject(messageReady.result().body().toString());
//Get answer value
int answerValue = Calcul.factorial(jsonObject.getInteger("param"));
String answer = Integer.toString(answerValue);
messageReady.reply(answer);//ERROR HERE
}
else
System.out.println("> No response!");
});
}
}
and a Consumer.java class:
package TP1;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
public class Consumer extends AbstractVerticle
{
public void start() throws Exception
{
System.out.println("> Launching Consumer...");
String jsonString = "{\"class\":\"Calcul\",\"method\":\"factoriel\",\"param\":5}";
JsonObject jsonObj = new JsonObject(jsonString);
EventBus ebReady = vertx.eventBus();
//Wait for ready message
ebReady.consumer("canal-ready", messageReady -> {
//Parse the ready message
String readyString = messageReady.body().toString();
//Make sure it's the ready message
if(readyString.equals("ready"))
{
//Send json back (messageReady.succeeded())
messageReady.reply(jsonObj, messageReadyReply -> {
System.out.println(messageReadyReply);
});
}
});
}
}
I can't build the Productor class but have no problem with the Consumer one.
What's wrong with the messageReady.reply(answer); part in the Productor.java class?
You were missing a call to result() (see here) before getting the message and executing methods on it. However, you're using methods that have been deprecated in the 3.8 version (example) and are missing from 4.0, so I would advise that you use the other signature instead.
I have been struggling with getting this test to work for awhile, the relevant code executes fine in production my assumption is that it has some additional configuration, lots of searching seems to be related specifically to email handling and additional libraries, I don't want to include anything else, what am I missing to link DataHandler to a relevant way of handling "text/plain" ?
Expected result: DataHandler allows me to stream the input "Value" back into a result.
Reproduce issue with this code:
import java.io.IOException;
import java.io.InputStream;
import javax.activation.CommandInfo;
import javax.activation.CommandMap;
import javax.activation.DataHandler;
import org.apache.commons.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class DataHandlerTest {
#Before
public void setUp() throws Exception {
}
#After
public void tearDown() throws Exception {
}
#Test
public void test() throws IOException {
printDefaultCommandMap();
DataHandler dh = new DataHandler("Value", "text/plain");
System.out.println("DataHandler commands:");
printDataHandlerCommands(dh);
dh.setCommandMap(CommandMap.getDefaultCommandMap());
System.out.println("DataHandler commands:");
printDataHandlerCommands(dh);
final InputStream in = dh.getInputStream();
String result = new String(IOUtils.toByteArray(in));
System.out.println("Returned String: " + result);
}
private void printDataHandlerCommands(DataHandler dh) {
CommandInfo[] infos = dh.getAllCommands();
printCommands(infos);
}
private void printDefaultCommandMap() {
CommandMap currentMap = CommandMap.getDefaultCommandMap();
String[] mimeTypes = currentMap.getMimeTypes();
System.out.println("Found " + mimeTypes.length + " MIME types.");
for (String mimeType : mimeTypes) {
System.out.println("Commands for: " + mimeType);
printCommands(currentMap.getAllCommands(mimeType));
}
}
private void printCommands(CommandInfo[] infos) {
for (CommandInfo info : infos) {
System.out.println(" Command Class: " +info.getCommandClass());
System.out.println(" Command Name: " + info.getCommandName());
}
}
}
Exception:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME
type text/plain at
javax.activation.DataHandler.getInputStream(DataHandler.java:249)
Help much appreciated, I hope this is a well formed question!
========================
Update 25th February
I have found if i know I stored a String in DataHandler, then I can cast the result to String and return the object that was stored, example:
#Test
public void testGetWithoutStream() throws IOException {
String inputString = "Value";
DataHandler dh = new DataHandler(inputString, "text/plain");
String rawResult = (String) dh.getContent();
assertEquals(inputString, rawResult);
}
But the code under test uses an InputStream, so my 'real' tests still fail when executed locally.
Continuing my investigation and still hoping for someone's assistance/guidance on this one...
Answering my own question for anyone's future reference.
All credit goes to: https://community.oracle.com/thread/1675030?start=0
The principle here is that you need to provide DataHandler a factory that contains a DataContentHandler that will behave as you would like it to for your MIME type, setting this is via a static method that seems to affect all DataHandler instances.
I declared a new class (SystemDataHandlerConfigurator), which has a single public method that creates my factory and provides it the static DataHandler.setDataContentHandlerFactory() function.
My tests now work correctly if I do this before they run:
SystemDataHandlerConfigurator configurator = new SystemDataHandlerConfigurator();
configurator.setupCustomDataContentHandlers();
SystemDataHandlerConfigurator
import java.io.IOException;
import javax.activation.*;
public class SystemDataHandlerConfigurator {
public void setupCustomDataContentHandlers() {
DataHandler.setDataContentHandlerFactory(new CustomDCHFactory());
}
private class CustomDCHFactory implements DataContentHandlerFactory {
#Override
public DataContentHandler createDataContentHandler(String mimeType) {
return new BinaryDataHandler();
}
}
private class BinaryDataHandler implements DataContentHandler {
/** Creates a new instance of BinaryDataHandler */
public BinaryDataHandler() {
}
/** This is the key, it just returns the data uninterpreted. */
public Object getContent(javax.activation.DataSource dataSource) throws java.io.IOException {
return dataSource.getInputStream();
}
public Object getTransferData(java.awt.datatransfer.DataFlavor dataFlavor,
javax.activation.DataSource dataSource)
throws java.awt.datatransfer.UnsupportedFlavorException,
java.io.IOException {
return null;
}
public java.awt.datatransfer.DataFlavor[] getTransferDataFlavors() {
return new java.awt.datatransfer.DataFlavor[0];
}
public void writeTo(Object obj, String mimeType, java.io.OutputStream outputStream)
throws java.io.IOException {
if (mimeType == "text/plain") {
byte[] stringByte = (byte[]) ((String) obj).getBytes("UTF-8");
outputStream.write(stringByte);
}
else {
throw new IOException("Unsupported Data Type: " + mimeType);
}
}
}
}
Asterisk 11.4.0
Asterisk-java: 1.0.0.CI-SNAPSHOT
I've try to run this code:
import org.asteriskjava.live.AsteriskChannel;
import org.asteriskjava.live.AsteriskQueue;
import org.asteriskjava.live.AsteriskQueueEntry;
import org.asteriskjava.live.internal.AsteriskAgentImpl;
import org.asteriskjava.live.AsteriskServer;
import org.asteriskjava.live.AsteriskServerListener;
import org.asteriskjava.live.DefaultAsteriskServer;
import org.asteriskjava.live.ManagerCommunicationException;
import org.asteriskjava.live.MeetMeRoom;
import org.asteriskjava.live.MeetMeUser;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
public class HelloLiveEverything implements AsteriskServerListener, PropertyChangeListener
{
private AsteriskServer asteriskServer;
public HelloLiveEverything()
{
asteriskServer = new DefaultAsteriskServer("localhost", "manager", "password");
}
public void run() throws ManagerCommunicationException
{
// listen for new events
asteriskServer.addAsteriskServerListener(this);
// add property change listeners to existing objects
for (AsteriskChannel asteriskChannel : asteriskServer.getChannels())
{
System.out.println(asteriskChannel);
asteriskChannel.addPropertyChangeListener(this);
}
}
public void onNewAsteriskChannel(AsteriskChannel channel)
{
System.out.println(channel);
channel.addPropertyChangeListener(this);
}
public void onNewMeetMeUser(MeetMeUser user)
{
System.out.println(user);
user.addPropertyChangeListener(this);
}
public void onNewQueueEntry(AsteriskQueueEntry user)
{
System.out.println(user);
user.addPropertyChangeListener(this);
}
public void onNewAgent(AsteriskAgentImpl user)
{
System.out.println(user);
user.addPropertyChangeListener(this);
}
public void propertyChange(PropertyChangeEvent propertyChangeEvent)
{
System.out.println(propertyChangeEvent);
}
public static void main(String[] args) throws Exception
{
HelloLiveEverything helloLiveEverything = new HelloLiveEverything();
helloLiveEverything.run();
while (true) {
}
}
}
When executed, connectios is OK. This code show me current channels but never show me new channels when callers make a calls.
I need to catch the events when new asterisk channels are opening.
What I made wrong?
Thank you
Try This:
Your Class HelloLiveEverything should implement ManagerEventListener
then override the onManagerEvent method
#Override
public void onManagerEvent(ManagerEvent event) {
String event_name = event.getClass().getSimpleName();
if (event_name.equals("DialEvent")) {
DialEvent e = (DialEvent) event;
System.out.println(e.getCallerIdNum());//caller number
System.out.println(e.getDestination());//Called number
//do something here
}
}
edit asterisk manager.conf :
[manager]
secret = password
deny=0.0.0.0/0.0.0.0
permit=209.16.236.73/255.255.255.0; change this ip with one your java app is using permit=127.0.0.1/255.255.255.0
read = system,call,log,verbose,command,agent,user,originate; add full permission
write = system,call,log,verbose,command,agent,user,originate; add full permission
I'm trying to understand how to work with Skype using java (JSkype lib)
i use example (official site):
package testproj;
import net.lamot.java.jskype.general.AbstractMessenger;
import net.lamot.java.jskype.general.MessageListenerInterface;
import net.lamot.java.jskype.windows.Messenger;
import java.lang.Thread;
import java.lang.Exception;
import java.util.Date;
public class JSkype implements MessageListenerInterface {
private AbstractMessenger msgr = null;
public JSkype() {
msgr = new Messenger();
msgr.addListener(this);
msgr.initialize();
try {
Thread.sleep(5000);
msgr.sendMessage("MESSAGE echo123 test message");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new JSkype();
}
public void onMessageReceived(String str) {
System.out.println("RESULT: "+str);
}
}
after run, in console i have many information, but for me more intresting information, that I receive after send message:
RESULT: MESSAGE 21129 STATUS SENDING
RESULT: MESSAGE 21129 STATUS SENDING
RESULT: CHAT #my.name/$echo123;9797238991f90d78 ACTIVITY_TIMESTAMP 1294574640
and now I'm trying to understand, how to determine the success of sending a message?
yep, we need parsind result string.. but what is a number 21129? 9797238991f90d78? how i can know this number before start parsing?