EJB with SOAP and XML - java

I need to make application but I dont know where to start...
What do I need to do is to make stateless EJB that gets XML file trough SOAP, and parse that XML file.
I tried to make bean, but i relly dont know what to do first and how (I'm new in this area):
package ServiceX;
public class ServiceBean implements Service {
public ServiceBean() {}
public String GetXML(String xml) {
return xml;
}
}
package ServiceX;
import java.rmi.RemoteException;
import java.rmi.Remote;
public interface Service extends Remote{
public String GetXML(String xml) throws RemoteException;
}

Related

How to let user to decide which implementation to use for interface in Spring?

I'm developing an SDK, which will be used to create additional applications for batch processing. There is core-a-api module, which holds interface Client
public interface Client {
void send();
}
and core-a-impl which holds couple implementations for Client interface - HttpClient and TcpClient.
Also, there is one more core module core-b-impl, which uses a particular instance of Client interface.
public class SendingTasklet implements Tasklet {
#Autowired
private Client client
public void process() {
client.send();
}
}
What instance should be created (HttpClient or SftpClient) should be decided by the user, who creates an application using SDK. He also needs to have an ability to create its own implementation for Client and use it in SendingTasklet. A user from core dependencies can see only interfaces from -api modules. For dependency injection, I'm using Spring. All beans for particular modules are created in each module separately. The user created beans are created in user's configuration class
#Configuration
public class UsersApplicationConf {
#Bean
public Client client {
return new UsersClient();
}
}
The issue is, that somehow without exposing -impl module details for user application, he should be able to decide what Client implementation can be used from the core provided implementations or he should be able to pass one of its own.
The first thought was to use qualifiers when injecting into SendingTasklet, but then you need to create a separate instance variable for each implementation in SendingTasklet and this is not very good because if there would be more implementations for Client interface it would be required to change SendingTasklet as well. And also the problem, that user should somehow decide wich implementation to use persists.
What I did, I exposed core-a-impl for client's application. So in his configuration, he can decide what instance to create for Client interface.
#Configuration
public class UsersApplicationConf {
#Bean
public Client client {
return new HttpClient();
}
}
But this is not very smart as well and I'm thinking is there any other way how to solve this issue?
You can use strategy or factory pattern as mentioned here but personally I would go with JSR 330 that you can find an example here , below code block for spring example:
package spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static spring.Spring.Platform;
#Configuration
#ComponentScan
public class Spring {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(Spring.class);
}
#Autowired
#Platform(Platform.OperatingSystems.ANDROID)
private MarketPlace android;
#Autowired
#Platform(Platform.OperatingSystems.IOS)
private MarketPlace ios;
#PostConstruct
public void qualifyTheTweets() {
System.out.println("ios:" + this.ios);
System.out.println("android:" + this.android);
}
// the type has to be public!
#Target({ElementType.FIELD,
ElementType.METHOD,
ElementType.TYPE,
ElementType.PARAMETER})
#Retention(RetentionPolicy.RUNTIME)
#Qualifier
public static #interface Platform {
OperatingSystems value();
public static enum OperatingSystems {
IOS,
ANDROID
}
}
}
interface MarketPlace {
}
#Component
#Platform(Platform.OperatingSystems.IOS)
class AppleMarketPlace implements MarketPlace {
#Override
public String toString() {
return "apple";
}
}
#Component
#Platform(Platform.OperatingSystems.ANDROID)
class GoogleMarketPlace implements MarketPlace {
#Override
public String toString() {
return "android";
}
}
Edit: I didnt test the code but I have used javax.inject.Qualifier
with CDI if this code doesnt work let me know I will update with
correct combination and imports

JAX-WS webservice on tomcat - cannot be instantiated

I would like to know how to handle this situation.
what i have done is i have followed the tutorial http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat-ssl-connection/
to create a similar secured web service, i am able to achieve it, but i have a error ,
ml.ws.api.server.InstanceResolver.createNewInstance(InstanceResolver.java:222)
at com.sun.xml.ws.api.server.InstanceResolver.createDefault(InstanceResolver.java:184)
at com.sun.xml.ws.server.EndpointFactory.create(EndpointFactory.java:209)
the error is because i am instantiating the webservice for example from this example the HelloWorldImpl class has a constructor with arguments to set a parameter from a class.
I understand , only default constructor is possible in Jax-ws web service, but in that case i want to know how to handle this?
say like
package com.mkyong.ws;
import javax.jws.WebService;
//Service Implementation Bean
#WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
private ABC abc;
public HelloWorldImpl (ABC abc)
{
this.abc = abc;
}
#Override
public String getHelloWorldAsString() {
return "Hello World JAX-WS";
}
// and i use this abc in one of the methods
}
This is not the correct notation for a constructor:
public void HelloWorldImpl (ABC abc) {
this.abc = abc;
}
Should be:
public HelloWorldImpl (ABC abc) {
this.abc = abc;
}
Update: So why not add a default constructor?
package com.mkyong.ws;
import javax.jws.WebService;
//Service Implementation Bean
#WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
private ABC abc;
public HelloWorldImpl() {
this("Hello World JAX-WS");
}
public HelloWorldImpl (ABC abc)
{
this.abc = abc;
}
#Override
public String getHelloWorldAsString() {
return this.getAbc();
}
// and i use this abc in one of the methods
}
I think you can control how the HelloWorldImpl gets created by using: #InstanceResolverAnnotation
I wasn't able to find any good example, but here is one link that uses this annotation: http://blog.vinodsingh.com/2008/12/let-spring-load-service-class-for-jax.html
The idea is that the class specified as the Value for this Annotation, will be used to create the Instance of HelloWorld WebService.

Amdatu: How to make ExceptionMapper (#Provider) to work?

I'm trying to manage all my exceptions with an ExceptionMapper, as i saw in multiple documentation and examples. However, it doesn't seem to work, at least in my conditions.
I'm in a OSGI environment, using the Felix Witheboard pattern, with Amdatu Wink, so i don't have a web.xml and everything is supposed to be managed by itself.
I tried to register my ExceptionMapper as a service as i did with my web services, with no results.
#Component(immediate=true, provide={Object.class})
#Provider
public class SessionTimeoutExeptionHandler implements ExceptionMapper<SessionTimeoutException>{
public Response toResponse(SessionTimeoutException arg0) {
Response toReturn = Response.status(Status.FORBIDDEN)
.entity("session_timeout")
.build();
return toReturn;
};
}
Don't pay attention to the Response itself, i was just playing around.
My code is never called, how am i supposed to setup that provider?
You have to register the Provider in a javax.ws.rs.core.Application. That Application should be registered as a service with a higher service ranking than the default one created by the Amdatu Wink bundle.
The following is a working example.
The Exception Mapper itself:
#Provider
public class SecurityExceptionMapper implements ExceptionMapper<SecurityException>{
#Override
public Response toResponse(SecurityException arg0) {
return Response.status(403).build();
}
}
The Application:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
public class MyApplication extends Application {
#Override
public Set<Object> getSingletons() {
Set<Object> s = new HashSet<Object>();
s.add(new JacksonJsonProvider());
s.add(new SecurityExceptionMapper());
return s;
}
}
Activator setting the service ranking property.
public class Activator extends DependencyActivatorBase{
#Override
public void destroy(BundleContext arg0, DependencyManager arg1) throws Exception {
}
#Override
public void init(BundleContext arg0, DependencyManager dm) throws Exception {
Properties props = new Properties();
props.put(Constants.SERVICE_RANKING, 100);
dm.add(createComponent().setInterface(Application.class.getName(), props).setImplementation(MyApplication.class));
}
}

JAX-WS Endpoint not working

I'm writing a (very) small SOAP web service using Glassfish. The code I have is below:
Milliseconds.java
package com.suture.self.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
#WebService
#SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL)
public interface Milliseconds {
#WebMethod
String currentMilliseconds();
}
MillisecondsImpl.java
package com.suture.self.ws;
import javax.jws.WebService;
#WebService(endpointInterface = "com.suture.self.ws.Milliseconds")
public class MillisecondsImpl implements Milliseconds {
#Override
public String currentMilliseconds() {
return String.valueOf(System.currentTimeMillis());
}
}
MillisecondsEndpoint.java
package com.suture.self.ws;
import javax.xml.ws.Endpoint;
public class MillisecondsEndpoint {
public static void main(String[] args) {
Endpoint.publish("http://sutureself.com:9292/ws/milli", new MillisecondsImpl());
}
}
When I run this on my Glassfish server (through Eclipse), the admin console shows me the usual working ?wsdl and ?Tester endpoints, but not the one I have created. Hitting that url (http://sutureself.com:9292/ws/milli) in a browser also returns an "Unable to connect" error.
If I select "Launch", then I am shown two links (one for each http port in Glassfish), but these return a 404.
If I just try to hit the "Context Root" path, that also doesn't work. I need a point of entry and I just cannot find it.
What am I missing?
Please note! All the sutureself.com's in the above code are actually localhost, SO doesn't like you posting URL's with localhost evidently.
Any more information as to how my setup is configured will happily be added.
I also did an example in SOAP WS some time before but I have one doubt, why are you using Glassfish Server because you are deploying your WS by main method which will bind the address with your Service. I think for that no server is required.
Just follow these steps with eclipse for testing the WS-
Note: Please shutdown your Glassfish Server
1-Create a new java project(not dynamic web project)
2-Create a HelloWorld class in hello package
package hello;
import javax.jws.WebMethod;
import javax.jws.WebService;
#WebService
public class HelloWorld{
#WebMethod
public String method(String name)
{
return "hello " +name;
}
}
you don't need to make interface explicitly.
3:Now create a Publisher class to publish the WebService in hello package
package hello;
import javax.xml.ws.Endpoint;
public class Publisher {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Endpoint.publish("http://LH.com:9292/ws/milli", new HelloWorld());
}
}
Now you have bound your WS with http://LH.com:9292/ws/milli. Check it by http://LH.com:9292/ws/milli?wsdl or http://LH.com:9292/ws/milli?test

Removing annotations and adding xml in developing JAX-WS webservice

I am a new bie to the world of webservices , I have one query as I was developing the JAX-WS the below web service both producer and client but I was using the annotaions could you please advise me how to develop the same program without use of annotations that is using XML ..itself..
Create A Web Service Endpoint Interface
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
#WebService
#SOAPBinding(style = Style.RPC)
public interface HelloWorld{
#WebMethod String getHelloWorldAsString(String name);
}
Create A Web Service Endpoint Implementation
import javax.jws.WebService;
//Service Implementation
#WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
#Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
Create A Endpoint Publisher
import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
Java Web Service Client Via Wsimport Tool
wsimport -keep http://localhost:9999/ws/hello?wsdl
It will generate necessary client files, which is depends on the provided wsdl file. In this case, it will generate one interface and one service implementation file.
finally the main class using the generated stub classes..
package com.mkyong.client;
import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldImplService;
public class HelloWorldClient{
public static void main(String[] args) {
HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();
System.out.println(hello.getHelloWorldAsString("mkyong"));
}
}
I came across this question while having the same issue...
finally found the so needed explanation here: http://jonas.ow2.org/JONAS_5_1_1/doc/doc-en/pdf/jaxws_developer_guide.pdf
look for : Overriding annotations

Categories