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

徘徊边缘 提交于 2019-12-22 04:17:45

问题


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


回答1:


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




回答2:


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();
                }



回答3:


Wouldn't it be easier to just use?

SOAPHeader header = envelope.getHeader();



回答4:


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.



来源:https://stackoverflow.com/questions/17058752/severe-saaj0120-cant-add-a-header-when-one-is-already-present

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!