Java Code Examples for org.apache.cxf.staxutils.StaxUtils#createXMLStreamReader()

The following examples show how to use org.apache.cxf.staxutils.StaxUtils#createXMLStreamReader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformTestUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
static void transformOutStreamAndCompare(String inname, String outname,
                                       Map<String, String> transformElements,
                                       Map<String, String> appendElements,
                                       List<String> dropElements,
                                       Map<String, String> transformAttributes)
    throws XMLStreamException {

    XMLStreamReader reader = createOutTransformedStreamReader(inname,
                                                              transformElements, appendElements,
                                                              dropElements, transformAttributes,
                                                              false, null);
    XMLStreamReader teacher =
        StaxUtils.createXMLStreamReader(
                  TransformTestUtils.class.getResourceAsStream(outname));

    verifyReaders(teacher, reader, false, true);
}
 
Example 2
Source File: TransformTestUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
static void transformInStreamAndCompare(String inname, String outname,
                                       Map<String, String> transformElements,
                                       Map<String, String> appendElements,
                                       List<String> dropElements,
                                       Map<String, String> transformAttributes)
    throws XMLStreamException {

    XMLStreamReader reader = createInTransformedStreamReader(inname,
                                                             transformElements,
                                                             appendElements,
                                                             dropElements,
                                                             transformAttributes);
    XMLStreamReader teacher =
        StaxUtils.createXMLStreamReader(
                  TransformTestUtils.class.getResourceAsStream(outname));

    verifyReaders(teacher, reader, false, true);
}
 
Example 3
Source File: EnumTypeTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testType() throws Exception {
    EnumType type = new EnumType();
    type.setTypeClass(SmallEnum.class);
    type.setSchemaType(new QName("urn:test", "test"));

    tm.register(type);

    Element element = writeObjectToElement(type, SmallEnum.VALUE1, getContext());

    assertEquals("VALUE1", element.getTextContent());

    XMLStreamReader xreader = StaxUtils.createXMLStreamReader(element);
    ElementReader reader = new ElementReader(xreader);
    Object value = type.readObject(reader, getContext());

    assertEquals(SmallEnum.VALUE1, value);
}
 
Example 4
Source File: OutTransformWriterTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testReplaceDefaultNamespace() throws Exception {
    InputStream is = new ByteArrayInputStream(
            "<test xmlns=\"http://bar\"><a>1</a></test>".getBytes());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer =
        new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()),
                               null, null, null, null, false, "");
    StaxUtils.copy(new StreamSource(is), writer);
    writer.flush();

    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    StaxUtils.copy(reader, bos);
    String value = bos.toString();
    assertEquals("<ps1:test xmlns:ps1=\"http://bar\"><ps1:a>1</ps1:a></ps1:test>", value);
}
 
Example 5
Source File: TransformTestUtils.java    From cxf with Apache License 2.0 6 votes vote down vote up
static XMLStreamReader createOutTransformedStreamReader(
    String file,
    Map<String, String> emap,
    Map<String, String> append,
    List<String> dropEls,
    Map<String, String> amap,
    boolean attributesToElements,
    String defaultNamespace) throws XMLStreamException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer =
        new OutTransformWriter(StaxUtils.createXMLStreamWriter(os, StandardCharsets.UTF_8.name()),
                               emap, append, dropEls, amap, attributesToElements, defaultNamespace);
    StaxUtils.copy(new StreamSource(TransformTestUtils.class.getResourceAsStream(file)), writer);
    writer.flush();

    return StaxUtils.createXMLStreamReader(new ByteArrayInputStream(os.toByteArray()));
}
 
Example 6
Source File: EnumTypeTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNillable() throws Exception {
    AegisType type = tm.getTypeCreator().createType(EnumBean.class);

    tm.register(type);

    Element root = writeObjectToElement(type, new EnumBean(), getContext());
    ElementReader reader = new ElementReader(StaxUtils.createXMLStreamReader(root));
    Object value = type.readObject(reader, getContext());

    assertTrue(value instanceof EnumBean);
    EnumBean bean = (EnumBean)value;
    assertNull(bean.getCurrency());
}
 
Example 7
Source File: ColocUtil.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void convertSourceToObject(Message message) {
    List<Object> content = CastUtils.cast(message.getContent(List.class));
    if (content == null || content.isEmpty()) {
        // nothing to convert
        return;
    }
    // only supporting the wrapped style for now  (one pojo <-> one source)
    Source source = (Source)content.get(0);
    DataReader<XMLStreamReader> reader =
        message.getExchange().getService().getDataBinding().createReader(XMLStreamReader.class);
    MessagePartInfo mpi = getMessageInfo(message).getMessagePart(0);
    XMLStreamReader streamReader = null;
    Object wrappedObject = null;
    try {
        streamReader = StaxUtils.createXMLStreamReader(source);
        wrappedObject = reader.read(mpi, streamReader);
    } finally {
        try {
            StaxUtils.close(streamReader);
        } catch (XMLStreamException e) {
            // Ignore
        }
    }
    MessageContentsList parameters = new MessageContentsList();
    parameters.put(mpi, wrappedObject);

    message.setContent(List.class, parameters);
}
 
Example 8
Source File: InTransformReaderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPartialReadWithComplexTransformationNamespace() throws Exception {
    XMLStreamReader reader =
        StaxUtils.createXMLStreamReader(
                  InTransformReader.class.getResourceAsStream("../resources/complexReqIn3.xml"));

    Map<String, String> inMap = new HashMap<>();
    inMap.put("{http://cxf.apache.org/transform/header/element}*",
            "{http://cxf.apache.org/transform/header/otherelement}*");
    inMap.put("{http://cxf.apache.org/transform/test}*",
            "{http://cxf.apache.org/transform/othertest}*");
    inMap.put("{http://schemas.xmlsoap.org/soap/envelope/}Envelope",
            "{http://schemas.xmlsoap.org/soap/envelope/}TheEnvelope");

    // set the block original reader flag to true
    reader = new InTransformReader(reader,
                                   inMap, null, null,
                                   null, true);

    QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body");
    PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag);

    XMLStreamReader reader2 =
        StaxUtils.createXMLStreamReader(
              InTransformReader.class.getResourceAsStream("../resources/complexReq3partial.xml"));
    TransformTestUtils.verifyReaders(reader2, filteredReader, false, true);
}
 
Example 9
Source File: WSS4JInInterceptor.java    From steady with Apache License 2.0 5 votes vote down vote up
protected void advanceBody(
    SoapMessage msg, Node body
) throws SOAPException, XMLStreamException, WSSecurityException {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new DOMSource(body));
    // advance just past body
    int evt = reader.next();
    int i = 0;
    while (reader.hasNext() && i < 1
           && (evt != XMLStreamConstants.END_ELEMENT || evt != XMLStreamConstants.START_ELEMENT)) {
        reader.next();
        i++;
    }
    msg.setContent(XMLStreamReader.class, reader);
}
 
Example 10
Source File: StaxBuilder.java    From cxf with Apache License 2.0 5 votes vote down vote up
public Document build(InputStream is) throws XMLStreamException {
    isReadingMidStream = false;
    XMLStreamReader reader = null;
    try {
        reader = StaxUtils.createXMLStreamReader(is);
        return buildInternal(reader);
    } finally {
        StaxUtils.close(reader);
    }
}
 
Example 11
Source File: RPCOutInterceptorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteInbound() throws Exception {
    RPCOutInterceptor interceptor = new RPCOutInterceptor();
    soapMessage.setContent(XMLStreamWriter.class, XMLOutputFactory.newInstance().createXMLStreamWriter(
            baos));

    interceptor.handleMessage(soapMessage);
    assertNull(soapMessage.getContent(Exception.class));
    soapMessage.getContent(XMLStreamWriter.class).flush();
    baos.flush();

    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    XMLStreamReader xr = StaxUtils.createXMLStreamReader(bais);
    DepthXMLStreamReader reader = new DepthXMLStreamReader(xr);
    StaxUtils.toNextElement(reader);
    assertEquals(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"), reader
            .getName());

    StaxUtils.nextEvent(reader);
    StaxUtils.toNextElement(reader);

    assertEquals(new QName(null, "out"), reader.getName());

    StaxUtils.nextEvent(reader);
    StaxUtils.toNextElement(reader);

    assertEquals(new QName("http://apache.org/hello_world_rpclit/types", "elem1"), reader.getName());

    StaxUtils.nextEvent(reader);
    StaxUtils.toNextText(reader);
    assertEquals("elem1", reader.getText());
}
 
Example 12
Source File: SamlTokenTest.java    From steady with Apache License 2.0 4 votes vote down vote up
private SoapMessage makeInvocation(
    Map<String, Object> outProperties,
    List<String> xpaths,
    Map<String, Object> inProperties,
    Map<String, String> inMessageProperties
) throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);

    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    for (String key : outProperties.keySet()) {
        msg.put(key, outProperties.get(key));
    }

    handler.handleMessage(msg);

    doc = part;

    for (String xpath : xpaths) {
        assertValid(xpath, doc);
    }

    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(inProperties);

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    inmsg.put(SecurityConstants.SAML_ROLE_ATTRIBUTENAME, "role");
    for (String inMessageProperty : inMessageProperties.keySet()) {
        inmsg.put(inMessageProperty, inMessageProperties.get(inMessageProperty));
    }
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.handleMessage(inmsg);

    return inmsg;
}
 
Example 13
Source File: SoapFaultSerializerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSoap12Out() throws Exception {
    String faultString = "Hadrian caused this Fault!";
    SoapFault fault = new SoapFault(faultString, Soap12.getInstance().getSender());

    QName qname = new QName("http://cxf.apache.org/soap/fault", "invalidsoap", "cxffaultcode");
    fault.setSubCode(qname);

    SoapMessage m = new SoapMessage(new MessageImpl());
    m.setVersion(Soap12.getInstance());

    m.setContent(Exception.class, fault);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out);
    writer.writeStartDocument();
    writer.writeStartElement("Body");

    m.setContent(XMLStreamWriter.class, writer);

    Soap12FaultOutInterceptorInternal.INSTANCE.handleMessage(m);

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.close();

    Document faultDoc = StaxUtils.read(new ByteArrayInputStream(out.toByteArray()));

    assertValid("//soap12env:Fault/soap12env:Code/soap12env:Value[text()='ns1:Sender']",
                faultDoc);
    assertValid("//soap12env:Fault/soap12env:Code/soap12env:Subcode/"
                + "soap12env:Value[text()='cxffaultcode:invalidsoap']",
                faultDoc);
    assertValid("//soap12env:Fault/soap12env:Reason/soap12env:Text[@xml:lang='en']",
                faultDoc);
    assertValid("//soap12env:Fault/soap12env:Reason/soap12env:Text[text()='" + faultString + "']",
                faultDoc);

    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(out.toByteArray()));
    m.setContent(XMLStreamReader.class, reader);

    reader.nextTag();

    Soap12FaultInInterceptor inInterceptor = new Soap12FaultInInterceptor();
    inInterceptor.handleMessage(m);

    SoapFault fault2 = (SoapFault)m.getContent(Exception.class);
    assertNotNull(fault2);
    assertEquals(Soap12.getInstance().getSender(), fault2.getFaultCode());
    assertEquals(fault.getMessage(), fault2.getMessage());
    assertEquals(fault.getSubCode(), fault2.getSubCode());
}
 
Example 14
Source File: WSS4JFaultCodeTest.java    From steady with Apache License 2.0 4 votes vote down vote up
/**
 * Test that an invalid Timestamp gets mapped to a proper fault code 
 */
@Test
public void testInvalidTimestamp() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    
    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    msg.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    msg.put(WSHandlerConstants.TTL_TIMESTAMP, "1");

    handler.handleMessage(msg);

    doc = part;
    
    assertValid("//wsse:Security", doc);

    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    inHandler.setProperty(WSHandlerConstants.TTL_TIMESTAMP, "1");

    try {
        //
        // Sleep for over a second to make the timestamp invalid
        //
        Thread.sleep(1250);
        inHandler.handleMessage(inmsg);
        fail("Expected failure on an invalid Timestamp");
    } catch (SoapFault fault) {
        assertTrue(fault.getReason().startsWith(
            "The message has expired"));
        QName faultCode = new QName(WSConstants.WSSE_NS, "MessageExpired");
        assertTrue(fault.getFaultCode().equals(faultCode));
    }
}
 
Example 15
Source File: XMLMessageOutInterceptorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private XMLStreamReader getXMLReader() throws Exception {
    ByteArrayOutputStream o = (ByteArrayOutputStream) xmlMessage.getContent(OutputStream.class);
    writer.flush();
    InputStream in = new ByteArrayInputStream(o.toByteArray());
    return StaxUtils.createXMLStreamReader(in);
}
 
Example 16
Source File: WSS4JInOutTest.java    From steady with Apache License 2.0 4 votes vote down vote up
@Test
public void testCustomProcessor() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    
    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    msg.put(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
    msg.put(WSHandlerConstants.SIG_PROP_FILE, "outsecurity.properties");
    msg.put(WSHandlerConstants.USER, "myalias");
    msg.put("password", "myAliasPassword");

    handler.handleMessage(msg);

    doc = part;
    
    assertValid("//wsse:Security", doc);
    assertValid("//wsse:Security/ds:Signature", doc);

    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(
        WSS4JInInterceptor.PROCESSOR_MAP,
        createCustomProcessorMap()
    );
    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(properties);

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.NO_SECURITY);

    inHandler.handleMessage(inmsg);
    
    WSSecurityEngineResult result = 
        (WSSecurityEngineResult) inmsg.get(WSS4JInInterceptor.SIGNATURE_RESULT);
    assertNull(result);
}
 
Example 17
Source File: SignatureConfirmationTest.java    From steady with Apache License 2.0 4 votes vote down vote up
@org.junit.Test
public void testSignatureConfirmationRequest() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    
    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    msg.put(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
    msg.put(WSHandlerConstants.ENABLE_SIGNATURE_CONFIRMATION, "true");
    msg.put(WSHandlerConstants.SIG_PROP_FILE, "outsecurity.properties");
    msg.put(WSHandlerConstants.USER, "myalias");
    msg.put("password", "myAliasPassword");
    //
    // This is necessary to convince the WSS4JOutInterceptor that we're
    // functioning as a requestor
    //
    msg.put(org.apache.cxf.message.Message.REQUESTOR_ROLE, true);

    handler.handleMessage(msg);
    doc = part;
    
    assertValid("//wsse:Security", doc);
    assertValid("//wsse:Security/ds:Signature", doc);

    byte[] docbytes = getMessageBytes(doc);
    //
    // Save the signature for future confirmation
    //
    List<WSHandlerResult> sigv = CastUtils.cast((List<?>)msg.get(WSHandlerConstants.SEND_SIGV));
    assertNotNull(sigv);
    assertTrue(sigv.size() != 0);
    
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.SIGNATURE);
    inHandler.setProperty(WSHandlerConstants.SIG_PROP_FILE, "insecurity.properties");
    inHandler.setProperty(WSHandlerConstants.ENABLE_SIGNATURE_CONFIRMATION, "true");

    inHandler.handleMessage(inmsg);
    
    //
    // Check that the inbound signature result was saved
    //
    WSSecurityEngineResult result = 
        (WSSecurityEngineResult) inmsg.get(WSS4JInInterceptor.SIGNATURE_RESULT);
    assertNotNull(result);
    
    List<WSHandlerResult> sigReceived = 
        CastUtils.cast((List<?>)inmsg.get(WSHandlerConstants.RECV_RESULTS));
    assertNotNull(sigReceived);
    assertTrue(sigReceived.size() != 0);
    
    testSignatureConfirmationResponse(sigv, sigReceived);
}
 
Example 18
Source File: DocLiteralInInterceptorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testUnmarshalSourceData() throws Exception {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass()
        .getResourceAsStream("resources/multiPartDocLitBareReq.xml"));

    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());

    XMLStreamReader filteredReader = new PartialXMLStreamReader(reader,
         new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));

    // advance the xml reader to the message parts
    StaxUtils.read(filteredReader);
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());

    Message m = new MessageImpl();
    Exchange exchange = new ExchangeImpl();

    Service service = control.createMock(Service.class);
    exchange.put(Service.class, service);
    EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding());
    EasyMock.expect(service.size()).andReturn(0).anyTimes();
    EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();

    Endpoint endpoint = control.createMock(Endpoint.class);
    exchange.put(Endpoint.class, endpoint);

    OperationInfo operationInfo = new OperationInfo();
    operationInfo.setProperty("operation.is.synthetic", Boolean.TRUE);
    MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT,
                                              new QName("http://foo.com", "bar"));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo1"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo2"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo3"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName("http://foo.com", "partInfo4"), null));

    for (MessagePartInfo mpi : messageInfo.getMessageParts()) {
        mpi.setMessageContainer(messageInfo);
    }

    operationInfo.setInput("inputName", messageInfo);

    BindingOperationInfo boi = new BindingOperationInfo(null, operationInfo);
    exchange.put(BindingOperationInfo.class, boi);

    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    BindingInfo binding = control.createMock(BindingInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    EasyMock.expect(endpointInfo.getBinding()).andReturn(binding).anyTimes();
    EasyMock.expect(binding.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();

    ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
    EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();

    EasyMock.expect(serviceInfo.getName()).andReturn(new QName("http://foo.com", "service")).anyTimes();
    InterfaceInfo interfaceInfo = control.createMock(InterfaceInfo.class);
    EasyMock.expect(serviceInfo.getInterface()).andReturn(interfaceInfo).anyTimes();
    EasyMock.expect(interfaceInfo.getName())
        .andReturn(new QName("http://foo.com", "interface")).anyTimes();

    EasyMock.expect(endpointInfo.getName()).andReturn(new QName("http://foo.com", "endpoint")).anyTimes();
    EasyMock.expect(endpointInfo.getProperty("URI", URI.class)).andReturn(new URI("dummy")).anyTimes();

    List<OperationInfo> operations = new ArrayList<>();
    EasyMock.expect(interfaceInfo.getOperations()).andReturn(operations).anyTimes();

    m.setExchange(exchange);
    m.put(Message.SCHEMA_VALIDATION_ENABLED, false);
    m.setContent(XMLStreamReader.class, reader);

    control.replay();

    new DocLiteralInInterceptor().handleMessage(m);

    MessageContentsList params = (MessageContentsList)m.getContent(List.class);

    assertEquals(4, params.size());
    assertEquals("StringDefaultInputElem",
                 ((DOMSource)params.get(0)).getNode().getFirstChild().getNodeName());
    assertEquals("IntParamInElem",
                 ((DOMSource)params.get(1)).getNode().getFirstChild().getNodeName());
}
 
Example 19
Source File: SamlTokenTest.java    From steady with Apache License 2.0 4 votes vote down vote up
private SoapMessage makeInvocation(
    Map<String, Object> outProperties,
    List<String> xpaths,
    Map<String, Object> inProperties,
    Map<String, String> inMessageProperties
) throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);

    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    for (String key : outProperties.keySet()) {
        msg.put(key, outProperties.get(key));
    }

    handler.handleMessage(msg);

    doc = part;

    for (String xpath : xpaths) {
        assertValid(xpath, doc);
    }

    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(inProperties);

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    inmsg.put(SecurityConstants.SAML_ROLE_ATTRIBUTENAME, "role");
    for (String inMessageProperty : inMessageProperties.keySet()) {
        inmsg.put(inMessageProperty, inMessageProperties.get(inMessageProperty));
    }
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.handleMessage(inmsg);

    return inmsg;
}
 
Example 20
Source File: SignatureConfirmationTest.java    From steady with Apache License 2.0 4 votes vote down vote up
private void testSignatureConfirmationResponse(
    List<WSHandlerResult> sigSaved,
    List<WSHandlerResult> sigReceived
) throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");

    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();

    SoapMessage msg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    
    SOAPMessage saajMsg = MessageFactory.newInstance().createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    part.setContent(new DOMSource(doc));
    saajMsg.saveChanges();

    msg.setContent(SOAPMessage.class, saajMsg);

    msg.put(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    msg.put(WSHandlerConstants.RECV_RESULTS, sigReceived);
    
    handler.handleMessage(msg);

    doc = part;
    
    assertValid("//wsse:Security", doc);
    // assertValid("//wsse:Security/wsse11:SignatureConfirmation", doc);

    byte[] docbytes = getMessageBytes(doc);
    // System.out.println(new String(docbytes));
    
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);

    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);

    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();

    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);

    inHandler.setProperty(WSHandlerConstants.ACTION, WSHandlerConstants.TIMESTAMP);
    inmsg.put(WSHandlerConstants.SEND_SIGV, sigSaved);

    inHandler.handleMessage(inmsg);
}