Asynchronous web service call in flex and java - java

How can i access to my Asynchronous web service in java? I made this thing in Flex Builder, and it`s pretty easy: just add web service throw "Data -> Connect to Web Service -> Enter the URL of wsdl" and add this lines:
private function result(e:ResultEvent):void
{
trace(e.result.toString());
}
private function fault(e:FaultEvent):void
{
trace(e.toString());
}
var d:DomainAuth = new DomainAuth();
d.AuthFuncName(login, pass);
d.addEventListener(ResultEvent.RESULT, result);
d.addEventListener(FaultEvent.FAULT, fault);
How can i do this in Java using eclipse EE?

Basically you need to do a SOAP web service client in java if I understand you correctly.
JAX-WS can be your friend. The following code is from here
package simpleclient;
import javax.xml.ws.WebServiceRef;
import helloservice.endpoint.HelloService;
import helloservice.endpoint.Hello;
public class HelloClient {
#WebServiceRef(wsdlLocation="http://localhost:8080/
helloservice/hello?wsdl")
static HelloService service;
public static void main(String[] args) {
try {
HelloClient client = new HelloClient();
client.doTest(args);
} catch(Exception e) {
e.printStackTrace();
}
}
public void doTest(String[] args) {
try {
System.out.println("Retrieving the port from
the following service: " + service);
Hello port = service.getHelloPort();
System.out.println("Invoking the sayHello operation
on the port.");
String name;
if (args.length > 0) {
name = args[0];
} else {
name = "No Name";
}
String response = port.sayHello(name);
System.out.println(response);
} catch(Exception e) {
e.printStackTrace();
}
}
}

Related

Run Spring Boot server and HttpServer on single application?

I have init method which starts HttpServer with controller:
public void init() {
GatewayServer server = new GatewayServer("some_host", 8080);
server.registerController(WorkshopOrderEndpoint.class);
ControllerFactory.createController();
server.startServer();
}
This is the GatewayServer.class:
public class GatewayServer {
private static Logger logger = LogManager.getFormatterLogger(GatewayServer.class);
private final String serverHost;
private final String serverPort;
private URI address;
private ResourceConfig resourceConfig = null;
private com.sun.net.httpserver.HttpServer server;
public GatewayServer(final String host, final Integer port) {
serverHost = host;
serverPort = String.valueOf(port);
try {
logger.info("HTTP: Create Http-Server.");
resourceConfig = new ResourceConfig();
address = new URI(String.format("http://%s:%s/", serverHost, serverPort));
} catch (URISyntaxException ex) {
logger.error("HTTP: %s", ex.getMessage());
LoggingHelper.sendExceptionLog(ex, "STATUS_URI_ERROR", "URI Encoding error.");
} catch (ProcessingException ex) {
logger.error("HTTP: %s", ex.getMessage());
LoggingHelper.sendExceptionLog(ex, "STATUS_HTTP_ERROR", "HTTP-Server start error.");
}
}
public void registerController(Class<?> controller) {
if (resourceConfig != null) {
logger.info("HTTP: Register Controller: %s", controller.getName());
resourceConfig.register(controller);
}
}
public void startServer() {
server = JdkHttpServerFactory.createHttpServer(address, resourceConfig, false);
logger.info("HTTP: Start Http-Server. Adress: %s", address);
server.start();
}
public void stopServer(int delay) {
logger.info("HTTP: Stop Http-Server. Address: %s", address);
server.stop(delay);
}
}
This is pure java application and I want to start Spring Server in order to run Eureka Server by adding this code to the init() method:
SpringRestApplication springRestApplication = new SpringRestApplication();
springRestApplication.start();
Where SpringRestApplication.class is starting the Spring Boot server:
#SpringBootApplication
#EnableEurekaServer
public class SpringRestApplication {
public void start() {
SpringApplication.run(SpringRestApplication.class, new String[0]);
}
}
I would to run two servers on same host but different ports is it possible to connect Spring Boot Tomcat server with HttpServer?
You can run the two on different ports.
Eugene shows a couple of options to change the port of the Spring Boot application: https://www.baeldung.com/spring-boot-change-port
This is the most straight-forward:
public void start() {
SpringApplication app = new SpringApplication(SpringRestApplication.class);
app.setDefaultProperties(Collections
.singletonMap("server.port", "8083"));
app.run(args);
}

Java RMI with Callable

I have a server and several clients. The server should be able to delegate tasks to the clients so I tried to implement RMI. I followed this tutorial and everything is working fine if I use String as param- and/or return-value.
Now the server should send undefined tasks to the clients so I tried to use a Callable as param but the program crashed with a NotSerializableException. Since Callable doesn't implement the Serializeable interface thats the result I expected.
Now I found several sources that use Callable and Runnable as params and that confuses me. Is there any trick to get it to work? Or do i miss something important? Maybe theres a technology that fits better?
Resource1 S. 33
Resource2 s. 5
And heres my code:
// Client
public static void main(String[] args) throws InterruptedException {
App app = new App();
app.startClient();
Thread.sleep(20000);//just for test purpose
}
private void startClient() {
try {
// create on port 1099
Registry registry = LocateRegistry.createRegistry(1099);
// create a new service named myMessage
registry.rebind("calcClient", new CalculateRemoteImpl<String>());
} catch (RemoteException e) {
e.printStackTrace();
}
System.out.println("System is ready");
}
// RemoteInterface
public interface CalculateRemote<T> extends Remote {
public T hello(Callable<T> hello) throws RemoteException;
}
// RemoteInterfaceImpl
public class CalculateRemoteImpl<T> extends UnicastRemoteObject implements CalculateRemote<T> {
public T hello(Callable<T> hello) throws RemoteException {
return (T) ("Hello " + hello);// just print address of object
}
}
.
// Server
public static void main(String[] args) {
App app = new App();
app.doTest();
}
private void doTest() {
try {
// fire to localhost port 1099
Registry myRegistry = LocateRegistry.getRegistry("127.0.0.1", 1099);
// search for myMessage service
CalculateRemote<String> impl = (CalculateRemote<String>) myRegistry.lookup("calcClient");
// call server's method
System.out.println("Message: " + impl.hello(new Callable<String>() {
public String call() throws RemoteException, Exception {
return "hello";
}
}));
System.out.println("Message Sent");
} catch (NotBoundException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
// And the same RemoteInterface
public interface CalculateRemote<T> extends Remote {
public T hello(Callable<T> hello) throws RemoteException;
}
.
// stacktrace
java.rmi.MarshalException: error marshalling arguments; nested exception is:
java.io.NotSerializableException: de.fhb.rmicalcserver.App$1
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:156)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148)
at $Proxy0.hello(Unknown Source)
at de.fhb.rmicalcserver.App.doTest(App.java:30)
at de.fhb.rmicalcserver.App.main(App.java:18)
Caused by: java.io.NotSerializableException: de.fhb.rmicalcserver.App$1
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
at sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:292)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:151)
If you want to send objects to clients whose classes aren't deployed at the client you need to take a long look at the RMI codebase feature.

Connection refused to host using RMI

First of here is the Exception that I'm getting: http://i.imgur.com/dE5Ou.png
Just to give little background I'm trying to write simple RMI program that connects two remote computers (Client/Server) using java's RMI. I have my Server program up and running but when I run my Client program I get the exception showed above in the link. Since I'm telling it to connect to 192.168.0.104 why is it saying "Connection refused to host: 127.0.1.1"???
Client
public class Client
{
public static void main(String[] args)
{
ServerInterface server;
Registry registry;
try
{
registry = LocateRegistry.getRegistry("192.168.0.104", (new Integer(1099)).intValue());
server = (ServerInterface)Naming.lookup("//192.168.0.104/ServerTest");
String serverString = server.getAndSetMessage("Connecting");
System.out.println("Reply from the server is: " + serverString);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
Server
public class Server extends UnicastRemoteObject implements ServerInterface
{
static String hostName = "192.168.0.104";
String name;
public Server(String name) throws RemoteException
{
super();
this.name = name;
}
public String getAndSetMessage(String message) throws RemoteException
{
return("My name is " + name + " Thanks for message " + message);
}
public static void main(String args[])
{
try
{
String objectname = "ServerTest";
Server theServer = new Server(objectname);
Naming.rebind("//"+hostName+"/"+objectname,theServer);
System.out.println("//"+hostName+"/"+objectname);
System.out.println("I am Registered");
}
catch (Exception ex)
{
System.out.println(ex);
System.exit(1);
}
}
}
You could try to add the following code to the server:
System.setProperty("java.rmi.server.hostname", "192.168.0.104");

PubSubHubBub in Java: can't subscribe

I'm trying to develop a simple Java PubSubHubBub application. I downloaded this sample code to test it, but when I try to subscribe I always get an error 409.
Following PuSH specifications, I created my own feed at this link: receivetemplate.eu01.aws.af.cm/feed/
This is the code in the Test class for the subscriber:
package sub;
import java.net.InetAddress;
import PubSubHubbub.Web;
import PubSubHubbub.Subscriber;
public class Test {
private static Web webserver;
private static Subscriber sbcbr;
private static String hostname = null;
private static Integer webserverPort = 8080;
private static void startServer(){
try {
webserver = new Web(webserverPort);
sbcbr = new Subscriber(webserver);
InetAddress addr = InetAddress.getByName("receivetemplate.eu01.aws.af.cm");
hostname = addr.getHostName();
System.out.println("http://" + hostname + "/");
hostname = "http://" + hostname + "/";
} catch (Exception e) {
e.printStackTrace();
System.out.println("WebServer can not start");
}
}
public static void main(String[] args) {
try {
String hub = "http://pubsubhubbub.appspot.com/subscribe";
String hub_topic = "http://receivetemplate.eu01.aws.af.cm/feed/";
startServer();
int statusCode = sbcbr.subscribe(hub, hub_topic, hostname, null, null);
if (statusCode == 204){
System.out.println("the status code of the subscription is 204: the request was verified and that the subscription is active");
} else if (statusCode == 202){
System.out.println("the status code of the subscription is 202: the subscription has yet to be verified (i.e., the hub is using asynchronous verification)");
} else{
System.out.println("the status code of the subscription is:" + statusCode);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
If I replace hub_topic with http://pubsubhubbub-subscriber.appspot.com/ and if I pass pubsubhubbub-subscriber.appspot.com to InetAddress.getByName(), the response I get is 204 and everything works.
Can you give me some informations on what I'm doing wrong? Is there any error in my feed?

How to get DN and password with UnboundID

I need some help concerning UnboundID. I heard it was a great choice but I'm not really used to it.
So I need to make a LDAP listener. On this listener, i should be able to catch bind request (from a ldap browser for example). I wonder how to get the DN and the password. Here is my code for the LDAP listener:
public ResultCode CreateLdapServer () throws LDAPException {
CannedResponseRequestHandler requestHandler = new CannedResponseRequestHandler();
LDAPListenerConfig config =
new LDAPListenerConfig(4243, requestHandler);
try
{
config.setListenAddress(
InetAddress.getByName("localhost"));
}
catch (final Exception e)
{
System.err.println("Unable to create the listen server.");
return ResultCode.PARAM_ERROR;
}
listener = new LDAPListener(config);
try
{
listener.startListening();
System.out.println("Serveur is listening ...");
}
catch (final Exception e)
{
System.err.println("Unable to start listening.");
return ResultCode.LOCAL_ERROR;
}
return ResultCode.SUCCESS;
}
public static void main(String[] args) throws LDAPException {
MyConnection connect = new MyConnection();
connect.CreateLdapServer();
}
I read a lot of UnboundID documentation, but i can't find any simple example of what I need.
Also, i'm not really sure of the utility of CannedResponseRequestHandler. For what i need, is it enough ?
An other question: I'm not sure, but I have the feeling that my server is not listening OR i don't catch anything (when I connect with a ldap Browser, nothing happened). Any Idea / Suggestion ?
Thanks and have a nice day !
EDIT : Thanks to xhochy, I was able to catch the password and the username. As he said, I subclassed LDAPListenerRequestyHandler to override, first, newInstance then ProcessBindRequest. Here is the code (it's absolutely not perfect and it's still a beginning).
public class MyConnection {
private LDAPListener listener;
public MyConnection(){
}
public ResultCode CreateLdapServer() throws LDAPException {
MyLDAPListenerRequestHandler requestHandler = new MyLDAPListenerRequestHandler();
LDAPListenerConfig config =
new LDAPListenerConfig(4243, requestHandler);
try
{
config.setListenAddress(
InetAddress.getByName("localhost"));
}
catch (final Exception e)
{
System.err.println("Unable to create the listen server.");
return ResultCode.PARAM_ERROR;
}
listener = new LDAPListener(config);
try
{
listener.startListening();
System.out.println("Serveur is listening ...");
}
catch (IOException e)
{
System.err.println("Unable to start listening.");
return ResultCode.LOCAL_ERROR;
}
return ResultCode.SUCCESS;
}
public static void main(String[] args) throws LDAPException {
MyConnection connect = new MyConnection();
connect.CreateLdapServer();
}
}
Then the subclass of LDAPListenerRequestHandler:
public class MyLDAPListenerRequestHandler extends LDAPListenerRequestHandler {
#Override
public LDAPListenerRequestHandler newInstance(
LDAPListenerClientConnection arg0) throws LDAPException {
System.out.println("New Instance.");
LDAPConnectionOptions option = new LDAPConnectionOptions();
LDAPConnection connection = new LDAPConnection(option, "yourIPadress", yourport);
System.out.println("Connected to : " + connection.getConnectedAddress()+ " " + connection.getConnectedPort());
return this;
}
#Override
public LDAPMessage processBindRequest(int arg0, BindRequestProtocolOp arg1,
List<Control> arg2) {
System.out.println(arg1.getBindDN());
System.out.println(arg1.getSimplePassword());
return null;
}
}
Thanks again !
Many LDAP server implementations will not return a password and many will not return a password you can use. (ie it maybe a hash).
I would be very curious why there could be a reason to return the password.
-jim
You should subclass LDAPListenerRequestHandler and implement processBindRequest. All the information you are looking for is included in BindRequestProtocolOp (second argument of processBindRequest). Add an empty implementation for all other abstract methods.
If request is your BindRequestProtocolOp instance then you get your information via:
String username = request.getBindDN();
ByteString password = request.getSimplePassword();

Categories