Specifying targetNamespace? - java

I am writing WSDL file. In WSDL file can I specify any URL to targetNamespace? Or should it be valid?
My project has two WSDLs. In both the WSDLs I gave same targetNamespace. will there be any issues? Should targetNamespace be unique across WSDLs in the project?
targetNamespace="http://wsdl.mycompany.com/service
Thsnks!

Yes you can specify any URL as Target Namespace. It is required to be a valid URL or URI (Not necessary deployed somewhere). Although it is a good practice to specify URL as namespaces. This help to make them unique and avoid conflicts.
Namespace are very similarly to java packages, they help to avoid element name conflicts. You should go for different namespaces in both WSDL's

Yes you can specify any URL (acctually any URI) as a valid target namespace since at that location mostly nothing will be found anyways. Like a sort of java packages you can see a namespace as a location ("context") in which your definition of elements, types and attributes will be integer (e.g. you for example have only one element 'Person' in your namespace unless the elements are otherwise different).
It is also possible to have as many WSDLs/ XMLs/ XSDs share the same namespace as you want.
Note in this case anything you define will be added to that namespace and must be integer within it because otherwise anyone that wants to generate stubs out of your WSDLs will get errors like "element xy is ambigious".
On the other hand you oftenly want to have certain aspects of your webservice in its own namespaces.
So you maybe want a namespace http://www.myCompany.com/namespaces/comm to which all of your technical request-types (like 'SendBillToBackofficeRequest') will belong while you have a namespace http://www.myCompany.com/namespaces/baseTypes for any business objects like 'Bill', 'Account', 'Customer' and so on.
I recomend to read something about http://www.w3schools.com/xml/xml_namespaces.asp to fully understand namespaces (it is acctually pretty easy yet most SWEs dont fully understand it).

Related

Selecting an XSD schema during SAX parsing

Assume the following:
I have a set of XSD schemas S, each with distinct namespace URIs.
I know that I'm going to be receiving an XML document containing a root element that contains exactly one namespace declaration that refers to a member of S. I can abort parsing immediately with an error if I don't receive exactly one namespace declaration, or if the received namespace doesn't refer to any schema in S.
I want to parse the incoming XML document with a SAX parser, and I want to validate the incoming document during parsing against one of the schemas in S. I know from the above that the first call I'm going to see in the ContentHandler that I give to the parser will be a call to startPrefixMapping when the parser encounters the namespace declaration.
Is it possible to, in the startPrefixMapping call, pick one of the schemas in S for validation once I know which one I need?
It seems that I could maybe call setSchema on the parser inside the startPrefixMapping call, but I get the feeling from the API documentation that I'm not supposed to do this (and that it may be too late to call the method at that point anyway).
Is there some other way to supply a set of schemas to the parser and perhaps have it pick the right one itself based on the namespace declaration it receives?
Edit: I was wrong, it's not just inadvisable to call setSchema on a parser once parsing has started - it's actually impossible. Parsers don't expose a setSchema call, only parser factories do. This means that my options are limited to those that can allow the parser to select a schema for itself. Unfortunately, that has its own problems: It's not possible for an XML document to merely specify a namespace, it also has to specify a filename for the intended schema (which in my opinion is an implementation detail on the parser side and should not be required of the incoming data) and the parser has to intercept the request for this filename to supply a member of S for validation.
Edit: I've solved this. I've put together some heavily-commented public domain example code here that looks up schemas based on pre-specified systemIds, and the schemas are delivered programatically (so they can be served from databases, class resources, etc). It correctly rejects any document that specifies an unknown schema, specifies no schema, or tries to specify its own schemaLocation to try to fool the validator.
https://github.com/io7m/xml-schema-lookup-example

JAX-RS: Is it possible to have an externally configurable #PATH?

Is it possible to load the value for the #PATH annotation from configuration (web.xml, etc) for a given class?
#Path(<value loaded from config>)
public class myRestService {
...
Independent of JAX-RS: Annotations in Java are compile time constants so they can't be changed at runtime.
I don't know your use case but possible ways to change the values of the annotations are:
Replacing variables before compilation, e.g. through a maven plugin.
Adding the #Path annotations dynamically like described here.
Using one generic ResourceClass mapped to /* which decides which subresource should be returned.
No comment if one of these approaches makes sense as I don't know why you want to change them. As the URI names a resource I don't see any reason to change it. See also: Cool URIs don't change
Update: JAX_RS_SPEC-60 requests "A Dynamic way to register JAX-RS resources (not based on annotations)".
According to JAX-RS specification (here), there is no standard way to do this, I think.

JAXB issue with missing namespace definition

So I searched around quite a bit for a solution to this particular issue and I am hoping someone can point me in a good direction.
We are receiving data as XML, and we only have XSD to validate the data. So I used JAXB to generate the Java classes. When I went to unmarshal a sample XML, I found that some attribute values are missing. It turns out that the schema expects those attributes to be QName, but the data provider didn't define the prefix in the XML.
For instance, one XML attribute value is "repository:<uuid>", but the namespace prefix "repository" is never defined in the dataset. (Never mind the provider's best practices suggest defining it!)
So when I went to unmarshal a sample set, the QName attributes with the specified prefix ("repository" in my sample above) are NULL! So it looks like JAXB is "throwing out" those attribute QName values which have undefined namespace prefix. I am surprised that it doesn't preserve even the local name.
Ideally, I would like to maintain the value as is, but it looks like I can't map the QName to a String at binding time (Schema to Java).
I tried "manually" inserting a namespace definition to the XML and it works like a charm. What would be the least complicated method to do this?
Is there a way to "insert" namespace mapping/definition at runtime? Or define it "globally" at binding time?
The simplest would be to use strings instead of QName. You can use the javaType customization to achieve this.
If you want to add prefix/namespace mappings in the runtime, there are quite a few ways to do it:
Similar to above, you could provide your own QName converter which would consider your prefixes.
You can put a SAX or StAX filter in between and declare additional prefixes in the startDocument.
What you actually need is to add your prefix mappings into the UnmarshallingContext.environmentNamespaceContext. I've checked the source code but could not find a direct and easy way to do it.
I personally would implement a SAX/StAX filter to "preprocess" your XML on the event level.

FasterXML Issues while marsahaling to XML

I am trying to marsahall Objects to XML using jackson-dataformat-xml and woodstax but its adding additional namespace prefix wstxns1. Any suggestions ?
My Beans look like as below
#JacksonRootElement(localname="Blah" namespace="http://something"
Bla {
#JacksonXMLProperty(localname="SomeProperty" namespace="http://something"
String SomePropety;
#JacksonXMLProperty(localname="SomeClass" namespace="http://something-different"
Class SomeClass;
....
I assume that you want one of following:
Define "default namespace" (one with no prefix) to bind to URI for element, to avoid prefix -- this is only possible for a single namespace at a time. OR,
Make Woodstox use some other base for prefixes it generates as needed
You would like to offer suggestion for prefix to use (since Stax XMLStreamWriter allows this).
Jackson XML module does not have mechanisms for handling prefixes at this point (although RFEs and pull requests are welcome).
But Woodstox itself has fair bit of configurability, beyond basic Stax API (which is quite limited).
The places to look beyond (documentation, blogs) for additional configuration properties for output are classes:
org.codehaus.stax2.XMLOutputFactory2 for Stax2 extension properties (implemented by Woodstox and Aalto)
com.ctc.wstx.api.WstxOutputProperties for Woodstox-specific properties
and these properties are set via XMLOutputFactory.setProperty(), same as standard Stax properties.
Property of interest here would be org.codehaus.stax2.XMLOutputFactory2#P_AUTOMATIC_NS_PREFIX, which defaults to "wstxns" but can be changed to any other valid XML id String.
Beyond this, it may be possible to specify pre-configured XMLStreamWriter for Jackson XML module to use. If so, it would also be possible to use standard Stax method ("writeNamespace()" I think?) to create specific prefix-to-URL namespace bindings.
Finally, Jackson mailing lists are the best place to ask questions. Developers like myself do read StackOverflow and other forums as well, but latency tends to be higher as you have noticed.

Is there a way of handling multiple xsd versions with xmlBeans?

I know that I can compile multiple xsd files in a single jar. I've tried using different namespaces which only takes me half way through my goal. This way I can parse the correct schema but I want this to be transparent to my users which will receive the xmlBeans object that I've parsed.
They don't have to know which version of xml file is currently present on the system. I would need a super class for every xsd version to achieve this.
Could this be done with xmlBeans?
My understanding is, if you have a com namespace and a com.v1 and com.v2 namespace and you have an xsd element called EmployeeV1 in com.v1 and EmployeeV2 in com.v2.
You want to a super class called Employee in the com namespace which you want to return to your caller?
Do you think EmployeeV1 and EmployeeV2 could extend from Employee in your xsd? Then maybe when you generate you will get the class hierarchy that represents your xsd.
If that doesn't work, (i haven't used xmlbeans in years now), you might have to create your own domain object and make your callers consume that. That might be worth the effort, since to me it looks like you handle the parsing of an XML that other people rely on, you could abstract all other users from the structure of the XML (which is in flux) by having an intermediary domain object.

Categories

Resources