PHP - Creating Client for Java WSDL - java

I have ganerate a Web Service in Java using NetBeans.
The Web Service work succesfully when I consume it from Java too.
I want to consume it from PHP, how can I do that?
NewWebService.java
package NewWeb;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
#WebService(serviceName = "NewWebService")
public class NewWebService {
#WebMethod(operationName = "hello")
public String hello(#WebParam(name = "name") String txt) {
return "Hello " + txt + " !";
}
}
Client.java
package client;
public class Client {
public static void main(String[] args) {
// TODO code application logic here
System.out.println(hello("Name"));
}
private static String hello(java.lang.String name) {
newweb.NewWebService_Service service = new
newweb.NewWebService_Service();
newweb.NewWebService port = service.getNewWebServicePort();
return port.hello(name);
}
}

Related

How can I call a method from another project in Java using Remote Method Invocation (RMI)?

I am using IntelliJ as my IDE and the code below runs alright if they are in the same src folder. However, what I want is to call the sayHello() method in another project. Is that possible? I thought this is possible since this is what RMI enables, but am I wrong?
I tried to create another project that contains a Main java class and has the same code as the Client Test Drive below, hoping to call the sayHello() method by utilizing Naming.lookup() but it doesn't work! If I try to run it, I was given a java.rmi.UnmarshalException: error unmarshalling return; exception. What am I missing?
How can I call the sayHello() method "remotely"?
Remote Interface:
package Remote;
import java.rmi.*;
public interface HelloRemote extends Remote {
String sayHello() throws RemoteException;
}
Remote Implementation
package Remote;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class HelloRemoteImpl extends UnicastRemoteObject implements HelloRemote{
public HelloRemoteImpl() throws RemoteException {};
#Override
public String sayHello() throws RemoteException {
return "Server says, \"Hello!\"";
}
public static void main(String[] args) {
try {
// 2.3 register the service
HelloRemote service = new HelloRemoteImpl();
final int PORT = 1888;
Registry registry = LocateRegistry.createRegistry(PORT);
registry.rebind("hello", service);
System.out.println("Service running on PORT: " + PORT);
} catch(Exception e) {
e.printStackTrace();
}
}
}
Client Test Drive
package Remote;
import java.rmi.Naming;
public class SayHelloTest {
public static void main(String[] args) {
try {
HelloRemote service = (HelloRemote) Naming.lookup("rmi://127.0.0.1:1888/hello");
String helloStr = service.sayHello();
System.out.println(helloStr);
} catch (Exception e) {
e.printStackTrace();
}
}
}

How to Send More Than One Argument in java soap based web services

Hello i am creating a webservice in java using eclipse ide my server code is looking like below and also my client code when i called a method using single parameter in method it return the right result but when i am trying to call a method using two parameter, it return first parameter right and the second parameter value is null what is the solution as i am new in web service
package cris;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
#WebService
public class RequestHandler {
#WebMethod
public String test(String str)
{
return "Hello"+str;
}
#WebMethod
public int add1() {
int a=5;
int b=6;
int c=a+b;
return c;
}
#WebMethod
public String test1(String str1,String str2) {
return str1+"and"+str2;
}
}
i generate wsdl file using eclipse ide and create a new client using same ide
package cris;
import java.rmi.RemoteException;
public class ClientTest {
public static void main(String[] args) {
RequestHandlerProxy req=new RequestHandlerProxy();
try {
String r = req.test("anil");
System.out.println(r);
int res=req.add1();
System.out.println(res);
String res1=req.test1("anil","sahu");
System.out.println(res1);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
and my output is
Helloanil
11
anilandnull

Apache CXF client to access multiple resourcess

My question might be stupid but I am new in the world of web services and I found a tutorial but I have some doubts about it. It is using Apache CXF. I have the Calculator class which has all the resources, CalculatorStartUp which will startup the server, and the Client class where I have more clients.
Bellow is the code:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/calc")
public class Calculator {
#GET
#Path("/add/{a}/{b}")
#Produces(MediaType.TEXT_PLAIN)
public String addPlainText(#PathParam("a") double a, #PathParam("b") double b) {
return (a + b) + "";
}
#GET
#Path("/sub/{a}/{b}")
#Produces(MediaType.TEXT_PLAIN)
public String subPlainText(#PathParam("a") double a, #PathParam("b") double b) {
return (a - b) + "";
}
}
Server:
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
public class CalculatorStartUp {
public static void main(String[] args) {
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(Calculator.class);
sf.setResourceProvider(Calculator.class,
new SingletonResourceProvider(new Calculator()));
sf.setAddress("http://localhost:9999/calcrest/");
Server server = sf.create();
}
}
Client:
import org.apache.cxf.jaxrs.client.WebClient;
public class Client {
final static String REST_URI = "http://localhost:9999/calcrest";
final static String ADD_PATH = "calc/add";
final static String SUB_PATH = "calc/sub";
final static String MUL_PATH = "calc/mul";
final static String DIV_PATH = "calc/div";
public static void main(String[] args) {
int a = 122;
int b = 34;
String s = "";
WebClient plainAddClient = WebClient.create(REST_URI);
plainAddClient.path(ADD_PATH).path(a + "/" + b).accept("text/plain");
s = plainAddClient.get(String.class);
System.out.println(s);
WebClient plainSubClient = WebClient.create(REST_URI);
plainSubClient.path(SUB_PATH).path(a + "/" + b).accept("text/plain");
s = plainSubClient.get(String.class);
System.out.println(s);
}
My questions are:
why there are two clients? what if I write some resources for the mul and div resources? Do I need to add more resources..why write a client for each resource? there has to be a way to create only one client that can access a certain resource.
I saw that when creating a web client you can pass a provider or a list of providers. Can anyone explain what those providers represent?
I would appreciate any help!
For your questions:
You can re-use the same WebClient. If you want to re-use it you need to call the WebClient.back(true) or WebClient.replacePath(path) to update the path and can re-use the same baseURI.
You can use the WebClient.create(String baseAddress, List<?> providers) where the second argument is to provide JAX-RS provider like JacksonJsonProvider. Providers are used to customize the JAX-RS runtime. WebClient client = WebClient.create(REST_URI,
Collections.singletonList(new JacksonJsonProvider()));
More about JAX-RS providers: What does Provider in JAX-RS mean?

Akka Remote connection using Java

I am new to Akka and am not familiar with Scala at all. I am trying to use the Akka+Java for running a project on multiple machines. I was able to successfully run Akka Java examples locally but when I tried to run any of them on two machine, the code stops working. I've looked at the Akka-Sample-Remote source code as well. It also works on a single machine but breaks when used on more than one machine. I think the problem is with the way I set the configurations. I have summarized the problem into a simple HelloWorld problem as below.
There code is divided into two projects with two separate configuration files: a Hello Actor project and a World Actor project. World Actor waits for receiving the Hello Message from the Hello Actor and then prints out the "Hello World". Here below you can see the code and the configuration for these two projects. As you can see, the World Actor is started on port 1719 and the Hello Actor starts on port 1720 and tries to connect to the World Actor using "akka.tcp://WorldApplication#192.27.336.187:1719/user/WorldActor" . Any idea on what is wrong with he code/configuration?
JWorld.java:
public class JWorld {
public static void main(String[] args) {
JWorldApplication app = new JWorldApplication();
System.out.println("Started World Application - waiting for Hello message");
}
}
JWorldApplication.java:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class JWorldApplication {
private ActorSystem system;
public JWorldApplication() {
system = ActorSystem.create("WorldApplication", ConfigFactory.load()
.getConfig("WorldConfig"));
ActorRef actor = system.actorOf(new Props(JWorldActor.class),
"WorldActor");
}
}
JWolrdActor.java:
import akka.actor.UntypedActor;
public class JWorldActor extends UntypedActor {
#Override
public void onReceive(Object message) {
if (message instanceof HelloMessage) {
HelloMessage recMsg = (HelloMessage) message;
System.out.println("Received Message: " + recMsg.getText());
System.out.println("***** Hello World! ******" );
} else {
System.out.println("UnHandled Message Received" );
unhandled(message);
}
}
}
HelloMessage.java:
import akka.actor.ActorRef;
public class HelloMessage{
private ActorRef receiver;
private String text;
HelloMessage() {}
HelloMessage(ActorRef receiver){ this.receiver = receiver;}
public ActorRef getReceiver(){ return receiver;}
public void setText(String text) { this.text = text;}
public String getText() {return text;}
}
Application.conf:
WorldConfig {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname="192.27.336.187"
port=1719
}
}
}
JHello.java:
public class JHello {
public static void main(String[] args) {
JHelloApplication testApp = new JHelloApplication();
System.out.println("Started Hello Application - Sending Hello Message");
testApp.sayHello();
}
}
JHelloApplication.java:
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;
public class JHelloApplication {
private ActorSystem system;
private ActorRef remoteActor, myActor;
public JHelloApplication() {
system = ActorSystem.create("HelloApplication", ConfigFactory.load()
.getConfig("HelloConfig"));
myActor = system.actorOf(new Props(JHelloActor.class),"HelloActor");
remoteActor = system
.actorFor("akka.tcp://WorldApplication#192.27.336.187:1719/user/WorldActor");
}
public void sayHello() {
myActor.tell(new HelloMessage(remoteActor));
}
}
JHelloActor.java:
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
public class JHelloActor extends UntypedActor {
#Override
public void onReceive(Object message) {
if (message instanceof HelloMessage) {
HelloMessage msg = (HelloMessage) message;
if (msg.getReceiver() !=null){
msg.setText("Hello");
msg.getReceiver().tell(msg, getSelf());
}
} else {
System.out.println("UnHandled Message Received" );
unhandled(message);
}
}
}
application.conf:
HelloConfig {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
netty.tcp {
hostname="192.27.336.187"
port=1720
}
}
}
As mentioned by Patrcik the question is finally answered by Patrik on Akka groups. The problem was that the Akka{} tag is missing in both configuration files. Adding this tag solves the problem.

How to call Fedora Commons findObjects method (web service)

I'm trying to make a search through the Fedora Commons web service. I'm interested in the findObjects method. How can I make a search in Java equal to the example described on the findObjects syntax documentation.
I'm particularly interested in this type of request:
http://localhost:8080/fedora/search?terms=fedora&pid=true&title=true
I'll attach some code, I have a class that can call my Fedora service already.
package test.fedora;
import info.fedora.definitions._1._0.types.DatastreamDef;
import info.fedora.definitions._1._0.types.MIMETypedStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import javax.xml.ws.BindingProvider;
import org.w3c.dom.Document;
public class FedoraAccessor {
info.fedora.definitions._1._0.api.FedoraAPIAService service;
info.fedora.definitions._1._0.api.FedoraAPIA port;
final String username = "xxxx";
final String password = "yyyy";
public FedoraAClient() {
service = new info.fedora.definitions._1._0.api.FedoraAPIAService();
port = service.getFedoraAPIAServiceHTTPPort();
((BindingProvider) port.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
((BindingProvider) port).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
}
public List findObjects() {
//how?
}
public List<DatastreamDef> listDatastreams(String pid, String asOfTime) {
List<DatastreamDef> result = null;
try {
result = port.listDatastreams(pid, asOfTime);
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
}
It's easier using the client from mediashelf (http://mediashelf.github.com/fedora-client/). Here's an example searching for objects containing the string foobar in the title:
#Test
public void doTest() throws FedoraClientException {
connect();
FindObjectsResponse response = null;
response = findObjects().pid().title().query("title~foobar").execute(fedoraClient);
List<String> pids = response.getPids();
List<String> titles = new ArrayList<String>();
for (String pid : pids) {
titles.add(response.getObjectField(pid, "title").get(0));
}
assertEquals(7, titles.size());
}

Categories

Resources