1

I am trying to send marshalled jaxb data from a gwt server, and read it at reception in a servlet run by jboss.

This is the code in the sending part:

url = new URL(MessageFormat.format(urlStr,ip));
conn = url.openConnection();
conn.setReadTimeout(ApplicationSettings.instance.m_nUrlReadTimeout_ms);
conn.setConnectTimeout(ApplicationSettings.instance.m_nUrlConnectTimeout_ms);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter outStream = new OutputStreamWriter(  conn.getOutputStream());            

Marshaller tmarshaller;
JAXBContext tJAXB= JAXBContext.newInstance(path.to.application.jaxb.componentCurrentKPIs.ComponentCurrentKPIs.class);
tmarshaller = tJAXB.createMarshaller();
tmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
tmarshaller.marshal(new JAXBElement<ComponentCurrentKPIs>(new QName("componentCurrentKPIs"), ComponentCurrentKPIs.class, currentTableChanges), outStream);          
outStream.flush();

BufferedReader reader = new BufferedReader(new   InputStreamReader(conn.getInputStream()));
String line  = reader.readLine();
reader.close();
outStream.close();

In eclipse debugging the poster output stream is as following

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<componentCurrentKPIs command="http://{0}:8080/monserver/SetCurrentKpis/?user=xxxx&amp;password=xxxx">
    <CurrentKPI value="interface0:interface1" name="Interface"/>
    <CurrentKPI value="false:false" name="Enable"/>
    <CurrentKPI value="interface:interface1" name="PassThroughPort"/>
</componentCurrentKPIs>

This is the receiving part

public void processRequest(HttpServletRequest pRequest, HttpServletResponse pResponse)throws  IOException
{
    //To avoid new request while a request is processed
    synchronized (lock)
    {
           try
           {
               JAXBContext tJAXB = JAXBContext.newInstance("path.to.class.supervisor.jaxb.componentCurrentKPIs");
               tJAXB = JAXBContext.newInstance(ComponentCurrentKPIs.class);
               Unmarshaller tUnmarshaller = tJAXB.createUnmarshaller();
               JAXBElement model = null;


               BufferedReader rd = new BufferedReader(new InputStreamReader(pRequest.getInputStream()));
               model = (JAXBElement)tUnmarshaller.unmarshal(rd);

               ComponentCurrentKPIs currentKpis = (ComponentCurrentKPIs) model.getValue();

               for(CurrentKPI kpi : currentKpis.getCurrentKPI())
               {
                    Logger.getLogger(SetCurrentKpis.class).debug("in Set Current Kpis  !" + kpi.getName() + " = " +  kpi.getValue());
               }

           } catch (Exception e)
           {
               Logger.getLogger(SetCurrentKpis.class).error("Exception !" ,e);
           }
                    PrintWriter out = pResponse.getWriter();

                    out.println("aaaaaaaaaaaaaaaaa");
            }
        }


}

and i get exception in the servlet(receiving part)

18-06-2013 18:40:22  ERROR  131       path.to.servlets.SetCurrentKpis  -  Exception !
javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]
            at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
            at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:481)
            at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
            at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:172)
            at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
            at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:194)
            at path.to.applicationName.servlets.SetCurrentKpis.processRequest(SetCurrentKpis.java:120)
            at path.to.applicationName.servlets.infrastructure.AbstractOperationServlet.doPost(AbstractOperationServlet.java:36)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at path.to.applicationName.servlets.LoginFilter.doFilter(LoginFilter.java:86)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
            at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
            at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
            at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
            at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
            at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
            at java.lang.Thread.run(Thread.java:619)
Caused by: org.xml.sax.SAXParseException: Premature end of file.
            at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
            at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
            at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
            at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
            at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
            at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
            at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
            at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:199)
            ... 29 more

The information i send arrive to the servlet because when i print the headers and parameters of the http request i can see it.

connection : keep-alive
content-type : application/x-www-form-urlencoded
content-length : 353
user : xxxx
  amp;password : xxxx">
    <CurrentKPI value="interface0:interface1" name="Interface"/>
    <CurrentKPI value="false:false" name="Enable"/>
    <CurrentKPI value="interface0:interface1" name="PassThroughPort"/>
</componentCurrentKPIs>

  password : xxxx
  <?xml version : "1.0" encoding="UTF-8" standalone="yes"?>
<componentCurrentKPIs command="http://{0}:8080/monserver/SetCurrentKpis/?user=xxxx
  blablanull
yaron
  • 439
  • 6
  • 16

1 Answers1

1

I found the problems:

  1. I needed to add content-type definition:

    conn.setRequestProperty("Content-Type", "text/html");

  2. I needed to @XmlRootElement in xsd generated code. I still didn't found how to generate it. But there seems to be a lot about this

  3. I had a casting error, so to learn the unmarshalled type,I printed the unmarshalled object class

    Object o = tUnmarshaller.unmarshal(rd); print o.getClass();

yaron
  • 439
  • 6
  • 16