Forum Stats

  • 3,733,367 Users
  • 2,246,748 Discussions
  • 7,856,668 Comments

Discussions

How can I change namespace prefix for JAX-RPC client request?

WP v.2
WP v.2 Member Posts: 240 Blue Ribbon

I'm creating a JAX-RPC client to invoke a RPC/encoded web service. The service was generated from a ColdFusion program and for some reason when the SOAP namespace prefix is anything but "soapenv" it returns text/html instead of text/xml. Currently the client is sending requests with the prefix "env" and I'd like to change it to "soapenv".

I created a type of javax.xml.rpc.handler.GenericHandler and attempted to do the follow:

<span class="lit" style="color: #800000;">@Override</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">public</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">boolean</span><span class="pln"> handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">MessageContext</span><span class="pln"> p1</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span>
<span class="typ" style="color: #2b91af;">     SOAPMessage</span><span class="pln"> msg </span><span class="pun">=</span><span class="pln"> </span><span class="pun">((</span><span class="typ" style="color: #2b91af;">SOAPMessageContext</span><span class="pun">)</span><span class="pln"> p1</span><span class="pun">).</span><span class="pln">getMessage</span><span class="pun">();</span><span class="pln">  </span>

<span class="pln">     </span><span class="kwd" style="color: #00008b;">try</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln">          </span><span class="typ" style="color: #2b91af;">SOAPPart</span><span class="pln"> part </span><span class="pun">=</span><span class="pln"> msg</span><span class="pun">.</span><span class="pln">getSOAPPart</span><span class="pun">();</span><span class="pln"> </span> <span class="pln">          </span><span class="typ" style="color: #2b91af;">SOAPEnvelope</span><span class="pln"> envelope </span><span class="pun">=</span><span class="pln"> part</span><span class="pun">.</span><span class="pln">getEnvelope</span><span class="pun">();</span><span class="pln">  </span> <span class="pln">          envelope</span><span class="pun">.</span><span class="pln">setPrefix</span><span class="pun">(</span><span class="str" style="color: #800000;">"soapenv"</span><span class="pun">);</span><span class="pln">  </span> <span class="pln">          msg</span><span class="pun">.</span><span class="pln">saveChanges</span><span class="pun">();</span><span class="pln">  </span> <span class="pln">     </span><span class="pun">}</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">catch</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">SOAPException</span><span class="pln"> ex</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> </span> <span class="pln">          </span><span class="com" style="color: gray;">// TODO</span><span class="pln"> </span> <span class="pln">          </span><span class="kwd" style="color: #00008b;">return</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">false</span><span class="pun">;</span><span class="pln"> </span> <span class="pln">     </span><span class="pun">}</span><span class="pln">  </span>
<span class="pln">     </span><span class="kwd" style="color: #00008b;">return</span><span class="pln"> </span><span class="kwd" style="color: #00008b;">true</span><span class="pun">;</span><span class="pln"> </span> <span class="pun">}</span>

However I get the following exception:

<span class="pln">java</span><span class="pun">.</span><span class="pln">rmi</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">RemoteException</span><span class="pun">:</span><span class="pln"> </span><span class="typ" style="color: #2b91af;">SOAPFaultException</span><span class="pln"> </span><span class="pun">-</span><span class="pln"> </span><span class="typ" style="color: #2b91af;">FaultCode</span><span class="pln"> </span><span class="pun">[{</span><span class="pln">http</span><span class="pun">:</span><span class="com" style="color: gray;">//schemas.xmlsoap.org/soap/envelope/}Server] FaultString [UNIMPLEMENTED</span><span class="pln"> </span><span class="pun">]</span><span class="pln"> </span><span class="typ" style="color: #2b91af;">FaultActor</span><span class="pln"> </span><span class="pun">[</span><span class="kwd" style="color: #00008b;">null</span><span class="pun">]</span><span class="pln"> </span><span class="typ" style="color: #2b91af;">Detail</span><span class="pln"> </span><span class="pun">[<</span><span class="pln">detail</span><span class="pun">><</span><span class="pln">bea_fault</span><span class="pun">:</span><span class="pln">stacktrace xmlns</span><span class="pun">:</span><span class="pln">bea_fault</span><span class="pun">=</span><span class="str" style="color: #800000;"><span>"</span><a class="jive-link-external-small" href="http://www.bea.com/servers/wls70/webservice/fault/1.0.0">http://www.bea.com/servers/wls70/webservice/fault/1.0.0</a><span>"</span></span><span class="pun">></span><span class="pln">java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">AssertionError</span><span class="pun">:</span><span class="pln"> UNIMPLEMENTED </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">xml</span><span class="pun">.</span><span class="pln">domimpl</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">NodeImpl</span><span class="pun">.</span><span class="pln">setPrefix</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">NodeImpl</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">173</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at test</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">MyHandler</span><span class="pun">.</span><span class="pln">handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">MyHandler</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">33</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">handler</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">JaxrpcHandlerChain</span><span class="pun">.</span><span class="pln">handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">JaxrpcHandlerChain</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">58</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">ws</span><span class="pun">.</span><span class="pln">dispatch</span><span class="pun">.</span><span class="pln">server</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">JaxrpcChainHandler</span><span class="pun">.</span><span class="pln">handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">JaxrpcChainHandler</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">102</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">handler</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">HandlerIterator</span><span class="pun">.</span><span class="pln">handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">HandlerIterator</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">141</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">handler</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">HandlerIterator</span><span class="pun">.</span><span class="pln">handleRequest</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">HandlerIterator</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">107</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">ws</span><span class="pun">.</span><span class="pln">dispatch</span><span class="pun">.</span><span class="pln">client</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">ClientDispatcher</span><span class="pun">.</span><span class="pln">dispatch</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">ClientDispatcher</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">132</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">ws</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">WsStub</span><span class="pun">.</span><span class="pln">invoke</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">WsStub</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">87</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at weblogic</span><span class="pun">.</span><span class="pln">wsee</span><span class="pun">.</span><span class="pln">jaxrpc</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">StubImpl</span><span class="pun">.</span><span class="pln">_invoke</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">StubImpl</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">341</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at test</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_Wrap_Stub</span><span class="pun">.</span><span class="pln">echo</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_Wrap_Stub</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">31</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at test</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_WrapPortClient</span><span class="pun">.</span><span class="pln">echo</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_WrapPortClient</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">130</span><span class="pun">)</span><span class="pln"> </span>
<span class="pln">at test</span><span class="pun">.</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_WrapPortClient</span><span class="pun">.</span><span class="pln">main</span><span class="pun">(</span><span class="typ" style="color: #2b91af;">Approvedsuppliers_WrapPortClient</span><span class="pun">.</span><span class="pln">java</span><span class="pun">:</span><span class="lit" style="color: #800000;">43</span><span class="pun">)</span>

Is there any workaround? I appreciate any feedback.

Thanks, Bill

Tagged:

Best Answer

  • WP v.2
    WP v.2 Member Posts: 240 Blue Ribbon
    Accepted Answer

    What I ended up doing is converting the SOAP message to a string, replacing the namespace prefix and converting it back to a SOAP message.

    @Override
       public boolean handleRequest(MessageContext messageContext) {
          SOAPMessageContext soapMessageContext = (SOAPMessageContext) messageContext;
          SOAPMessage soapMessage = soapMessageContext.getMessage();
        
          String soapString = convertSOAPToString(soapMessage);
    
    
          soapString = soapString.replaceAll("env:", "soapenv:");
          soapString = soapString.replaceAll("xmlns:env", "xmlns:soapenv");
        
          SOAPMessage newSoapMessage = convertStringToSOAP(soapString);
        
          MimeHeaders mimeHeader = newSoapMessage.getMimeHeaders();
          mimeHeader.setHeader("SOAPAction", "");
        
          soapMessageContext.setMessage(newSoapMessage);
        
          return true;
       }
    
       private String convertSOAPToString(SOAPMessage soapMessage) {
          StringWriter stringWriter = null;
        
          try {
             stringWriter = new StringWriter();
             StreamResult streamResult = new StreamResult(stringWriter);
           
             TransformerFactory transformFactory = TransformerFactory.newInstance();
             Transformer transformer = transformFactory.newTransformer();
             transformer.transform(new DOMSource(soapMessage.getSOAPPart()), streamResult);
           
          } catch (TransformerException e) {
             throw new RuntimeException(e);
          } finally {
             close(stringWriter);
          }
        
          return stringWriter.toString();    
       }
    
       private SOAPMessage convertStringToSOAP(String soapString) {
          SOAPMessage soapMessage = null;
          ByteArrayInputStream byteInputStream = null;
        
          try {
             MessageFactory msgFactory = MessageFactory.newInstance();
             soapMessage = msgFactory.createMessage();
             SOAPPart soapPart = soapMessage.getSOAPPart();
           
             // Load the SOAP text into a stream source
             byte[] buffer = soapString.getBytes();
             byteInputStream = new ByteArrayInputStream(buffer);
           
             StreamSource source = new StreamSource(byteInputStream);
           
             // Set contents of message 
             soapPart.setContent(source);        
           
          } catch (SOAPException e) {
             throw new RuntimeException(e);
          } finally {
             close(byteInputStream);
          }   
        
          return soapMessage;
       }
    
       private void close(Closeable closeable) {
          if(closeable != null) {
             try {
                closeable.close();
             } catch (IOException e) {
                // TODO
             }
          }
       }
    

Answers

  • WP v.2
    WP v.2 Member Posts: 240 Blue Ribbon
  • WP v.2
    WP v.2 Member Posts: 240 Blue Ribbon
    Accepted Answer

    What I ended up doing is converting the SOAP message to a string, replacing the namespace prefix and converting it back to a SOAP message.

    @Override
       public boolean handleRequest(MessageContext messageContext) {
          SOAPMessageContext soapMessageContext = (SOAPMessageContext) messageContext;
          SOAPMessage soapMessage = soapMessageContext.getMessage();
        
          String soapString = convertSOAPToString(soapMessage);
    
    
          soapString = soapString.replaceAll("env:", "soapenv:");
          soapString = soapString.replaceAll("xmlns:env", "xmlns:soapenv");
        
          SOAPMessage newSoapMessage = convertStringToSOAP(soapString);
        
          MimeHeaders mimeHeader = newSoapMessage.getMimeHeaders();
          mimeHeader.setHeader("SOAPAction", "");
        
          soapMessageContext.setMessage(newSoapMessage);
        
          return true;
       }
    
       private String convertSOAPToString(SOAPMessage soapMessage) {
          StringWriter stringWriter = null;
        
          try {
             stringWriter = new StringWriter();
             StreamResult streamResult = new StreamResult(stringWriter);
           
             TransformerFactory transformFactory = TransformerFactory.newInstance();
             Transformer transformer = transformFactory.newTransformer();
             transformer.transform(new DOMSource(soapMessage.getSOAPPart()), streamResult);
           
          } catch (TransformerException e) {
             throw new RuntimeException(e);
          } finally {
             close(stringWriter);
          }
        
          return stringWriter.toString();    
       }
    
       private SOAPMessage convertStringToSOAP(String soapString) {
          SOAPMessage soapMessage = null;
          ByteArrayInputStream byteInputStream = null;
        
          try {
             MessageFactory msgFactory = MessageFactory.newInstance();
             soapMessage = msgFactory.createMessage();
             SOAPPart soapPart = soapMessage.getSOAPPart();
           
             // Load the SOAP text into a stream source
             byte[] buffer = soapString.getBytes();
             byteInputStream = new ByteArrayInputStream(buffer);
           
             StreamSource source = new StreamSource(byteInputStream);
           
             // Set contents of message 
             soapPart.setContent(source);        
           
          } catch (SOAPException e) {
             throw new RuntimeException(e);
          } finally {
             close(byteInputStream);
          }   
        
          return soapMessage;
       }
    
       private void close(Closeable closeable) {
          if(closeable != null) {
             try {
                closeable.close();
             } catch (IOException e) {
                // TODO
             }
          }
       }
    
This discussion has been closed.