public void addToSOAPBody(org.apache.axis.Message msg, XRoadProtocolHeader xRoadProtocolHeader) { try { // get SOAP envelope from SOAP message org.apache.axis.message.SOAPEnvelope se = msg.getSOAPEnvelope(); SOAPBody body = se.getBody(); @SuppressWarnings("rawtypes") Iterator items = body.getChildElements(); if (items.hasNext()) { body.removeContents(); } SOAPBodyElement element = body.addBodyElement( se.createName( getSendingOptionsResponseType.DEFAULT_RESPONSE_ELEMENT_NAME, CommonStructures.NS_DHL_PREFIX, CommonStructures.NS_DHL_URI)); if (xRoadProtocolHeader.getProtocolVersion().equals(XRoadProtocolVersion.V2_0)) { SOAPElement elParing = element.addChildElement(se.createName("paring")); elParing.addTextNode(this.dataMd5Hash); } // X-road "keha" part in SOAP message SOAPElement elKeha = element.addChildElement(se.createName("keha")); elKeha.addAttribute(se.createName("href"), "cid:" + kehaHref); } catch (Exception ex) { CommonMethods.logError(ex, this.getClass().getName(), "addToSOAPBody"); } }
// Copied and adapted this method from ORG.oclc.os.SRW.SRWServlet // ToDo: apply xslt logic. private String cleanup(Message message, String nameElementClose, Boolean cleanup) throws AxisFault { String soapResponse = message.getSOAPPartAsString(); int start, stop = soapResponse.indexOf(nameElementClose); if (stop == -1) { log.warn("Could not find closing element: " + nameElementClose); return soapResponse; } String nameElementOpen = nameElementClose.replace("</", "<").substring(0, nameElementClose.length() - 2); start = soapResponse.indexOf(nameElementOpen); stop += nameElementClose.length(); return (cleanup) ? cleanup(soapResponse.substring(start, stop).toCharArray()) : soapResponse.substring(start, stop); }
/** * Test that first signs, then encrypts a WS-Security envelope. * * <p> * * @throws Exception Thrown when there is any problem in signing, encryption, decryption, or * verification */ public void testEncryptedKeySignature() throws Exception { SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope(); LOG.info("Before Sign/Encryption...."); Document doc = unsignedEnvelope.getAsDocument(); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); WSSecEncryptedKey encrKey = new WSSecEncryptedKey(); encrKey.setKeyIdentifierType(WSConstants.ISSUER_SERIAL); encrKey.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security"); encrKey.setKeySize(192); encrKey.prepare(doc, crypto); WSSecEncrypt encrypt = new WSSecEncrypt(); encrypt.setEncKeyId(encrKey.getId()); encrypt.setEphemeralKey(encrKey.getEphemeralKey()); encrypt.setSymmetricEncAlgorithm(WSConstants.TRIPLE_DES); encrypt.setEncryptSymmKey(false); encrypt.setEncryptedKeyElement(encrKey.getEncryptedKeyElement()); WSSecSignature sign = new WSSecSignature(); sign.setKeyIdentifierType(WSConstants.CUSTOM_SYMM_SIGNING); sign.setCustomTokenId(encrKey.getId()); sign.setSecretKey(encrKey.getEphemeralKey()); sign.setSignatureAlgorithm(SignatureMethod.HMAC_SHA1); Document signedDoc = sign.build(doc, crypto, secHeader); Document encryptedSignedDoc = encrypt.build(signedDoc, crypto, secHeader); if (LOG.isDebugEnabled()) { LOG.debug("Signed and encrypted message with IssuerSerial key identifier (both), 3DES:"); String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(encryptedSignedDoc); LOG.debug(outputString); } LOG.info("After Sign/Encryption...."); verify(encryptedSignedDoc); }
/** * Constructs a soap envelope * * <p> * * @return soap envelope * @throws java.lang.Exception if there is any problem constructing the soap envelope */ protected SOAPEnvelope getSOAPEnvelope() throws Exception { InputStream in = new ByteArrayInputStream(SOAPMSG.getBytes()); Message msg = new Message(in); msg.setMessageContext(msgContext); return msg.getSOAPEnvelope(); }
@Override public void write( Writer writer, SolrQueryRequest solrQueryRequest, org.apache.solr.response.SolrQueryResponse solrQueryResponse) throws IOException { final MessageContext msgContext = (MessageContext) solrQueryResponse.getValues().get("MessageContext"); final Message message = msgContext.getResponseMessage(); if (message == null) return; SolrSRWDatabase.RequestTypes requestType = (SolrSRWDatabase.RequestTypes) msgContext.getProperty(SolrSRWDatabase.RequestTypes.class.getSimpleName()); String tag = (requestType == null) ? null : "</" + requestType.name().replace("Request", "Response") + ">"; final SolrSRWDatabase.Transport transport = (SolrSRWDatabase.Transport) msgContext.getProperty(SolrSRWDatabase.Transport.class.getSimpleName()); switch (transport) { case SRW: ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { message.writeTo(baos); } catch (SOAPException e) { log.error(e, e); // Impossible at this point. } writer.write(baos.toString("UTF-8")); break; case SRU: // Response which requires removal of SOAP envelope and xsi:type attributes. writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); Object resource = msgContext.getProperty("resource"); if (resource == null) // No system stylesheet... but sometimes people may add a custom one. { resource = msgContext.getProperty("stylesheet"); if (resource != null) { writer.write( "<?xml-stylesheet title=\"Custom XSL formatting\" type=\"text/xsl\" href=\""); writer.write(String.valueOf(resource)); writer.write("\"?>"); } } else { Object _stylesheet = msgContext.getProperty("stylesheet"); String stylesheet = (_stylesheet == null) ? String.valueOf(resource) : String.valueOf(_stylesheet); if (stylesheet.length() != 0) { writer.write( "<?xml-stylesheet title=\"OCLC XSL formatting\" type=\"text/xsl\" href=\""); writer.write(String.valueOf(resource)); writer.write("\"?>"); } } // We need to change the SOAP response into SRU writer.write(cleanup(message, tag, true)); break; case JSON: SolrSRWDatabase db = (SolrSRWDatabase) msgContext.getProperty("db"); String jsonp = (String) msgContext.getProperty("jsonp"); writer.write(jsonp); writer.write("("); Transformer t = db.getTransformers("xml-2-json"); StreamSource source = new StreamSource(new StringReader(cleanup(message, tag, true))); try { t.transform(source, new StreamResult(writer)); } catch (TransformerException e) { log.error(e, e); } writer.write(")"); break; } }