Registering User using Smack 4.1.3 - xmpp

We are building a chat client in java and using smack 4.1.3. What I noticed is there is huge change in smack APIs after smack 4.0 and registrations examples available on the internet are not working for smack 4.1.3. I am not getting write APIs to register user. May anybody give sample codes.
Thanks in advance...

This connects ejabberd server with smack 4.1.3.
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
.setUsernameAndPassword("testuser", "pass")
.setServiceName("example.com")
.setHost("example.com")
.setResource("test")
.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled)
.setPort(5222)
.build();
SASLMechanism mechanism = new SASLDigestMD5Mechanism();
SASLAuthentication.registerSASLMechanism(mechanism);
SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
AbstractXMPPConnection con = new XMPPTCPConnection(config);
try {
con.connect();
con.login();
} catch (Exception ex) {
System.out.println(ex.getCause() + " " + ex.getMessage());
Logger.getLogger(redcore.class.getName()).log(Level.SEVERE, null, ex);
}

Do you have any problem or restriction that you are not allowed to user simple REST api's, plugins provided by openfire for registering user's, retrieving, deleting etc.
Refer here for the official docs.

Related

Embedded Jetty Server giving Same response for different urls , without restarting its not working,

I have developed the embedded jetty server to implement the rest service.
I have setup the eclipse project in the eclipse.
I have written the sample program which returns some details through rest url,
I was successfully compiled the program and created a Runnable jar.
I was successfully able to run the Jar files and the server started and running on the port which i gave ,
I have the testing url
http://localhost:1234/getuser/1
it gave me the user details in the response
<username>test1</username>
I ran the same url with different id no
http://localhost:1234/getuser/2
Again it gave me the same result,
`<username>test1</username>`
So i have restarted the server and then it got me the proper details,
<username>test2</username>
public static void main(String[] args) {
// TODO Auto-generated method stub
ServletContextHandler context = new
ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(1234);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", org.test.test.getuser.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
e.printStackTrace();
} finally{
jettyServer.destroy();
}
}
Without restarting the jetty web server how to get the proper results.
Is there any thing i need to add in the code to get it worked.
or any settings i need to do for this auto refresh?
I have found the answer, jetty server was able to refresh automatically, there was a object refresh didnt happened in the back end, resolved it from myside and it worked

Calling Watson on Bluemix from java causes Trust Manager error

Trying out some code to call a Watson Service I provisioned on Bluemix. I get the following error:
Unable to extract the trust manager on
okhttp3.internal.Platform#e19ef72e, sslSocketFactory is class
com.ibm.jsse2.SSLSocketFactoryImpl
Here is my code:
try {
DocumentConversion service = new DocumentConversion("2016-05-25");
service.setUsernameAndPassword("*****", "*****");
File doc = new File("C:/Sample1.pdf");
Answers htmlToAnswers = service.convertDocumentToAnswer(doc).execute();
System.out.println(htmlToAnswers);
} catch(Exception e) {
System.out.println("Error! -> " + e.getMessage());
}
Thoughts? Do I need to import a cert from Bluemix? Thanks.
Looks like it is a known problem in the okhttp library. Are you using Java SDK to use the IBM Watson version 3.0.0-RC1? If so, try with 2.10.0 as it has an older version of the okhttp library which may not suffer from this problem.

Smack API throws "Session establishment not offered by server" Exception

I'm trying to connect to the Vines XMPP server from Java using the Smack API. However, when I use the following connection code:
ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration("localhost", 5222);
this.connection = new XMPPConnection(connectionConfiguration);
connection.connect();
connection.login(AUCTION_LOGIN, AUCTION_PASSWORD, AUCTION_RESOURCE);
I receive the following error message:
Caused by: Session establishment not offered by server:
at org.jivesoftware.smack.SASLAuthentication.bindResourceAndEstablishSession(SASLAuthentication.java:456)
I understand the issue to be related to sessions now being deprecated from the XMPP protocol. I've been unsuccessful in finding a way to use the ConnectionConfiguration class and others to work around this problem.
You appear to be using an old, vulnerable Smack version (i.e. < 4.0.0).
The current stable Smack version does not throw such an exception, as it considers session binding to be option. It can even be completly disabled with setLegacySessionDisabled(true).

Google Admin SDK throws bad request java client

I am trying to use the admin SDK using the java client. My requirement is for a server side application to manage the users without the explicit consent of the end user. I have followd the following steps.
I have create a service account is the Google API console.
Added the service account to the third party oauth access section the Google Apps admin console
Added the scopes for user, user.readonly for the same.
Created a super admin to be used as the service account user
I am using the java client as follows:
The same action is working in the api explorer using the service email to authenticate
HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
JsonFactory JSON_FACTORY = new JacksonFactory();
GoogleCredential credential=null;
try {
credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("xxx#developer.gserviceaccount.com")
.setServiceAccountScopes(DirectoryScopes.all())
.setServiceAccountPrivateKeyFromP12File(new File("/Users/xxx/Downloads/file-privatekey.p12"))
.setServiceAccountUser("xx#subdomain.domain.com") //Super admin account
.build();
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Directory directory = new Directory.Builder(HTTP_TRANSPORT,JSON_FACTORY,credential).setApplicationName("Sync Service").build();
try {
Directory.Users.List list = directory.users().list();
list.setDomain("subdomain.domain.com");
//list.setCustomer("xxx");
Users users = list.execute();
} catch (IOException e) {
e.printStackTrace();
}
I am getting the following error. Not sure why!
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "access_denied"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at GappsClient.main(GappsClient.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Process finished with exit code 0
Have you add the client ID of your service account to Manage Third Party OAuth Access in your Cpanel?
I followed #Emily Lam answer and formed service account using old Google API console and i was able to get the expected result. Please let me know if anybody needs detailed steps i followed. Follow OAuth 2.0 client IDs part from below URL https://developers.google.com/console/help/#creatingdeletingprojects

How to receive messages in a GWT App using Kaazing Websockets <type>proxy</type> configuration?

I'm trying to build a GWT (2.4.0) application that can communicate (full-duplex) with a server using a text-based protocol. To accomplish this I'm using Kaazing Websocket Gateway Version 3.3.2 to act as a proxy.
GWT App <-----[websocket]-----> Kaazing <-----[tcp]-----> Backend Server
This is my Kaazing configuration:
<service>
<accept>ws://127.0.0.1:9444/foobar</accept>
<connect>tcp://localhost:50189</connect>
<type>proxy</type>
<cross-site-constraint>
<allow-origin>http://127.0.0.1:8888</allow-origin>
</cross-site-constraint>
</service>
This setup works with the Java implementation of the Kaazing client. Now I want to use this service in a GWT application.
This is my Code for the GWT client implementation:
WebSocket ws;
try {
ws = new WebSocket("ws://127.0.0.1:9444/foobar");
ws.addCloseHandler(new CloseHandler() {
#Override
public void onClose(CloseEvent ev) {
}
});
ws.addMessageHandler(new MessageHandler() {
#Override
public void onMessage(MessageEvent ev) {
System.out.println(ev.getData());
}
});
ws.addOpenHandler(new OpenHandler() {
#Override
public void onOpen(OpenEvent ev) {
}
});
} catch (WebSocketException e) {
e.printStackTrace();
}
The GWT client can successfully send text to the backend server. It just can't process any response from the server. A JavaScript Exception is thrown as soon as ev.getData() is called.
Uncaught JavaScript exception [Uncaught java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject$ cannot be cast to java.lang.String]
If I change the WebSocket address in the code above to ws://127.0.0.1:9444/echo to use the Kaazing echo service, any text I send is successfully received by the GWT application. If I change the service back to proxy (or broadcast) the above JavaScript exception is thrown every time a message from the server is received.
The same code in a Java program (using the Kaazing java client) works just fine.
As I am no GWT expert, is there any way I can further debug the problem or is there a simple solution I just fail to see?
I believe I've seen that issue before, do you mind sharing your gwtclient.gwt.xml file content? Or, maybe, try adding '' or '' depending on whether you are using ByteSocket or WebSocket in the 'gwtclient.gwt.xml' of the GWT client.
Hope this helps,
-Marcelo