I have a web service I am trying to call from Java. The XSD for the service defines a field as an xsd:base64Binary. I am using maven jaxb2 plugin to generate Java artifacts. The field becomes a byte[] in the generated Java object. The data that comes back in that field is either CSV or XML data depending on what is passed into the service. SoapUI displays the data perfectly (not encoded). Watching the wire with wireshark I can also see the non encoded data. My question is, how do I grab this data as a string in Java? I want to take this data and later write it into a file.
Response looks something like this:
Service Agreement,Interval Start Time,Interval End Time,Quantity,Unit of Measure .... etc.
Relevant bit of XSD:
Relevant bit of generated java:
protected byte[] greenDoc;
In my client java code I have been trying every possible combination of new String(byte[]), new String(byte[], charset), Base64 decoding, etc. and I just cannot seem to get the data correctly. I know it is not a limitation of the web service because like I said SoapUI can display the data perfectly.
Any pointers on how the client code can take the byte array and convert to string? Thanks!
Programmatically, you can use DatatypeConverter
Related
I have the following csv file (In production the number of records can range from 20k-100k and has many fields )
id,firstname,lastname,email,profession
100,Betta,Wandie,Betta.Wandie#gmail.com,developer
101,Janey,Firmin,Janey.Firmin#gmail.com,doctor
I need to convert this to json and do further processing.
CSV->JSON->PROCESS FURTHER
.I am able to convert it to JSON directly using the code given here
directly convert CSV file to JSON file using the Jackson library
But i want do validations for json like if lastname has null value then ignore that record or id is missing then ignore that record.
How can i handle the validation?I am using Java 8 and spring boot latest version
I have done something similar by using JavaScript (Nashorn). Yes, that is nasty, but it works, and it is astonishingly fast!
Unfortunately, I do not have the source code at hand …
Why I did it that way had the same reasons as #chrylis-on strike implies in their comment: the validation is much easier if you have an object for each JSON record. But as I was lazy, and there was definitely no need for the Java object I had to create for this, I had the idea with the JavaScript script inside my Java program.
Basically, a JSON String is the source for a JavaScript program; you can assign it directly to a JavaScript variable and then you can access the records as array elements and the fields by their name. So your JavaScript code walks through the records and drops those that do not match your validation rules.
Now the Java part: the keyword here is JSR223; it is an API that allows you to execute scripts inside your Java environment. In your case, you have to provide the converted JSON in the context, then you start the script that writes the modified JSON back to the context.
If the converted JSON is too large, you can even use the same technique to check record by record; if you compile the script, it is nearly as fast as native Java.
You can even omit Jackson and let JavaScript do the conversion …
Sorry that I cannot provide code samples; I will try to get hold on them and add them if I get them.
I have to make call to OpenNMS REST service, but it only accepts PUT with application/x-www-form-urlencoded, what is the way using Java to encode non trivial objects into this format.
First, to address getting Java content to application-x-www-form-urlencoded: Java has the URLEncoder which will let you convert a String to the desired format.
To get your data into a useful format to go into the URLEncoder, I recommend a custom method to read the Object and produce the desired String. This is dependent upon the specific call you are trying to make (from a quick review of OpenNMS ReST, they have many different routes and requirements for data associated)).
If making a large number of these calls I recommend making methods to address each of the API calls on the Object that would internally produce the desired String, encode it, and then return the Encoded content for the call.
I am having difficulty receiving a PDF file via a rest service. The rest service returns a long string with the data that is suppose to make up the PDF. My overall goal is to save the response as a PDF file for later use.
I call this service url: http://4.hidemyass.com/ip-2/encrypted/dnXuHKAZVZaONS2GNfC9RFn8k8puE2YJx6MjcPDMaKdpMRTBkvNF4CrTg4m7GeKjcLfO1bgYWIwR9bz1ZJP-LTK6Gm8tG_-d4V-oSUMfT-tIJMuZizsz9AeZp5tcZWVcz62A6j7YRWqJRAS_s_cMFLlo&f=norefer
and according the docs, it should be the string contents that make up a valid PDF.
What am I missing? What do I need to do in order to make is viewable as a PDF.
Thanks!
Chuc
Sorry the above link failed.
In the end we found that sending the PDF as binary data wrapped in JSON did not work very well. The creators of the service found that their framework was manipulating the binary data ever so slightly and converting some characters. They ended up switching to Base 64 encoding which worked great.
I'm new to Netty and Java, and I'm trying to build up a simple Netty server that reads XML from a separate client (The client will keep sending me XMLs with a fixed format). For each XML, I need to do some processing.
I've looked at examples at echo-client/server and Object echo-client/server, and trying to decide which one I should model after, I'm not exactly sure if I should use a ChannelInboundByteHandlerAdapter or a ChannelInboundMessageAdapter?
And are there utility packages in Netty 3.6.x that handle demarshalling XMLs? How should I handle converting the raw data to XML?
Also, I don't know how stable 4.0.0.Beta is, since this app is not too complicated I wonder if it's just OK to use the 3.6.x.Final as it's probably more stable.
Thanks a bunch!!
Create XMLDecoder, XMLEncoder class(extends OneToOneEncoder) in Server program.
And add ChannelPipeline as keys "decoder", "encoder".
In decode of XMLDecoder class, Convert received xml to custom class using JAXB.
If a server send to client using xml, convert response custom class to xml string in encode method of XmlEncoder.
sorry, I have limited English proficiency.
I have a GWT app which is deployed on GAE. One part of my application relies on static data, which is currently stored in an XML file. The application reads this data into a collection of POJOs. The data is sent over to the client using GWT-RPC. Based on the selections made by the user, it applies filters to the collection to get specific objects (the filtering is done on the client side).
The data may contain up to 3000 records, and the total XML file size would be around 1MB. There'll be no updates on this data from the application side (it's read-only), but I may be frequently adding new records or updating/fixing existing records during the initial few months (as the application evolves). The data has no relationship with any other data in the application.
One of my main consideration in fetch performance. I tried using Apache Digester to parse the XML, but noticed that even parsing 600 records and sending them to the client was a bit slow.
Given these requirements which of the following would be better and why - 1. Keep the data in the XML file, or 2. Store the data in the app engine data store?
Thanks.
One alternative would be to load the XML file directly by GWT (without GWT-RPC and server parsing):
Use RequestBUilder to get the data from the server. Example usage: http://www.gwtapps.com/doc/html/com.google.gwt.http.client.html
Then use XMLParser to parse the reply. Example: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsXML.html#parsing
Here's an example combining both: http://www.roseindia.net/tutorials/gwt/retrieving-xml-data.shtml
The only downside is that you have to manually parse XML via DOM, where GWT-RPC directly produces objects.
Update:
based on the comments I'd recommend this:
Parse XML file with JAXB to create your objects: http://jaxb.java.net/guide/_XmlRootElement_and_unmarshalling.html
Save those to memcache: http://code.google.com/appengine/docs/java/memcache/overview.html
On RPC request, check memcache, if data not there goto 1.
The way I look at things, there are two bottle necks, though interrelated. One is loading the data (reading from XML, parsing). Another is sending it to the client.
Instead of sending the whole bunch, you might consider batch sending, like pagination.
Usually I prefer storing such files under WEB-INF.