Custom fields in WSDL for Cxf Webservice - web-services

I need to generate a custom wsdl from my java artifacts. I use cxf apis to generate service.
Let me describe the scenario in details,
I want to generate different WSDL from same set of java artifacts.The idea behind such requirement to is to provide license based operation/messages to the customers. We have set of java objects defined as messages and service interfaces/implementation. Using Cxf API (ServerFactoryBean) I generate Service/WSDL on demand( purely run time operation).
Just to make it clear, I am giving example scenario
public class InputBean {
private XYZ xyz;
private ABC abc;
private PQR pqr;
Where XYZ, ABC, PQR are some java objects and InputBean is used in my Service Implementation class.
The requirement is to have these fields conditionally based on license
if(XYZ Licensed){
Include XYZ in bean
}
if(PQR Licensed){
Include PQR in bean
}
if(XYZ and PQR licensed){
Inlcude XYZ and PQR
}
I know this is strange requirement, and not sure if I had explained my problem clearly.
I would have tried with inheritance(dynamic method dispatch), but I want random combinations.
Any help would be appreciated!

I have solved this issue by creating a template file(free marker template), with place holder for required classes. This will be substituted at run time with required class names, created java source files using free marker api and then created java classes by java compiler api. Also developed custom class loader to load the class at run time. All the operation will perform in background at runtime. I could not find a better solution for my requirement, but I am happy with this as it is giving perfect result as expected.

Related

JAX/WS wsgen is not generating

I have a problem like this ; Could you please help me if you know the reason?
I m trying to generate jaxb classes for input and out types using wsgen command line utility.
The print screen you posted (and #home's comment is spot on - you should add the stacktrace not a print screen) tells you the problem.
#javax.jws.WebService annotated classes that do not belong to a package must have the #javax.jws.WebService.targetNamespace element.
You don't specify a target namespace and wsgen can't infer one because you classes are in the default package. Having classes in the default package is a frown upon practice. Put your classes under a package and try again or have the WebService.targetNamespace element set up which also gives you more control on what the tool generates.
#javax.jws.WebService the annotated classe who implements Web Service Business interface, must have a no-argument constructor otherwise wsGen tool gets such error.
Reference: Creating a Simple Web Service and Client with JAX-WS

Compile Scala classes on the fly to implement webservices

I am struggling with how to dynamically add some webservices. I am using Scalatra for the webservice framework.
I want to allow the developer to be able to change authentication, for example, so that rather than using hard-coded credentials, instead use a database or password file or whatever they need.
I also want to allow them to add new webservices inside the servlet.
So, what I want to do is in the bootstrap code have it load up and recompile the class and then use that version.
I have looked at this, but I need to recompile an entire class, not snippets.
Generating a class from string and instantiating it in Scala 2.10
This is what I have tried, but I added a "/help" webservice but it isn't found, so the new class isn't being used yet.
class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) {
val sourceDir = new java.io.File("C:/Temp/MyServlet.scala")
val sse = ScalaScriptEngine.onChangeRefresh(sourceDir)
sse.refresh
println("*** - " + sse.compilationStatus.startTime + " " + sse.compilationStatus.stopTime)
context.mount(sse.get[MyServlet]("test.proj.MyServlet"), "/*")
I am using scalascriptengine (https://code.google.com/p/scalascriptengine/) at the moment.
So, how can I recompile the class file for the webservice, when it may have case classes, annotations and object classes in the same file, on the fly?
I am wondering if I need to have the webservice in Groovy instead, but I would prefer to keep it functional.
UPDATE
I had thought about plugins first, but ran into a problem with how would I add new webservices that way, and it may be that Scalatra is not going to be the right choice, that I may need to change my REST service framework.
Eventually I want to be able to change the webservices on the fly without having to restart the application, and recompiling the source would allow that.
Realizing a plug-in affordance is not too hard, at least for reasonably simple cases. The essential elements are:
A trait or abstract class defining the obligations of realizations of the plug-in.
A means to get the code for plug-ins onto the class-path. Alternatively, if you're familiar with working with classloaders, you can do it dynamically. I don't have much experience with that.
Once you have an instance of java.lang.Class[P <: PlugInType] it's trivial to get an instance, provided you don't need constructor parameters.
A protocol in the plug-in trait that allows the plug-in to, e.g., reserve a top-level URL path segment from which you derive a Scalatra route that covers all those paths. You then dispatch requests that match that leading path segment via the plug-in instance. All you have to do is make sure you don't let two plug-ins claim the same path or if you do you have some further means of resolving them.
Thanks to #RandallSchultz I found a solution that works.
override def init(context: ServletContext) {
val sourceDir = new java.io.File("C:/Temp/HelpServlet.scala")
val sse = ScalaScriptEngine.onChangeRefresh(sourceDir)
sse.refresh
println("*** - " + sse.compilationStatus.startTime + " " + sse.compilationStatus.stopTime)
context.mount(new MyServlet, "/*")
context.mount(sse.get("org.myproject.rest.HelpServlet"), "/help/*")
So when I went to "/help/help" I got my help page as expected, so the new webservice was added.
By reading it from a file I was able to add it while the application was running.

Design architecture to contact 3rd party Web Service

Actually the code is structured as in the first design. But I think it's not very readable..
The namespaces name are saying nothing of the responsability of each class
Is it really useful to split between DAO and BLO layer a so small architecture? There is almost no logic..
The logic / responsability of the BLO is not identifiable by the names of class / methods / namespace
What is UserType? The namespace is not grouping it with any other class..
Config class is a good name for me since it identifies a functionality. But It's the only class with a precise purpose specified by its name.
Get3rdPartyUrl and logging in (to that url) could all be put together in a same class, while InitializeConfigValue and ParseErrorMessage could be put into some other Help class.
Contacting a 3rdParty WS is reusable. Everything should inherit from just one interface also defining logging.
The WS which I contact will make a callback to my infrastructure. I could I recognize / trace the session between this two calls? From my infrastructure and back to my infrastructure?
I wait your comment and proposal!
EDIT
This is the result after the first refactoring. What do you think about?
I do not split beetwen DAO and BLO levels in small projects. I use QueryObject pattern and put all my queries into this objects. You can put simple logic (validation f.e.) into this queries.
The WS which I contact will make a callback to my infrastructure. I could I recognize / >trace the session between this two calls? From my infrastructure and back to my >infrastructure?
You can use WS-Addressing. WS-Addressing headers has fields wsa:MessageID and wsa:RelatesTo for message correlations (mean this as MessageId and CorrelationId) and wsa:ReplyTo/wsa:Address for callback server address.
For example Oracle SOA Suite has strong WS-Addressing support out-of-the-box.

Web services passing entities as parameters

I just designed a web service (code firts approach) with one method with the following signature getItems(MyEntity myEntity) The point is that MyEntity has others Entities as properties and so on. Because of this, when the client generates the classes from the WSDL, lots of entities are created in the client side besides the xml (WSDL) generated is too complex, the question is; is it rigth to expose the entities of my sistem to the clients?, or should i create a entity with simple values? (int amount, String code, String user) like a facade.
I would recommend only exposing the properties and objects that you need. If there are properties on your MyEntity class that you will never expect the client to populate, there's no point in including that in the WSDL.

(Java) How can I pass Schema-validated XML documents as parameters between distributed components (e.g. web services or sockets)?

Here is a description of the scenario and I would appreciate also any comments on the approach used
The core of my application is a set of web services backed by a P2P database. One service accepts a simple XML-based record (I have designed a generic schema for it). The service processes this data (mainly creating keys based on certain criteria) and pass the original data along with the created keys to a listening SocketServer in one of the listening P2P nodes. This key,data pair is routed to the proper node, which stores the data (associated with the key as an ID) in an XML database.
A second service accepts a query document that is structured based on the same schema, but with optional values that would be used for searching and matching from the previously stored ones. So the second service would pass this query (with the proper keys) to the P2P part, get back the results and pass them back to the service client.
E.g. if the original record submitted to the first service was < attr1 >value1 < /attr1 > < attr2 > value2 < / attr2 > (attribute list along with some other metadata mandated by the schema) then the second service should retrieve that record if the query received was < attr2 >value2 < / attr2 >
(I could later think about using more complex XPath or XQuery queries as the underlying XML database allows instead of exact matches for values here but that is not important at this stage. there is also a third service I am working on but it depends on getting the first two in proper shape first)
So my questions are:
1) What data type should I use as the parameters of the web services? How to utilize my schema for this usage? I was considering various XML binding frameworks (especially JAXB and SDO) for this but didn't know how to proceed.
2) How can I enhance the two services (call them store and search) to use dynamically created templates based on the original generic schema? The service would still accept documents of the main schema type but has the inner attribute list based on a template say template1 only requires whose values are ints while template2 require (float) and (string). The current JSP-based prototype manually creates this template but as an XML document that is assembled by hand (<>tags dispersed in text) and there is no type checking what so ever so I thought I could do better!
3) Is it possible to generate a quick web app prototype for simple access to this system (again by using the schema (&templates) to edit the appropriate XML message structures? What I am looking for is for the (human) user to choose a template and then just "fill in the blanks" and submit, no need for any fancy look and feel.
4) Can I or how can I also use this XML message type for communicating across sockets?
5) Does it matter if I deploy the services as stateless EJBs or not? Do I need them to be EJBs or servlets would be more than enough?
I currently have a rudimentary implementation (from previous developers) that were meant for a subset of my current requirements (I am improving on the services and adding new derived ones) but there was no schema nor validation and the data is passed all along as basic strings, thus providing weak typing and difficult to update manual parsing. The reason I want to update this to a stronger bound typing is to introduce changes in the data schema that would be passed along the whole system easily. Basically I want the system to be as less coupled to the data format/schema used as possible; the current prototype is too coupled to the data that I am finding it extremely difficult to change the data without breaking the system.
My initial investigation led me to consider JAXB but it supports only static typing (cannot create a schema/types dynamically at runtime that I want to persist for later usage). So I came across SDO which has both dynamic and static typing. The problem is just that there is not enough community and/or examples of using this approach so it seems risky (the examples of Apache Tuscany and Eclipselink implementations are very scarce and I could not find complete examples that are not 5+ years old (like this http://www.ibm.com/developerworks/java/library/j-sdo/) and also tackles the XML use case of SDO (most seem to focus on the relational usage of SDO).
This is my first time asking for programming help (here and elsewhere) so please bear with me. I searched a lot on the net but I could not find anything useful but pieces here and there that did not add up.
Any comment or hint is really appreciated.
trfndr
EDIT
I forgot one thing: how would the search service get back the results? Since it is opening a client socket connection, there is no way to get back any results synchronously. The current implementation tackles this by having the service client opening a listening socket on a random port and putting this contact info in the query document. After the search web service sends the query to the p2p part it finishes. The p2p sends the results as a WS call to another service which sends them back to the service client socket. I don't like this approach much, is there any more elegant solution?
I lead the EclipseLink JAXB & SDO implementations and represent Oracle on those specifications so hopefully I can help you out. This question is very similar to talk I'm giving at JavaOne in September.
1) What data type should I use as the
parameters of the web services? How to
utilize my schema for this usage? I
was considering various XML binding
frameworks (especially JAXB and SDO)
for this but didn't know how to
proceed.
This depend's on what web service framework you are using. JAXB is much easier to use with JAX-WS, and while JAXB is still easier to use with JAX-RS SDO, is a possible alternative.
2) How can I enhance the two services
(call them store and search) to use
dynamically created templates based on
the original generic schema? The
service would still accept documents
of the main schema type but has the
inner attribute list based on a
template say template1 only requires
whose values are ints while template2
require (float) and (string). The
current JSP-based prototype manually
creates this template but as an XML
document that is assembled by hand
(<>tags dispersed in text) and there
is no type checking what so ever so I
thought I could do better!
I'm not 100% what you mean here, but the following may be helpful:
Using #XmlAnyElement to Build a Generic Message
3) Is it possible to generate a quick
web app prototype for simple access to
this system (again by using the schema
(&templates) to edit the appropriate
XML message structures? What I am
looking for is for the (human) user to
choose a template and then just "fill
in the blanks" and submit, no need for
any fancy look and feel.
JAX-RS is a nice framework for creating quick prototypes. Below is an example I created:
Part 1 - The Database
Part 2 - Mapping the Database to Objects
Part 3 - Mapping the Objects to XML
Part 4 - The RESTful Service
Part 5 - The Client
4) Can I or how can I also use this
XML message type for communicating
across sockets?
I prefer frameworks like JAX-RS that communicate over the HTTP protocol.
5) Does it matter if I deploy the
services as stateless EJBs or not? Do
I need them to be EJBs or servlets
would be more than enough?
My preference is to use an EJB session bean for the service. If you are interacting with a database then you can leverage the Java Transaction API (JTA) to manage your database transactions.
Part 4 - The RESTful Service
SDO
EclipseLink is the SDO 2.1.1 (JSR-235) reference implementation. We have some examples posted below. If you are looking how to do something specific, I will try to post a relevant example.
http://wiki.eclipse.org/EclipseLink/Examples/SDO
JAXB
JAXB is static. It is also more popular than SDO. Recognizing this in EclipseLink we have implemented a dynamic JAXB feature. It gives you the dynamic aspect of SDO with a JAXB slant.
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/Dynamic
EDIT #1
Since you are dealing with JAX-WS and your model is almost entirely dynamic, I think you should skip the JAXB binding altogether. In the following link see the section "Switching Off Data Binding"
http://java.sun.com/developer/technicalArticles/xml/jaxrpcpatterns3/
This will give us the body of the message as a javax.xml.transform.Source object. We will need to process the XML based on the dynamic templates. SDO would be a good choice here. You can constantly add new types to the HelperContext using XML schemas.
helperContext.getXSDHelper().define(schema1, null);
helperContext.getXSDHelper().define(schema2, null);
You wil be able to unmarshal the Source from the web service as follows:
XMLDocument doc = helperContext.getXMLHelper().load(source, null, null);
DataObject rootDataObject = doc.getRootObject();
String someValue = rootDataObject.getString("attr3/childAttr/anotherChildAttr");
You will also be able to use the XMLHelper to marshal your objects to XML when calling another service.