SEVERE: SAAJ0120: Can't add a header when one is already present
Asked Answered
S

4

9

I'm trying to deploy my jsp site on Oracle web server, but when I call my web service using a port I get the following error : SEVERE: SAAJ0120: Can't add a header when one is already present

This is the code for my HeaderHandler

            public boolean handleMessage(SOAPMessageContext smc) {

                    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

                    if (outboundProperty.booleanValue()) {


                        SOAPMessage message = smc.getMessage();
                        SOAPEnvelope envelope = null;
                        SOAPHeader header = null;

                        try {

                            envelope = smc.getMessage().getSOAPPart().getEnvelope();
                            header = envelope.addHeader();

                            SOAPElement security =
                                    header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                                                                                //http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd
                            SOAPElement BinarySecurityToken =
                                    security.addChildElement("BinarySecurityToken", "wsse");
                            BinarySecurityToken.addAttribute(new QName("xmlns:wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

                            //EncodingType="SSHA" ValueType="AccessManagerSSOSecurityToken" wsu:Id="OAMToken"

                            BinarySecurityToken.addAttribute(new QName("EncodingType"), "SSHA");
                            BinarySecurityToken.addAttribute(new QName("ValueType"), "AccessManagerSSOSecurityToken");
                            BinarySecurityToken.addAttribute(new QName("wsu:Id"), "OAMToken");

                            BinarySecurityToken.addTextNode(token);
                            //message.writeTo(System.out);
                            //System.out.println("");

                        } catch (Exception e) {
                            try {
                                header = envelope.addHeader();
                            } catch (SOAPException e1) {
                                e1.printStackTrace();
                            }
                            //e.printStackTrace();
                        }

                    } else {
                        try {

                            //This handler does nothing with the response from the Web Service so
                            //we just print out the SOAP message.
                            SOAPMessage message = smc.getMessage();
                            //message.writeTo(System.out);
                            //System.out.println("");

                        } catch (Exception ex) {
                            ex.printStackTrace();
                        } 
                    }


                    return outboundProperty;

                }

EDIT:

The error is

            com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl addHeader
            SEVERE: SAAJ0120: Can't add a header when one is already present
            com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Can't add a header when one is already present.
                   at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.addHeader(EnvelopeImpl.java:114)
                   at com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl.addHeader(EnvelopeImpl.java:94)
                   at com.standardandpoors.wso.HeaderHandler.handleMessage(HeaderHandler.java:33)
                   at com.standardandpoors.wso.HeaderHandler.handleMessage(HeaderHandler.java:1)
                   at com.sun.xml.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:297)
                   at com.sun.xml.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:138)
                   at com.sun.xml.ws.handler.ClientSOAPHandlerTube.callHandlersOnRequest(ClientSOAPHandlerTube.java:144)
                   at com.sun.xml.ws.handler.HandlerTube.processRequest(HandlerTube.java:115)
                   at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:892)
                   at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:841)
                   at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:804)
                   at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:706)
                   at com.sun.xml.ws.client.Stub.process(Stub.java:385)
                   at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:189)
                   at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:119)
                   at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:102)
                   at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:172)
                   at $Proxy125.findUserById(Unknown Source)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at weblogic.wsee.jaxws.spi.ClientInstanceInvocationHandler.invoke(ClientInstanceInvocationHandler.java:84)
                   at $Proxy120.findUserById(Unknown Source)
                   at com.standardandpoors.wso.IdmWSUtil.findUserById(IdmWSUtil.java:44)
                   at com.standardandpoors.idm.controller.JspRedirectionController.forgotPassword(JspRedirectionController.java:123)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:597)
                   at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:175)
                   at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
                   at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
                   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:774)
                   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
                   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
                   at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
                   at javax.servlet.http.HttpServlet.service(HttpServlet.java:751)
                   at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
                   at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:242)
                   at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:216)
                   at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:132)
                   at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:338)
                   at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:221)
                   at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3284)
                   at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3254)
                   at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
                   at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
                   at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
                   at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2163)
                   at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2089)
                   at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2074)
                   at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1513)
                   at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:254)
                   at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
                   at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)

EDIT: The issue is present using JDK 1.8, not in 1.7

Subshrub answered 12/6, 2013 at 6:13 Comment(4)
Where in your code is the exception occurring? Do you have additional information from the exception that you can provide?Fogle
@STLDeveloper The exception is at the line : header = envelope.addHeader(); I'll add the console output as an edit to the question.Subshrub
That's my mistake. I didn't see the dependency between the two lines. Are you missing something in your call to envelope.addHeader()?Fogle
I guess no. Because I run the same code as a Java application and it works as a charm. The problem comes when I export the code as a jar file and use it in a web application. -(this is user1927060)Cachalot
S
10

I was finally able to resolve the issue. I just added an if condition to check whether the header was NULL or not, if it was NULL only then I added the header else continued with the program. This worked for me !

if(header == null){
   header = envelope.addHeader();
}
Subshrub answered 18/6, 2013 at 7:6 Comment(2)
Did you find why it runs ok in Java 7 and not in Java 8?Exquisite
I did. It works with Java 7 and 8. Tested today!Parang
N
28

You can replace the header by detaching the old one first.

  if (envelope.getHeader() != null) {
     envelope.getHeader().detachNode();
  }
  SOAPHeader header = envelope.addHeader();

http://docs.oracle.com/javaee/5/api/javax/xml/soap/SOAPEnvelope.html

Notogaea answered 27/3, 2014 at 7:40 Comment(1)
OK works, same problem on JDK 8, detach the node before add header resolve the . com.sun.xml.internal.messaging.saaj.soap.impl.EnvelopeImpl addHeader SEVERE: SAAJ0120: Can't add a header when one is already presentAccordance
S
10

I was finally able to resolve the issue. I just added an if condition to check whether the header was NULL or not, if it was NULL only then I added the header else continued with the program. This worked for me !

if(header == null){
   header = envelope.addHeader();
}
Subshrub answered 18/6, 2013 at 7:6 Comment(2)
Did you find why it runs ok in Java 7 and not in Java 8?Exquisite
I did. It works with Java 7 and 8. Tested today!Parang
G
2

Wouldn't it be easier to just use?

SOAPHeader header = envelope.getHeader();
Ghent answered 18/4, 2018 at 7:9 Comment(0)
M
2
SOAPHeader header = envelope.getHeader();
if (header == null) {
   header = add.addHeader();
}
header.addChildElement(wsSecurityElement);

Is my personal choice: detaching the previous SOAPHeader can break the code, for instance in case of handlers chaining, where other upstream handlers might add their elements to the header. null testing makes the code backward compatible with previous JREs; otherwise switching from addHeader to getHeader is enough.

Mozellemozes answered 28/6, 2018 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.