@Test public void testDetails() throws Exception { MessageFactory mf = MessageFactory.newInstance(); SOAPMessage smsg = mf.createMessage(new MimeHeaders(), new ByteArrayInputStream(xmlString.getBytes())); SOAPBody body = smsg.getSOAPBody(); //smsg.writeTo(System.out); SOAPFault fault = body.getFault(); fault.addDetail(); javax.xml.soap.Detail d = fault.getDetail(); Iterator i = d.getDetailEntries(); while (i.hasNext()) { DetailEntry entry = (DetailEntry)i.next(); String name = entry.getElementName().getLocalName(); if ("tickerSymbol".equals(name)) { assertEquals("the value of the tickerSymbol element didn't match", "MACR", entry.getValue()); } else if ("exceptionName".equals(name)) { assertEquals("the value of the exceptionName element didn't match", "test.wsdl.faults.InvalidTickerFaultMessage", entry.getValue()); } else { assertTrue("Expecting details element name of 'tickerSymbol' or " + "'expceptionName' - I found :" + name, false); } } }
public void _testFaults2() throws Exception { SOAPEnvelope envelope = getSOAPEnvelope(); SOAPBody body = envelope.getBody(); SOAPFault fault = body.addFault(); assertTrue(body.getFault() != null); Detail d1 = fault.addDetail(); Name name = envelope.createName("GetLastTradePrice", "WOMBAT", "http://www.wombat.org/trader"); d1.addDetailEntry(name); Detail d2 = fault.getDetail(); assertTrue(d2 != null); Iterator i = d2.getDetailEntries(); assertTrue(getIteratorCount(i) == 1); i = d2.getDetailEntries(); while (i.hasNext()) { DetailEntry de = (DetailEntry)i.next(); assertEquals(de.getElementName(), name); } }
@Validated @Test public void testAddDetailEntry2() throws Exception { msg = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage(); sp = msg.getSOAPPart(); envelope = sp.getEnvelope(); body = envelope.getBody(); //Add a SOAPFault object to the SOAPBody SOAPFault sf = body.addFault(); //Add a Detail object to the SOAPFault object Detail d = sf.addDetail(); QName name = new QName("http://www.wombat.org/trader", "GetLastTradePrice", "WOMBAT"); //Add a DetailEntry object to the Detail object DetailEntry de = d.addDetailEntry(name); //Successfully created DetailEntry object assertNotNull(de); assertTrue(de instanceof DetailEntry); }
/** * It can either e within an extra tag, or directly. */ private Iterator detailChildrenIterator(Detail detail) { /* sb.append("<ns2:AccessDeniedWebServiceException xmlns:ns2=\"http://exceptionthrower.system.services.v4_0.soap.server.nameapi.org/\">"); sb.append("<blame>CLIENT</blame>"); sb.append("<errorCode>2101</errorCode>"); sb.append("<faultCause>AccessDenied</faultCause>"); */ DetailEntry firstDetailEntry = getFirstDetailEntry(detail); if (firstDetailEntry!=null) { String localName = firstDetailEntry.getElementName().getLocalName(); if (localName.endsWith("Exception")) { //got a subtag return firstDetailEntry.getChildElements(); } } return detail.getDetailEntries(); }
private static @Nullable QName getFirstDetailEntryName(@Nullable Detail detail) { if (detail != null) { Iterator<DetailEntry> it = detail.getDetailEntries(); if (it.hasNext()) { DetailEntry entry = it.next(); return getFirstDetailEntryName(entry); } } return null; }
/** * Adds a SOAP fault to the SOAP message of this response. * * @param cause the exception cause. * @return the SOAP fault which has been added to the SOAP message. null if * there is no SOAP message in this response or the fault has * already been added. * @throws SOAPException a SOAP error occurred when adding the the fault. */ public SOAPFault addFault(Throwable cause) throws SOAPException { if ((cause instanceof SOAPRequestException) && ((SOAPRequestException) cause).isSOAPFault()) { SOAPFaultException sfe = ((SOAPRequestException) cause) .getSOAPFault(); SOAPFault fault = addFault(sfe.getFaultCode(), sfe.getFaultActor(), sfe.getFaultString()); if (fault != null && sfe.hasDetailEntries()) { Detail detail = fault.addDetail(); Iterator detailEntries = sfe.getDetailEntryNames(); while (detailEntries.hasNext()) { Name entryName = (Name) detailEntries.next(); DetailEntry entry = detail.addDetailEntry(entryName); Object entryValue = sfe.getDetailEntryValue(entryName); if (entryValue instanceof SOAPElement) { entry.addChildElement((SOAPElement) entryValue); } else { entry.addTextNode(entryValue.toString()); } } } return fault; } else { return addFault(SOAPFaultException.SOAP_FAULT_SERVER, null, cause .toString()); } }
/** * Get SOAP Fault message from the information given. * * @return <code>SOAPMessage</code> object containing Fault element. */ public SOAPMessage getSOAPMessage() { try { final MessageFactory mf = MessageFactory.newInstance(); final SOAPMessage message = (SOAPMessage)mf.createMessage(); // set default SOAP XML declaration and encoding message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, Boolean.toString(EbxmlMessage.WRITE_XML_DECLARATION)); message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, EbxmlMessage.CHARACTER_SET_ENCODING); final SOAPPart part = message.getSOAPPart(); final SOAPEnvelope envelope = part.getEnvelope(); final SOAPBody body = envelope.getBody(); final SOAPFault fault = body.addFault(); fault.setFaultCode(errorCode); fault.setFaultString(errorString); if (faultActor != null) { fault.setFaultActor(faultActor); } if (detail != null) { Detail d = fault.addDetail(); Name name = envelope.createName(ELEMENT_ERROR, NAMESPACE_PREFIX_CECID, NAMESPACE_URI_CECID); DetailEntry entry = d.addDetailEntry(name); entry.addTextNode(detail); } return message; } catch (SOAPException e) { } return null; }
/** * Manually builds up a JPlagException SOAP message and replaces the * original one with it */ public void replaceByJPlagException(SOAPMessageContext smsg, String desc, String rep) { try { SOAPMessage msg = smsg.getMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); /* * Remove old header andy body */ SOAPHeader oldheader = envelope.getHeader(); if (oldheader != null) oldheader.detachNode(); SOAPBody oldbody = envelope.getBody(); if (oldbody != null) oldbody.detachNode(); SOAPBody sb = envelope.addBody(); SOAPFault sf = sb.addFault(envelope.createName("Server", "env", SOAPConstants.URI_NS_SOAP_ENVELOPE), "jplagWebService.server.JPlagException"); Detail detail = sf.addDetail(); DetailEntry de = detail.addDetailEntry(envelope.createName("JPlagException", "ns0", JPLAG_WEBSERVICE_BASE_URL + "types")); SOAPElement e = de.addChildElement("exceptionType"); e.addTextNode("accessException"); e = de.addChildElement("description"); e.addTextNode(desc); e = de.addChildElement("repair"); e.addTextNode(rep); } catch (SOAPException x) { x.printStackTrace(); } }
private SOAPFault newSOAPFault(String errorMsg) { try { MessageFactory mf = MessageFactory .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); SOAPMessage msg = mf.createMessage(); SOAPFault fault = msg.getSOAPBody().addFault(); QName faultName = new QName(SOAPConstants.URI_NS_SOAP_ENVELOPE, "Client"); fault.setFaultCode(faultName); fault.setFaultString(errorMsg); Detail detail = fault.addDetail(); QName entryName = new QName( "http://abhijitsarkar.name/webservices/jaxws/provider/", "error", "ns"); DetailEntry entry = detail.addDetailEntry(entryName); entry.addTextNode(errorMsg); return fault; } catch (Exception e) { throw new WebServiceException(e.getMessage(), e); } }
@Override public void OperationA() throws Exception { MessageFactory fac = MessageFactory.newInstance(); //Create a SOAPFault and throw it through SOAPFaultException SOAPMessage soapMessage = fac.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody body = envelope.getBody(); //Create a generic SOAPFault object SOAPFault soapFault = body.addFault(); soapFault.setFaultCode("Client"); soapFault.setFaultString("Generic fault"); //Add custom fault detail information Detail customFaultDetail = soapFault.addDetail(); Name customFaultElementName = envelope.createName("Fault", "sy", "http://fault.switchyard.org/fault"); DetailEntry customFaultElement = customFaultDetail.addDetailEntry(customFaultElementName); // Add a custom fault code element SOAPElement customFaultCodeElement = customFaultElement.addChildElement("FaultCode"); customFaultCodeElement.addTextNode("CUSTOMFAULTCODE"); // Add a custom fault message element with qualified name SOAPElement customQNamedFaultElement = customFaultElement.addChildElement(envelope.createName("FaultMessage", "sy", "http://fault.switchyard.org/fault")); customQNamedFaultElement.addTextNode("Custom fault message"); SOAPFaultException soapFaultException = new SOAPFaultException(soapFault); throw soapFaultException; }
private static Block[] getDetailBlocks(javax.xml.soap.SOAPFault soapFault) throws WebServiceException { try { Block[] blocks = null; Detail detail = soapFault.getDetail(); if (detail != null) { // Get a SAAJ->OM converter SAAJConverterFactory converterFactory = (SAAJConverterFactory)FactoryRegistry .getFactory(SAAJConverterFactory.class); SAAJConverter converter = converterFactory.getSAAJConverter(); // Create a block for each element OMBlockFactory bf = (OMBlockFactory)FactoryRegistry.getFactory(OMBlockFactory.class); ArrayList<Block> list = new ArrayList<Block>(); Iterator it = detail.getChildElements(); while (it.hasNext()) { DetailEntry de = (DetailEntry)it.next(); OMElement om = converter.toOM(de); Block b = bf.createFrom(om, null, om.getQName()); list.add(b); } blocks = new Block[list.size()]; blocks = list.toArray(blocks); } return blocks; } catch (Exception e) { throw ExceptionFactory.makeWebServiceException(e); } }
@Validated @Test public void testAddDetailEntry() throws Exception { //Add a SOAPFault object to the SOAPBody SOAPFault sf = body.addFault(); //Add a Detail object to the SOAPFault object Detail d = sf.addDetail(); QName name = new QName("http://www.wombat.org/trader", "GetLastTradePrice", "WOMBAT"); //Add a DetailEntry object to the Detail object DetailEntry de = d.addDetailEntry(name); assertNotNull(de); assertTrue(de instanceof DetailEntry); }
@Nullable private DetailEntry getFirstDetailEntry(@NotNull Detail detail) { Iterator detailEntries = detail.getDetailEntries(); while (detailEntries.hasNext()) { Object next = detailEntries.next(); if (next instanceof DetailEntry) { return (DetailEntry)next; } } return null; }
protected DetailEntry createDetailEntry(Name name) { return new DetailEntry1_1Impl( (SOAPDocumentImpl) getOwnerDocument(), name); }
protected DetailEntry createDetailEntry(QName name) { return new DetailEntry1_1Impl( (SOAPDocumentImpl) getOwnerDocument(), name); }
@Override protected DetailEntry createDetailEntry(Name name) { return new DetailEntry1_1Impl( (SOAPDocumentImpl) getOwnerDocument(), name); }
@Override protected DetailEntry createDetailEntry(QName name) { return new DetailEntry1_1Impl( (SOAPDocumentImpl) getOwnerDocument(), name); }
@Validated @Test public void testFaults() throws Exception { MessageFactory messageFactory = MessageFactory.newInstance(); SOAPFactory soapFactory = SOAPFactory.newInstance(); SOAPMessage message = messageFactory.createMessage(); SOAPBody body = message.getSOAPBody(); SOAPFault fault = body.addFault(); Name faultName = soapFactory.createName("Client", "", SOAPConstants.URI_NS_SOAP_ENVELOPE); fault.setFaultCode(faultName); fault.setFaultString("Message does not have necessary info"); fault.setFaultActor("http://gizmos.com/order"); Detail detail = fault.addDetail(); Name entryName = soapFactory.createName("order", "PO", "http://gizmos.com/orders/"); DetailEntry entry = detail.addDetailEntry(entryName); entry.addTextNode("Quantity element does not have a value"); Name entryName2 = soapFactory.createName("confirmation", "PO", "http://gizmos.com/confirm"); DetailEntry entry2 = detail.addDetailEntry(entryName2); entry2.addTextNode("Incomplete address: " + "no zip code"); message.saveChanges(); //message.writeTo(System.out); // Now retrieve the SOAPFault object and // its contents, after checking to see that // there is one if (body.hasFault()) { SOAPFault newFault = body.getFault(); // Get the qualified name of the fault code assertNotNull(newFault.getFaultCodeAsName()); assertNotNull(newFault.getFaultString()); assertNotNull(newFault.getFaultActor()); Detail newDetail = newFault.getDetail(); if (newDetail != null) { Iterator entries = newDetail.getDetailEntries(); while (entries.hasNext()) { DetailEntry newEntry = (DetailEntry)entries.next(); String value = newEntry.getValue(); assertNotNull(value); } } } }
/** * Creates a new <code>DetailEntry</code> object with the given name and adds it to this * <code>Detail</code> object. * * @param name a <code>Name</code> object identifying the new <code>DetailEntry</code> object * @return DetailEntry. * @throws SOAPException thrown when there is a problem in adding a DetailEntry object to this * Detail object. */ public DetailEntry addDetailEntry(Name name) throws SOAPException { SOAPElementImpl childElement = (SOAPElementImpl)addChildElement(name); DetailEntryImpl detailEntry = new DetailEntryImpl(childElement.element); childElement.element.setUserData(SAAJ_NODE, detailEntry, null); return detailEntry; }
/** * Creates a new DetailEntry object with the given name and adds it to this Detail object. * * @param name - a Name object identifying the new DetailEntry object * @throws SOAPException - thrown when there is a problem in adding a DetailEntry object to this * Detail object. */ public DetailEntry addDetailEntry(QName qname) throws SOAPException { SOAPElementImpl childElement = (SOAPElementImpl)addChildElement(qname); DetailEntryImpl detailEntry = new DetailEntryImpl(childElement.element); childElement.element.setUserData(SAAJ_NODE, detailEntry, null); return detailEntry; }