0 Replies Latest reply: Jul 1, 2011 6:54 AM by 872693 RSS

    Getting an Invalid or absent Content-Type exception with soap attachment

    872693
      Hi,

      I am trying to send an attachment using Saaj API. As soon as I use the attaachment object and attach it to message I get an exception . If I remove it the over all webservice communication works successfully. Even if I set the content type in the mimeheader explicetely it gives the same error. Any Suggestion how what could be wrong here or how can I fix it please ?

      ++com.inqmy.lib.jaxm.soap.SAPSoapException: javax.xml.soap.SOAPException: Invalid or absent Content-Type++
      ++     at com.inqmy.lib.jaxm.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:169)++
      ++     at com.philips.cms.Main.main(Main.java:261)++
      ++Caused by: javax.xml.soap.SOAPException: Invalid or absent Content-Type++
      ++     at com.inqmy.lib.jaxm.soap.SOAPMessageImpl.<init>(SOAPMessageImpl.java:69)++
      ++     at com.inqmy.lib.jaxm.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:40)++
      ++     at com.inqmy.lib.jaxm.soap.SOAPConnectionImpl.call(SOAPConnectionImpl.java:160)++
      ++     ... 1 more     ++

      Below is what the program looks like .

      public static void main(String[] args) {
                String nameSpace = "ns";
                String nameSpaceURI = "http://schemas.hp.com/SM/7";
                String comNameSpace = "com";
                String comNameSpaceURI = "http://schemas.hp.com/SM/7/Common";
                String xmNameSpace = "xm";
                String xmNameSpaceURI = "http://www.w3.org/2005/05/xmlmime";
                String endPoint = "http://GBRLONTSYSVW106.code1.emi.philips.com:13088/SM/7/ws";
                
                try {
                     
                          SOAPConnection soapConnection = SOAPConnectionFactory.newInstance().createConnection();
                          SOAPFactory soapFactory = SOAPFactory.newInstance();
                          MessageFactory messageFactory = MessageFactory.newInstance();
                     
                          // Create a message from the message factory.
                          SOAPMessage soapMessage = messageFactory.createMessage();

                     // start: setting HTTP headers - optional, comment out if not needed
                          String username = "BLAH";
                          String password = "****";

                          String authorization = new sun.misc.BASE64Encoder().encode((username+":"+password).getBytes());
                          MimeHeaders hd = soapMessage.getMimeHeaders();
                          hd.removeAllHeaders();
                          hd.addHeader("Authorization", "Basic " + authorization);
                          hd.addHeader("SOAPAction", "Create");
                          // creat a SOAP part have populate the envelope
                          SOAPPart soapPart = soapMessage.getSOAPPart();
                          SOAPEnvelope envelope = soapPart.getEnvelope();
                          envelope.addNamespaceDeclaration(nameSpace, nameSpaceURI);
                          envelope.addNamespaceDeclaration(comNameSpace, comNameSpaceURI);
                          envelope.addNamespaceDeclaration(xmNameSpace, xmNameSpaceURI);

                          // create a SOAP body
                          SOAPBody body = envelope.getBody();

                     
                          Name query = soapFactory.createName("query");
                          Name attachmentInfo = soapFactory.createName("attachmentInfo");
                          Name attachmentData = soapFactory.createName("attachmentData");

      ...

                          Name attachments = soapFactory.createName("attachments");
                          Name attachment = soapFactory.createName("attachment");
                          Name model = soapFactory.createName("model");
                          Name keys = soapFactory.createName("keys");
                
                          Name instance = soapFactory.createName("instance");
                     
                          Name cmsRequestName = envelope.createName( "CreateCaseRequest", nameSpace, nameSpaceURI);
                          SOAPElement soapRequest = body.addBodyElement(cmsRequestName);
                          soapRequest.addAttribute(attachmentInfo, "true");
                          soapRequest.addAttribute(attachmentData, "true");
                          soapRequest.addAttribute(ignoreEmptyElements, "true");
                          SOAPElement soapModel = soapRequest.addChildElement("model",nameSpace, nameSpaceURI);
                          soapModel.addAttribute(query,"?");
                          SOAPElement soapKeys = soapModel.addChildElement("keys",nameSpace, nameSpaceURI);
                          soapKeys.addAttribute(query,"?");
                          SOAPElement soapinstance = soapModel.addChildElement("instance",nameSpace, nameSpaceURI);
                          soapinstance.addAttribute(query,"?");
                          soapinstance.addAttribute(uniquequery,"?");     
                          soapinstance.addAttribute(recordid,"?");
                          SOAPElement soapDescription = soapinstance.addChildElement("Description",nameSpace, nameSpaceURI);
           
                          
                          SOAPElement soapDescriptionEle = soapDescription.addChildElement("Description",nameSpace, nameSpaceURI);
                          soapDescriptionEle.addTextNode("Test via Saaj");          
                          SOAPElement soapOwnerEle = soapinstance.addChildElement("Owner",nameSpace, nameSpaceURI);
                          soapOwnerEle.addTextNode("INTERFACE");
                          SOAPElement soapTechStatusEle = soapinstance.addChildElement("TechStatus",nameSpace, nameSpaceURI);

      ....

                     
                
                     
                          File file = new File("C:\\Bati\\Phillips\\test case.txt");
                          System.out.println(file.getAbsolutePath());
                          System.out.println(file.getName());
                          System.out.println(file.getPath());
                     
                          FileDataSource ds = new FileDataSource(file);
                          DataHandler dataHandler = new DataHandler(ds);
                     
                          AttachmentPart attachmentPart = soapMessage.createAttachmentPart(dataHandler);
                          attachmentPart.setContentType(dataHandler.getDataSource().getContentType());
                          attachmentPart.setContentId("test");
                          soapMessage.addAttachmentPart(attachmentPart);
                     
                          // set the saves into the structure
                          soapMessage.saveChanges();
                     
                          // output the message
                          System.out.println( "\n============= start request msg ==========\n" );
                          soapMessage.writeTo( System.out );
                          URLEndpoint urlEndpoint = new URLEndpoint(endPoint);
                          System.out.println( "\nSending message to URL: " + urlEndpoint.getURL() );
                          // now make that call over the SOAP connection
                          SOAPMessage reply = soapConnection.call( soapMessage, urlEndpoint );
                          // output the message reply
                          System.out.println( "\n============= start reply ==========\n" );
                          reply.writeTo( System.out );
                          System.out.println( "\n============= end reply ==========\n" );

                          // close down the connection
                          soapConnection.close();
                     
                } catch (Exception ex) {
                     ex.printStackTrace();
                }

      Thanks,
      Bati.