Beispiel #1
0
  /**
   * Get the content type of the attachments.
   *
   * @param sc provides the default content type
   * @return a <code>String</code> giving the content type of the attachment
   * @throws AxisFault if there was an error deducing the content type from this message
   */
  public String getContentType(SOAPConstants sc) throws AxisFault {
    boolean soap12 = false;

    if (sc != null) {
      if (sc == SOAPConstants.SOAP12_CONSTANTS) {
        soap12 = true;
      }
    } else {
      // Support of SOAP 1.2 HTTP binding
      SOAPEnvelope envelope = getSOAPEnvelope();
      if (envelope != null) {
        if (envelope.getSOAPConstants() == SOAPConstants.SOAP12_CONSTANTS) {
          soap12 = true;
        }
      }
    }

    String encoding = XMLUtils.getEncoding(this, msgContext);
    ;
    String ret = sc.getContentType() + "; charset=" + encoding.toLowerCase();

    // Support of SOAP 1.2 HTTP binding
    if (soap12) {
      ret = HTTPConstants.HEADER_ACCEPT_APPL_SOAP + "; charset=" + encoding;
    }

    if (getSendType() != Attachments.SEND_TYPE_NONE
        && mAttachments != null
        && 0 != mAttachments.getAttachmentCount()) {
      ret = mAttachments.getContentType();
    }
    return ret;
  }
 /**
  * Test T12 - unknown header, with MustUnderstand true
  *
  * @throws Exception
  */
 public void testT12() throws Exception {
   Call call = new Call(DOC_ENDPOINT);
   call.setOperationStyle(Style.DOCUMENT);
   call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
   SOAPEnvelope reqEnv = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
   SOAPHeaderElement header = new SOAPHeaderElement(TEST_NS, "Unknown");
   header.setObjectValue("test header");
   header.setMustUnderstand(true);
   reqEnv.addHeader(header);
   try {
     call.invoke(reqEnv);
   } catch (AxisFault fault) {
     assertEquals(Constants.FAULT_SOAP12_MUSTUNDERSTAND, fault.getFaultCode());
     ArrayList headers = fault.getHeaders();
     // If there is a NotUnderstood header, check it
     for (Iterator i = headers.iterator(); i.hasNext(); ) {
       SOAPHeaderElement h = (SOAPHeaderElement) i.next();
       if (h.getQName().equals(Constants.QNAME_NOTUNDERSTOOD)) {
         // TODO : check qname attribute
       }
     }
     return;
   }
   fail("Didn't receive expected fault!");
 }
Beispiel #3
0
  public void doTestData(boolean multiref) throws Exception {
    MessageContext msgContext = new MessageContext(new AxisServer());
    msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
    msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);

    SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
    RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");

    Data data = new Data();
    data.stringMember = "String member";
    data.floatMember = new Float("4.54");

    RPCParam arg2 = new RPCParam("", "struct", data);
    RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[] {arg1, arg2});
    msg.addBodyElement(body);

    Writer stringWriter = new StringWriter();
    SerializationContext context = new SerializationContext(stringWriter, msgContext);
    context.setDoMultiRefs(multiref);

    // Create a TypeMapping and register the specialized Type Mapping
    TypeMappingRegistry reg = context.getTypeMappingRegistry();
    TypeMapping tm = (TypeMapping) reg.createTypeMapping();
    tm.setSupportedEncodings(new String[] {Constants.URI_SOAP12_ENC});
    reg.register(Constants.URI_SOAP12_ENC, tm);

    QName dataQName = new QName("typeNS", "Data");
    tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());

    msg.output(context);

    String msgString = stringWriter.toString();

    log.debug("---");
    log.debug(msgString);
    log.debug("---");

    StringReader reader = new StringReader(msgString);

    DeserializationContext dser =
        new DeserializationContext(
            new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
    dser.parse();

    SOAPEnvelope env = dser.getEnvelope();
    RPCElement rpcElem = (RPCElement) env.getFirstBody();
    RPCParam struct = rpcElem.getParam("struct");
    assertNotNull("No <struct> param", struct);

    Data val = (Data) struct.getObjectValue();
    assertNotNull("No value for struct param", val);

    assertEquals("Data and Val string members are not equal", data.stringMember, val.stringMember);
    assertEquals(
        "Data and Val float members are not equal",
        data.floatMember.floatValue(),
        val.floatMember.floatValue(),
        0.00001F);
  }
 /**
  * Test T5 - echoOk header to unrecognized role (should be ignored)
  *
  * @throws Exception
  */
 public void testT5() throws Exception {
   Call call = new Call(DOC_ENDPOINT);
   call.setOperationStyle(Style.DOCUMENT);
   call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
   SOAPEnvelope reqEnv = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
   SOAPHeaderElement header = new SOAPHeaderElement(TEST_NS, "echoOk");
   header.setRole(ROLE_B);
   header.setObjectValue("test header");
   reqEnv.addHeader(header);
   SOAPEnvelope respEnv = call.invoke(reqEnv);
   assertTrue("Got unexpected header!", respEnv.getHeaders().isEmpty());
 }
Beispiel #5
0
  /** Test RPC element serialization when we have no MessageContext */
  public void testRPCElement() {
    try {
      SOAPEnvelope env = new SOAPEnvelope();
      RPCElement method = new RPCElement("ns", "method", new Object[] {"argument"});
      env.addBodyElement(method);
      String soapStr = env.toString();
    } catch (Exception e) {
      fail(e.getMessage());
    }

    // If there was no exception, we succeeded in serializing it.
  }
 /**
  * Test T6 - echoOk header targeted at endpoint via intermediary
  *
  * @throws Exception
  */
 public void testT6() throws Exception {
   Call call = new Call(INTERMEDIARY_ENDPOINT);
   call.setOperationStyle(Style.DOCUMENT);
   call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
   SOAPEnvelope reqEnv = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
   SOAPHeaderElement header = new SOAPHeaderElement(TEST_NS, "echoOk");
   header.setRole(ROLE_C);
   header.setObjectValue("test header");
   reqEnv.addHeader(header);
   SOAPEnvelope respEnv = call.invoke(reqEnv);
   SOAPHeaderElement respHeader = respEnv.getHeaderByName(TEST_NS, "responseOk");
   assertNotNull(respHeader);
   assertEquals("test header", respHeader.getValue());
 }
  /**
   * Test that encrypts using EncryptedKeySHA1, where it uses a symmetric key, rather than a
   * generated session key which is then encrypted using a public key. The request is generated
   * using WSHandler, instead of coding it.
   *
   * @throws java.lang.Exception Thrown when there is any problem in encryption or decryption
   */
  public void testEncryptionSHA1SymmetricBytesHandler() throws Exception {
    final WSSConfig cfg = WSSConfig.getNewInstance();
    final RequestData reqData = new RequestData();
    reqData.setWssConfig(cfg);
    java.util.Map messageContext = new java.util.TreeMap();
    messageContext.put(WSHandlerConstants.ENC_SYM_ENC_KEY, "false");
    messageContext.put(WSHandlerConstants.ENC_KEY_ID, "EncryptedKeySHA1");
    messageContext.put(WSHandlerConstants.PW_CALLBACK_REF, this);
    reqData.setMsgContext(messageContext);
    reqData.setUsername("");

    final java.util.Vector actions = new java.util.Vector();
    actions.add(new Integer(WSConstants.ENCR));

    Document doc = unsignedEnvelope.getAsDocument();
    MyHandler handler = new MyHandler();
    handler.send(WSConstants.ENCR, doc, reqData, actions, true);

    String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
    if (LOG.isDebugEnabled()) {
      LOG.debug(outputString);
    }

    verify(doc);
  }
Beispiel #8
0
 public void testNSStack2() throws Exception {
   String msg = prefix + m2 + suffix;
   StringReader strReader = new StringReader(msg);
   DeserializationContext dser =
       new DeserializationContext(
           new InputSource(strReader), null, org.apache.axis.Message.REQUEST);
   dser.parse();
   org.apache.axis.message.SOAPEnvelope env = dser.getEnvelope();
   String xml = env.toString();
   boolean oldIgnore = XMLUnit.getIgnoreWhitespace();
   XMLUnit.setIgnoreWhitespace(true);
   try {
     assertXMLIdentical("NSStack invalidated XML canonicalization", new Diff(msg, xml), true);
   } finally {
     XMLUnit.setIgnoreWhitespace(oldIgnore);
   }
 }
  /**
   * 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);
  }
  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");
    }
  }
Beispiel #11
0
  public void testEmptyXMLNS() {
    try {
      MessageContext msgContext = new MessageContext(new AxisServer());
      msgContext.setSOAPConstants(SOAPConstants.SOAP12_CONSTANTS);
      msgContext.setProperty(Constants.MC_NO_OPERATION_OK, Boolean.TRUE);

      String req =
          "<xsd1:A xmlns:xsd1=\"urn:myNamespace\">"
              + "<xsd1:B>"
              + "<xsd1:C>foo bar</xsd1:C>"
              + "</xsd1:B>"
              + "</xsd1:A>";

      StringWriter stringWriter = new StringWriter();
      StringReader reqReader = new StringReader(req);
      InputSource reqSource = new InputSource(reqReader);

      Document document = XMLUtils.newDocument(reqSource);

      String msgString = null;

      SOAPEnvelope msg = new SOAPEnvelope(SOAPConstants.SOAP12_CONSTANTS);
      RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", document.getFirstChild());
      arg1.setXSITypeGeneration(Boolean.FALSE);

      RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[] {arg1});
      msg.addBodyElement(body);
      body.setEncodingStyle(Constants.URI_LITERAL_ENC);

      SerializationContext context = new SerializationContext(stringWriter, msgContext);
      msg.output(context);

      msgString = stringWriter.toString();
      assertTrue(msgString.indexOf("xmlns=\"\"") == -1);
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
  /**
   * Test that signs (twice) and verifies a WS-Security envelope. The test uses the ThumbprintSHA1
   * key identifier type.
   *
   * <p>
   *
   * @throws java.lang.Exception Thrown when there is any problem in signing or verification
   */
  public void testDoubleX509SignatureThumb() throws Exception {
    WSSecSignature builder = new WSSecSignature();
    builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    // builder.setUserInfo("john", "keypass");
    builder.setKeyIdentifierType(WSConstants.THUMBPRINT_IDENTIFIER);
    Document doc = unsignedEnvelope.getAsDocument();

    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Document signedDoc = builder.build(doc, crypto, secHeader);
    Document signedDoc1 = builder.build(signedDoc, crypto, secHeader);
    verify(signedDoc1);
  }
  /**
   * Test that encrypts and decrypts a WS-Security envelope. The test uses the ThumbprintSHA1 key
   * identifier type.
   *
   * <p>
   *
   * @throws java.lang.Exception Thrown when there is any problem in encryption or decryption
   */
  public void testX509EncryptionThumb() throws Exception {
    WSSecEncrypt builder = new WSSecEncrypt();
    builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    builder.setKeyIdentifierType(WSConstants.THUMBPRINT_IDENTIFIER);

    LOG.info("Before Encrypting ThumbprintSHA1....");
    Document doc = unsignedEnvelope.getAsDocument();
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);
    Document encryptedDoc = builder.build(doc, crypto, secHeader);

    String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(encryptedDoc);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Encrypted message with THUMBPRINT_IDENTIFIER:");
      LOG.debug(outputString);
    }
    assertTrue(outputString.indexOf("#ThumbprintSHA1") != -1);

    LOG.info("After Encrypting ThumbprintSHA1....");
    verify(encryptedDoc);
  }
  /**
   * Test that signs and verifies a WS-Security envelope. The test uses the ThumbprintSHA1 key
   * identifier type.
   *
   * <p>
   *
   * @throws java.lang.Exception Thrown when there is any problem in signing or verification
   */
  public void testX509SignatureThumb() throws Exception {
    WSSecSignature builder = new WSSecSignature();
    builder.setUserInfo("16c73ab6-b892-458f-abf5-2f875f74882e", "security");
    builder.setKeyIdentifierType(WSConstants.THUMBPRINT_IDENTIFIER);
    // builder.setUserInfo("john", "keypass");
    LOG.info("Before Signing ThumbprintSHA1....");
    Document doc = unsignedEnvelope.getAsDocument();

    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Document signedDoc = builder.build(doc, crypto, secHeader);

    if (LOG.isDebugEnabled()) {
      LOG.debug("Signed message with ThumbprintSHA1 key identifier:");
      String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(signedDoc);
      LOG.debug(outputString);
    }
    LOG.info("After Signing ThumbprintSHA1....");
    verify(signedDoc);
  }
  /**
   * Test that encrypts using EncryptedKeySHA1, where it uses a symmetric key (bytes), rather than a
   * generated session key which is then encrypted using a public key.
   *
   * @throws java.lang.Exception Thrown when there is any problem in encryption or decryption
   */
  public void testEncryptionSHA1SymmetricBytes() throws Exception {
    WSSecEncrypt builder = new WSSecEncrypt();
    builder.setKeyIdentifierType(WSConstants.ENCRYPTED_KEY_SHA1_IDENTIFIER);
    builder.setEphemeralKey(keyData);
    builder.setEncryptSymmKey(false);
    builder.setUseKeyIdentifier(true);

    LOG.info("Before Encrypting EncryptedKeySHA1....");
    Document doc = unsignedEnvelope.getAsDocument();
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);
    Document encryptedDoc = builder.build(doc, crypto, secHeader);

    String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(encryptedDoc);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Encrypted message with ENCRYPTED_KEY_SHA1_IDENTIFIER:");
      LOG.debug(outputString);
    }
    assertTrue(outputString.indexOf("#EncryptedKeySHA1") != -1);

    LOG.info("After Encrypting EncryptedKeySHA1....");
    verify(encryptedDoc);
  }
Beispiel #16
0
 public void testEnvelope(SOAPEnvelope req, SOAPEnvelope resp) throws Exception {
   // Throw a header in and echo back.
   SOAPBodyElement body = req.getFirstBody();
   resp.addBodyElement(body);
   resp.addHeader(new SOAPHeaderElement("http://db.com", "local", "value"));
 }