List of usage examples for javax.xml.soap Name getLocalName
String getLocalName();
From source file:it.cnr.icar.eric.server.interfaces.soap.RegistryBSTServlet.java
public SOAPMessage onMessage(SOAPMessage msg, HttpServletRequest req, HttpServletResponse resp) { //System.err.println("onMessage called for RegistrySOAPServlet"); SOAPMessage soapResponse = null; SOAPHeader sh = null;/*from w w w . j a v a 2s . c o m*/ try { // set 'sh' variable ASAP (before "firstly") SOAPPart sp = msg.getSOAPPart(); SOAPEnvelope se = sp.getEnvelope(); SOAPBody sb = se.getBody(); sh = se.getHeader(); // Firstly we put save the attached repository items in a map HashMap<String, Object> idToRepositoryItemMap = new HashMap<String, Object>(); Iterator<?> apIter = msg.getAttachments(); while (apIter.hasNext()) { AttachmentPart ap = (AttachmentPart) apIter.next(); //Get the content for the attachment RepositoryItem ri = processIncomingAttachment(ap); idToRepositoryItemMap.put(ri.getId(), ri); } // Log received message //if (log.isTraceEnabled()) { // Warning! BAOS.toString() uses platform's default encoding /* ByteArrayOutputStream msgOs = new ByteArrayOutputStream(); msg.writeTo(msgOs); msgOs.close(); System.err.println(msgOs.toString()); */ //System.err.println(sb.getTextContent()); // log.trace("incoming message:\n" + msgOs.toString()); //} // verify signature // returns false if no security header, throws exception if invalid CredentialInfo credentialInfo = new CredentialInfo(); boolean noRegRequired = Boolean.valueOf( CommonProperties.getInstance().getProperty("eric.common.noUserRegistrationRequired", "false")) .booleanValue(); if (!noRegRequired) { WSS4JSecurityUtilBST.verifySOAPEnvelopeOnServerBST(se, credentialInfo); } //The ebXML registry request is the only element in the SOAPBody StringWriter requestXML = new StringWriter(); //The request as an XML String String requestRootElement = null; Iterator<?> iter = sb.getChildElements(); int i = 0; while (iter.hasNext()) { Object obj = iter.next(); if (!(obj instanceof SOAPElement)) { continue; } if (i++ == 0) { SOAPElement elem = (SOAPElement) obj; Name name = elem.getElementName(); requestRootElement = name.getLocalName(); StreamResult result = new StreamResult(requestXML); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(new DOMSource(elem), result); } else { throw new RegistryException( ServerResourceBundle.getInstance().getString("message.invalidRequest")); } } if (requestRootElement == null) { throw new RegistryException( ServerResourceBundle.getInstance().getString("message.noebXMLRegistryRequest")); } // unmarshalling request to message Object message = bu.getRequestObject(requestRootElement, requestXML.toString()); if (message instanceof JAXBElement<?>) { // If Element; take ComplexType from Element message = ((JAXBElement<?>) message).getValue(); } // request sets ServerContext with ComplexType: RegistryObjectType BSTRequest request = new BSTRequest(req, credentialInfo, message, idToRepositoryItemMap); Response response = request.process(); // response.getMessage() is ComplexType again soapResponse = createResponseSOAPMessage(response); if (response.getIdToRepositoryItemMap().size() > 0 && (response.getMessage().getStatus() .equals(BindingUtility.CANONICAL_RESPONSE_STATUS_TYPE_ID_Success))) { idToRepositoryItemMap = response.getIdToRepositoryItemMap(); Iterator<?> mapKeysIter = idToRepositoryItemMap.keySet().iterator(); while (mapKeysIter.hasNext()) { String id = (String) mapKeysIter.next(); RepositoryItem repositoryItem = (RepositoryItem) idToRepositoryItemMap.get(id); String cid = WSS4JSecurityUtilBST.convertUUIDToContentId(id); DataHandler dh = repositoryItem.getDataHandler(); AttachmentPart ap = soapResponse.createAttachmentPart(dh); ap.setMimeHeader("Content-Type", "text/xml"); ap.setContentId(cid); soapResponse.addAttachmentPart(ap); if (log.isTraceEnabled()) { log.trace("adding attachment: contentId=" + id); } } } } catch (Throwable t) { //Do not log ObjectNotFoundException as it clutters the log if (!(t instanceof ObjectNotFoundException)) { log.error(ServerResourceBundle.getInstance().getString("message.CaughtException", new Object[] { t.getMessage() }), t); Throwable cause = t.getCause(); while (cause != null) { log.error(ServerResourceBundle.getInstance().getString("message.CausedBy", new Object[] { cause.getMessage() }), cause); cause = cause.getCause(); } } soapResponse = createFaultSOAPMessage(t, sh); } if (log.isTraceEnabled()) { try { ByteArrayOutputStream rspOs = new ByteArrayOutputStream(); soapResponse.writeTo(rspOs); rspOs.close(); // Warning! BAOS.toString() uses platform's default encoding log.trace("response message:\n" + rspOs.toString()); } catch (Exception e) { log.error(ServerResourceBundle.getInstance().getString("message.FailedToLogResponseMessage", new Object[] { e.getMessage() }), e); } } return soapResponse; }
From source file:it.cnr.icar.eric.server.interfaces.soap.RegistryBSTServlet.java
private SOAPMessage createFaultSOAPMessage(java.lang.Throwable e, SOAPHeader sh) { SOAPMessage msg = null;// w w w . j ava 2s . c om if (log.isDebugEnabled()) { log.debug("Creating Fault SOAP Message with Throwable:", e); } try { // Will this method be "legacy" ebRS 3.0 spec-compliant and // return a URN as the <faultcode/> value? Default expectation // is of a an older client. Overridden to instead be SOAP // 1.1-compliant and return a QName as the faultcode value when // we know (for sure) client supports new approach. boolean legacyFaultCode = true; // get SOAPHeaderElement list from the received message // TODO: if additional capabilities are needed, move code to // elsewhere if (null != sh) { Iterator<?> headers = sh.examineAllHeaderElements(); while (headers.hasNext()) { Object obj = headers.next(); // confirm expected Iterator content if (obj instanceof SOAPHeaderElement) { SOAPHeaderElement header = (SOAPHeaderElement) obj; Name headerName = header.getElementName(); // check this SOAP header for relevant capability // signature if (headerName.getLocalName().equals(BindingUtility.SOAP_CAPABILITY_HEADER_LocalName) && headerName.getURI().equals(BindingUtility.SOAP_CAPABILITY_HEADER_Namespace) && header.getValue().equals(BindingUtility.SOAP_CAPABILITY_ModernFaultCodes)) { legacyFaultCode = false; // only interested in one client capability break; } } } } msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope env = msg.getSOAPPart().getEnvelope(); SOAPFault fault = msg.getSOAPBody().addFault(); // set faultCode String exceptionName = e.getClass().getName(); // TODO: SAAJ 1.3 has introduced preferred QName interfaces Name name = env.createName(exceptionName, "ns1", BindingUtility.SOAP_FAULT_PREFIX); fault.setFaultCode(name); if (legacyFaultCode) { // we now have an element child, munge its text (hack alert) Node faultCode = fault.getElementsByTagName("faultcode").item(0); // Using Utility.setTextContent() implementation since Java // WSDP 1.5 (containing an earlier DOM API) does not // support Node.setTextContent(). Utility.setTextContent(faultCode, BindingUtility.SOAP_FAULT_PREFIX + ":" + exceptionName); } // set faultString String errorMsg = e.getMessage(); if (errorMsg == null) { errorMsg = "NULL"; } fault.setFaultString(errorMsg); // create faultDetail with one entry Detail det = fault.addDetail(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); String str = sw.toString(); name = env.createName("StackTrace", "rs", "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0"); DetailEntry de = det.addDetailEntry(name); de.setValue(str); // de.addTextNode(str); // TODO: Need to put baseURL for this registry here msg.saveChanges(); } catch (SOAPException ex) { log.warn(ex, ex); // otherwise ignore the problem updating part of the message } return msg; }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test/*from w ww .j av a 2 s. co m*/ public void testGetElementNameWithoutNamespace() { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); Name name = element.getElementName(); assertEquals("", name.getURI()); assertEquals("test", name.getLocalName()); assertEquals("", name.getPrefix()); }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test// ww w. j a va 2s.co m public void testGetElementNameWithNamespace() { SOAPElement element = saajUtil.createSOAPElement("urn:test", "test", "p"); Name name = element.getElementName(); assertEquals("urn:test", name.getURI()); assertEquals("test", name.getLocalName()); assertEquals("p", name.getPrefix()); }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
@Validated @Test//from w w w.j ava 2s . c om public void testGetElementNameWithDefaultNamespace() { SOAPElement element = saajUtil.createSOAPElement("urn:test", "test", null); Name name = element.getElementName(); assertEquals("urn:test", name.getURI()); assertEquals("test", name.getLocalName()); assertEquals("", name.getPrefix()); }
From source file:it.cnr.icar.eric.server.interfaces.soap.RegistrySAMLServlet.java
@SuppressWarnings("unchecked") public SOAPMessage onMessage(SOAPMessage msg, HttpServletRequest req, HttpServletResponse resp) { SOAPMessage soapResponse = null; SOAPHeader soapHeader = null; try {/*w ww. j a v a 2 s . com*/ // extract SOAP related parts from incoming SOAP message // set 'soapHeader' variable ASAP (before "firstly") SOAPPart soapPart = msg.getSOAPPart(); SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); SOAPBody soapBody = soapEnvelope.getBody(); soapHeader = soapEnvelope.getHeader(); /**************************************************************** * * REPOSITORY ITEM HANDLING (IN) REPOSITORY ITEM HANDLING (IN) * ***************************************************************/ // Firstly we put save the attached repository items in a map HashMap<String, Object> idToRepositoryItemMap = new HashMap<String, Object>(); Iterator<AttachmentPart> apIter = msg.getAttachments(); while (apIter.hasNext()) { AttachmentPart ap = apIter.next(); // Get the content for the attachment RepositoryItem ri = processIncomingAttachment(ap); idToRepositoryItemMap.put(ri.getId(), ri); } /**************************************************************** * * LOGGING (IN) LOGGING (IN) LOGGING (IN) LOGGING (IN) * ***************************************************************/ // Log received message if (log.isTraceEnabled()) { // Warning! BAOS.toString() uses platform's default encoding ByteArrayOutputStream msgOs = new ByteArrayOutputStream(); msg.writeTo(msgOs); msgOs.close(); log.trace("incoming message:\n" + msgOs.toString()); } /**************************************************************** * * MESSAGE VERIFICATION (IN) MESSAGE VERIFICATION (IN) * ***************************************************************/ CredentialInfo credentialInfo = new CredentialInfo(); WSS4JSecurityUtilSAML.verifySOAPEnvelopeOnServerSAML(soapEnvelope, credentialInfo); /**************************************************************** * * RS:REQUEST HANDLING RS:REQUEST HANDLING RS:REQUEST * ***************************************************************/ // The ebXML registry request is the only element in the SOAPBody StringWriter requestXML = new StringWriter(); // The request as an // XML String String requestRootElement = null; Iterator<?> iter = soapBody.getChildElements(); int i = 0; while (iter.hasNext()) { Object obj = iter.next(); if (!(obj instanceof SOAPElement)) { continue; } if (i++ == 0) { SOAPElement elem = (SOAPElement) obj; Name name = elem.getElementName(); requestRootElement = name.getLocalName(); StreamResult result = new StreamResult(requestXML); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); trans.transform(new DOMSource(elem), result); } else { throw new RegistryException( ServerResourceBundle.getInstance().getString("message.invalidRequest")); } } if (requestRootElement == null) { throw new RegistryException( ServerResourceBundle.getInstance().getString("message.noebXMLRegistryRequest")); } // unmarshalling request to message Object message = bu.getRequestObject(requestRootElement, requestXML.toString()); if (message instanceof JAXBElement<?>) { // If Element; take ComplexType from Element message = ((JAXBElement<?>) message).getValue(); } SAMLRequest request = new SAMLRequest(req, credentialInfo.assertion, message, idToRepositoryItemMap); Response response = request.process(); /**************************************************************** * * RESPONSE HANDLING RESPONSE HANDLING RESPONSE HANDLING * ***************************************************************/ soapResponse = createResponseSOAPMessage(response); if (response.getIdToRepositoryItemMap().size() > 0 && (response.getMessage().getStatus() .equals(BindingUtility.CANONICAL_RESPONSE_STATUS_TYPE_ID_Success))) { idToRepositoryItemMap = response.getIdToRepositoryItemMap(); Iterator<String> mapKeysIter = idToRepositoryItemMap.keySet().iterator(); while (mapKeysIter.hasNext()) { String id = mapKeysIter.next(); RepositoryItem repositoryItem = (RepositoryItem) idToRepositoryItemMap.get(id); String cid = WSS4JSecurityUtilSAML.convertUUIDToContentId(id); DataHandler dh = repositoryItem.getDataHandler(); AttachmentPart ap = soapResponse.createAttachmentPart(dh); ap.setContentId(cid); soapResponse.addAttachmentPart(ap); if (log.isTraceEnabled()) { log.trace("adding attachment: contentId=" + id); } } } } catch (Throwable t) { // Do not log ObjectNotFoundException as it clutters the log if (!(t instanceof ObjectNotFoundException)) { log.error(ServerResourceBundle.getInstance().getString("message.CaughtException", new Object[] { t.getMessage() }), t); Throwable cause = t.getCause(); while (cause != null) { log.error(ServerResourceBundle.getInstance().getString("message.CausedBy", new Object[] { cause.getMessage() }), cause); cause = cause.getCause(); } } soapResponse = createFaultSOAPMessage(t, soapHeader); } if (log.isTraceEnabled()) { try { ByteArrayOutputStream rspOs = new ByteArrayOutputStream(); soapResponse.writeTo(rspOs); rspOs.close(); // Warning! BAOS.toString() uses platform's default encoding log.trace("response message:\n" + rspOs.toString()); } catch (Exception e) { log.error(ServerResourceBundle.getInstance().getString("message.FailedToLogResponseMessage", new Object[] { e.getMessage() }), e); } } return soapResponse; }
From source file:it.cnr.icar.eric.server.interfaces.soap.RegistrySAMLServlet.java
/** * This method is a copy of the respective method from RegistrySOAPServlet. * The SAML-based Servlet returns X.509 certificate base SOAP messages. * /*from w w w.j a va2 s . co m*/ */ private SOAPMessage createFaultSOAPMessage(java.lang.Throwable e, SOAPHeader sh) { SOAPMessage msg = null; if (log.isDebugEnabled()) { log.debug("Creating Fault SOAP Message with Throwable:", e); } try { // Will this method be "legacy" ebRS 3.0 spec-compliant and // return a URN as the <faultcode/> value? Default expectation // is of a an older client. Overridden to instead be SOAP // 1.1-compliant and return a QName as the faultcode value when // we know (for sure) client supports new approach. boolean legacyFaultCode = true; // get SOAPHeaderElement list from the received message // TODO: if additional capabilities are needed, move code to // elsewhere if (null != sh) { Iterator<?> headers = sh.examineAllHeaderElements(); while (headers.hasNext()) { Object obj = headers.next(); // confirm expected Iterator content if (obj instanceof SOAPHeaderElement) { SOAPHeaderElement header = (SOAPHeaderElement) obj; Name headerName = header.getElementName(); // check this SOAP header for relevant capability // signature if (headerName.getLocalName().equals(BindingUtility.SOAP_CAPABILITY_HEADER_LocalName) && headerName.getURI().equals(BindingUtility.SOAP_CAPABILITY_HEADER_Namespace) && header.getValue().equals(BindingUtility.SOAP_CAPABILITY_ModernFaultCodes)) { legacyFaultCode = false; // only interested in one client capability break; } } } } msg = MessageFactory.newInstance().createMessage(); SOAPEnvelope env = msg.getSOAPPart().getEnvelope(); SOAPFault fault = msg.getSOAPBody().addFault(); // set faultCode String exceptionName = e.getClass().getName(); // TODO: SAAJ 1.3 has introduced preferred QName interfaces Name name = env.createName(exceptionName, "ns1", BindingUtility.SOAP_FAULT_PREFIX); fault.setFaultCode(name); if (legacyFaultCode) { // we now have an element child, munge its text (hack alert) Node faultCode = fault.getElementsByTagName("faultcode").item(0); // Using Utility.setTextContent() implementation since Java // WSDP 1.5 (containing an earlier DOM API) does not // support Node.setTextContent(). Utility.setTextContent(faultCode, BindingUtility.SOAP_FAULT_PREFIX + ":" + exceptionName); } // set faultString String errorMsg = e.getMessage(); if (errorMsg == null) { errorMsg = "NULL"; } fault.setFaultString(errorMsg); // create faultDetail with one entry Detail det = fault.addDetail(); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); String str = sw.toString(); name = env.createName("StackTrace", "rs", "urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0"); DetailEntry de = det.addDetailEntry(name); de.setValue(str); // de.addTextNode(str); // TODO: Need to put baseURL for this registry here msg.saveChanges(); } catch (SOAPException ex) { log.warn(ex, ex); // otherwise ignore the problem updating part of the message } return msg; }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
/** * Test that {@link SOAPElement#getAllAttributes()} returns the correct {@link Name} for an * attribute with a namespace.//w ww . j a va 2s . c om */ @Validated @Test public void testGetAllAttributesWithNamespace() { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); element.setAttributeNS("urn:ns", "p:test", "value"); Iterator<?> it = element.getAllAttributes(); assertTrue(it.hasNext()); Name name = (Name) it.next(); assertEquals("urn:ns", name.getURI()); assertEquals("p", name.getPrefix()); assertEquals("test", name.getLocalName()); assertEquals("p:test", name.getQualifiedName()); }
From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java
/** * Test that {@link SOAPElement#getAllAttributes()} returns the correct {@link Name} for an * attribute without namespace.//w w w. j ava 2 s.co m */ @Validated @Test public void testGetAllAttributesWithoutNamespace() { SOAPElement element = saajUtil.createSOAPElement(null, "test", null); element.setAttributeNS(null, "test", "value"); Iterator<?> it = element.getAllAttributes(); assertTrue(it.hasNext()); Name name = (Name) it.next(); assertTrue(StringUtils.isEmpty(name.getURI())); assertTrue(StringUtils.isEmpty(name.getPrefix())); assertEquals("test", name.getLocalName()); assertEquals("test", name.getQualifiedName()); }
From source file:org.apache.axis.message.MessageElement.java
/** * construct using a {@link javax.xml.soap.Name} implementation, * @see #MessageElement(String, String, String) * @param eltName/*from w w w. jav a 2 s. c om*/ */ public MessageElement(Name eltName) { this(eltName.getLocalName(), eltName.getPrefix(), eltName.getURI()); }