Category: Exceptions

Converting base64 Decoded XML into Java Object

This program explains how to convert a base64 decoded XML data with namespace into Java Object along with few exceptions explained.


import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "xmlPojo",namespace="http://test.service.com")
public class XmlPojo {

 private String name;

 /**
 * @return the name
 */
 public String getName() {
 return name;
 }

 /**
 * @param name the name to set
 */
 @XmlElement(name="name")
 public void setName(String name) {
 this.name = name;
 }
}

Now we have main class to test the application.

 

import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class TeatMain {

 public static void main(String[] args) {

 try {
 String encodedXml= "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI/Pg0KPG5zMjp4bWxQb2pvDQoJeG1sbnM6bnMyPSJodHRwOi8vdGVzdC5zZXJ2aWNlLmNvbS8iPg0KPG5hbWU+bXlOYW1lPC9uYW1lPg0KPC9uczI6eG1sUG9qbz4=";
 byte[] asBytes = Base64.getDecoder().decode(encodedXml);

 String decodedXml = new String(asBytes, "utf-8");
 JAXBContext jaxbContext = JAXBContext.newInstance(XmlPojo.class);
 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

 XmlPojo rapidPerformance = (XmlPojo) unmarshaller.unmarshal(new StringReader(decodedXml));
 System.out
 .println(rapidPerformance.getName());
 } catch (UnsupportedEncodingException e) {
 } catch (JAXBException e) {
 }
 }
Output: myName
 
Sample xml file used in this program
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:xmlPojo
 xmlns:ns2="http://test.service.com/">
<name>myName</name>
</ns2:xmlPojo>

Key points to remember in this program,

  • when your xml has name space use namespace attribute in @XmlRootElement
  • while giving namespace in pojo make sure that it exactly matches in xml file ex: Here it ends with slash
  • If you forget the slash in the end of namespace we will get the below exception, I wrote this post because I spend half day find such issue.

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://test.service.com/", local:"xmlPojo"). Expected elements are <{http://test.service.com}xmlPojo>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:379)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:605)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3138)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:211)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
at com.test.TestMain(TestMain.java:37)

Invalid date in MYSQL causing problem in java application

Exception:
Caused by: java.sql.SQLException: Cannot convert value ‘0000-00-00 00:00:00’ from column 42 to TIMESTAMP.
Solution:
In hibernate data source mapping property please add the highlighted property
jdbc:mysql://yourserver:3306/yourdatabase?zeroDateTimeBehavior=convertToNull
 By default these invalid date will be converted into null values when it comes to Java object.
 Alternate options:

  • exception (the default), which throws an SQLException with an SQLState of S1009.
  • convertToNull, which returns NULL instead of the date.
  • round, which rounds the date to the nearest closest value which is 0001-01-01.

Proxy tunneling failed: ForbiddenUnable to establish SSL connection.

When ever your are tying to access a web-service URL using browser or curl or through elinks  command (In LINUX OS) which is hosted in different machine over intranet we may face this problem.
In our case we are trying to get the WSDL file using wget command,
Connecting to X.X.X.X:8085… connected. Proxy tunneling failed: ForbiddenUnable to establish SSL connection.
Where Y.Y.Y.Y stands for valid IP address and 7443 is port on which web-service is running .
We are getting “Proxy tunneling failed” exception, To overcome this issue we need to add the proxy server details as shown below in /etc/environment file.
Use vim /etc/environment  command and add the below entries for http_proxy and https_proxy with valid IP and port number in that file.
environment.
Ex :
http_proxy="http://X.X.X.X:8085/"
https_proxy="https://X.X.X.X:8085/"

The other way is you can use no-proxy in /etc/environment file which you can add the ip to be excluded from proxy server.

export no_proxy=”localhost,127.0.0.1,Y.Y.Y.Y”
Continue reading “Proxy tunneling failed: ForbiddenUnable to establish SSL connection.”

java.lang.ArrayIndexOutOfBoundsException: 63

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 63
at com.sun.xml.messaging.saaj.util.Base64.encode(Base64.java:180)

If  you are using sun Base64 encode and decode API, to encode/decode a string which has double codes, then you will get this error.

String str =”welcome\”welcome”;

Solution:

Don’t use sun’s Base64 encoding techinue , Go for  commons-codec’s encoding logic.

Maven dependency:

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>