Sunday, 20 September 2020

How To Solve XML Parsing Issue "Content is not allowed in prolog" In Java

If you have come to this post, then you are facing xml parsing issue in Java.

Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
        at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)

One common issue for this is BOM character. So what is a BOM (BYTE ORDER MARK) character? When you have multi byte format like UTF-16 or UTF-32, sever needs to indicate if the most significant byte starts from left or right (Big Endian & Little Endian). That's the purpose of BOM. It is a zero-width invisible character. Based on its value (FEFF or FFFE), a byte sequence can be treated as BIG Endian or Little Endian.
If your XML is in UTF-8 format, then BOM character is not required because UTF-8 is 8 bits (single byte). But the remote server might still send BOM character in the xml response due to legacy reasons. And while you are unmarshalling using JAXB, you will get above error.

To solve this, you can wrap around response input stream with BOMInputStream. It is from Apache Commons library.

BOMInputStream bomIn = new BOMInputStream(inputStream);
bomIn.hasBOM();
That should resolve the issue. Method hasBOM() actually checks & removes any BOM character in the input stream. Name of the method is a bit confusing.
Now you can parse the xml response without any error.

Thursday, 17 September 2020

A Free Online Tool To Draw Sequence Diagram Writing Text

Some of us need to create sequence diagrams while doing architecture design of product or a new feature. There are several online websites out there which can be used to draw sequence diagrams. Some of them are free & some of them are paid services. They support different features. Somewhere you can actually draw, somewhere you can type in text & some might support both. Personally I prefer to write text. That is quick & easy. We just need to type in the interactions between objects & tool will draw the sequence diagram for us. WebSequenceDiagrams is such a online tool. Its free version supports almost all the things that you would need to create a sequence diagram.





Wednesday, 16 September 2020

How To Fix "unable to find valid certification path to requested target" Error

If you are here, that means probably you have encountered below error:

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
        at sun.security.validator.Validator.validate(Validator.java:260)
        at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
        ... 101 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:145)
        at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
        at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)


That happens when you try to connect to a https url from Java application & that remote server SSL certificate is not trusted by your JVM. Basically Java has a Truststore where it stores root certificates & intermediate certificates from Certified Authorities (CA). So in case the remote server is using a self-signed certificate or a certificate from not a well-known CA, there is a chance that the root & intermediate certificates of the CA are not present in your Java Truststore. You need to add it manually.

I found out a small Java program online that can easily help you with that. You can just check this link & follow the steps to add certificates to your Java Truststore. This program also validates if the certificates are already added & working properly.

But just make sure you do really know the remote server before adding their certificates to your Java Truststore.

Saturday, 5 September 2020

Draw Tree Or Graph Online Using Plain Text

Sometimes we need to draw a binary tree or graph online to explain related stuffs. I wanted to share a website where you can write your tree or graph definition in plain english & you will get a graphical representation of it. You will just have to define relations between the nodes in text, that's all. It supports both directed & undirected graphs. Basically this tool follows DOT language which is nothing but a standard set of instructions to define the relations between nodes. It even supports attributes for edges & nodes.
For an example, just look at the screenshot below. Is it not simple? And then you can download it as an image.


How To Solve XML Parsing Issue "Content is not allowed in prolog" In Java

If you have come to this post, then you are facing xml parsing issue in Java. Caused by: org.xml.sax.SAXParseException; lineNumber: 1; colum...