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

The following examples show how to use org.apache.cxf.staxutils.StaxUtils#writeTo() . 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: CustomizationParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCopyAllJaxbDeclarations() throws Exception {
    Element schema = getDocumentElement("resources/test.xsd");
    Element binding = getDocumentElement("resources/embeded_jaxb.xjb");

    String checkingPoint = "//xsd:annotation/xsd:appinfo/jaxb:schemaBindings/jaxb:package[@name]";
    assertNull(selector.queryNode(schema, checkingPoint));

    Node jaxwsBindingNode = selector.queryNode(binding, "//jaxws:bindings[@node]");
    Node schemaNode = selector.queryNode(schema, "//xsd:schema");

    parser.copyAllJaxbDeclarations(schemaNode, (Element)jaxwsBindingNode);

    File file = new File(output, "custom_test.xsd");
    StaxUtils.writeTo(schemaNode, new FileOutputStream(file));
    Document testNode = StaxUtils.read(file);

    Node result = selector.queryNode(testNode, checkingPoint);
    assertNotNull(result);
}
 
Example 2
Source File: CustomizationParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInternalizeBinding1() throws Exception {
    Element wsdlDoc = getDocumentElement("resources/test.wsdl");
    Element jaxwsBinding = getDocumentElement("resources/external_jaxws.xml");

    parser.setWSDLNode(wsdlDoc);
    parser.internalizeBinding(jaxwsBinding, wsdlDoc, "");

    File file = new File(output, "custom_test.wsdl");
    StaxUtils.writeTo(wsdlDoc, new FileOutputStream(file));
    Document testNode = StaxUtils.read(file);

    String[] checkingPoints =
        new String[]{"wsdl:definitions/wsdl:portType/jaxws:bindings/jaxws:class",
                     "wsdl:definitions/jaxws:bindings/jaxws:package"};
    checking(testNode, checkingPoints);
}
 
Example 3
Source File: CustomizationParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInternalizeBinding2() throws Exception {
    Element wsdlDoc = getDocumentElement("resources/test.wsdl");
    Element jaxwsBinding = getDocumentElement("resources/external_jaxws_embed_jaxb.xml");

    parser.setWSDLNode(wsdlDoc);
    parser.internalizeBinding(jaxwsBinding, wsdlDoc, "");

    String base = "wsdl:definitions/wsdl:types/xsd:schema/xsd:annotation/xsd:appinfo/";
    String[] checkingPoints =
        new String[]{base + "jaxb:schemaBindings/jaxb:package"};

    File file = new File(output, "custom_test.wsdl");
    StaxUtils.writeTo(wsdlDoc, new FileOutputStream(file));
    Document testNode = StaxUtils.read(file);

    checking(testNode, checkingPoints);
}
 
Example 4
Source File: CustomizationParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInternalizeBinding3() throws Exception {
    Element wsdlDoc = getDocumentElement("resources/test.wsdl");
    Element jaxwsBinding = getDocumentElement("resources/external_jaxws_embed_jaxb_date.xml");
    parser.setWSDLNode(wsdlDoc);
    parser.internalizeBinding(jaxwsBinding, wsdlDoc, "");

    String base = "wsdl:definitions/wsdl:types/xsd:schema/xsd:annotation/xsd:appinfo/";
    String[] checkingPoints =
        new String[]{base + "jaxb:globalBindings/jaxb:javaType"};

    File file = new File(output, "custom_test.wsdl");
    StaxUtils.writeTo(wsdlDoc, new FileOutputStream(file));
    Document testNode = StaxUtils.read(file);

    checking(testNode, checkingPoints);
}
 
Example 5
Source File: CustomizationParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInternalizeBinding4() throws Exception {
    Element wsdlDoc = getDocumentElement("resources/hello_world.wsdl");
    Element jaxwsBinding = getDocumentElement("resources/binding2.xml");
    parser.setWSDLNode(wsdlDoc);
    parser.internalizeBinding(jaxwsBinding, wsdlDoc, "");

    String checkingPoint = "wsdl:definitions/wsdl:types/xsd:schema";
    checkingPoint += "/xsd:element[@name='CreateProcess']/xsd:complexType/xsd:sequence";
    checkingPoint += "/xsd:element[@name='MyProcess']/xsd:simpleType/xsd:annotation/xsd:appinfo";
    checkingPoint += "/jaxb:typesafeEnumClass/jaxb:typesafeEnumMember[@name='BLUE']";

    String[] checkingPoints =
        new String[]{checkingPoint};

    File file = new File(output, "custom_test4.wsdl");
    StaxUtils.writeTo(wsdlDoc, new FileOutputStream(file));
    Document testNode = StaxUtils.read(file);

    checking(testNode, checkingPoints);
}
 
Example 6
Source File: WSDLCorbaWriterImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeWSDL(Definition wsdlDef, Writer sink) throws WSDLException {
    try {
        StaxUtils.writeTo(getDocument(wsdlDef), sink, 2);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: WSDLCorbaWriterImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeWSDL(Definition wsdlDef, OutputStream sink) throws WSDLException {
    try {
        StaxUtils.writeTo(getDocument(wsdlDef), sink, 2);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: SchemaWriterImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeWSDL(Definition wsdlDef, Writer sink) throws WSDLException {
    try {
        StaxUtils.writeTo(getDocument(wsdlDef), sink, 2);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
 
Example 9
Source File: SchemaWriterImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void writeWSDL(Definition wsdlDef, OutputStream sink) throws WSDLException {
    try {
        StaxUtils.writeTo(getDocument(wsdlDef), sink, 2);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Source File: HandlerConfigGenerator.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void generateHandlerChainFile(Element hChains, Writer writer)
    throws ToolException {

    try {
        StaxUtils.writeTo(hChains, writer, 2);
        writer.close();
    } catch (Exception ex) {
        throw new ToolException(ex);
    }
}
 
Example 11
Source File: JAXBUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Create the jaxb binding file to customize namespace to package mapping
 *
 * @param namespace
 * @param pkgName
 * @return file
 */
public static File getPackageMappingSchemaBindingFile(String namespace, String pkgName) {
    Document doc = DOMUtils.getEmptyDocument();
    Element rootElement = doc.createElementNS(ToolConstants.SCHEMA_URI, "schema");
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns", ToolConstants.SCHEMA_URI);
    rootElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:jaxb", ToolConstants.NS_JAXB_BINDINGS);
    rootElement.setAttributeNS(ToolConstants.NS_JAXB_BINDINGS, "jaxb:version", "2.0");
    rootElement.setAttributeNS(null, "targetNamespace", namespace);
    Element annoElement = doc.createElementNS(ToolConstants.SCHEMA_URI, "annotation");
    Element appInfo = doc.createElementNS(ToolConstants.SCHEMA_URI, "appinfo");
    Element schemaBindings = doc.createElementNS(ToolConstants.NS_JAXB_BINDINGS, "jaxb:schemaBindings");
    Element pkgElement = doc.createElementNS(ToolConstants.NS_JAXB_BINDINGS, "jaxb:package");
    pkgElement.setAttributeNS(null, "name", pkgName);
    annoElement.appendChild(appInfo);
    appInfo.appendChild(schemaBindings);
    schemaBindings.appendChild(pkgElement);
    rootElement.appendChild(annoElement);
    File tmpFile = null;
    try {
        tmpFile = FileUtils.createTempFile("customzied", ".xsd");
        try (OutputStream out = Files.newOutputStream(tmpFile.toPath())) {
            StaxUtils.writeTo(rootElement, out);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return tmpFile;
}
 
Example 12
Source File: SamlEnvelopedOutInterceptor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Document createEnvelopedSamlToken(Message message, Document payloadDoc)
    throws Exception {

    Element docEl = payloadDoc.getDocumentElement();
    SamlAssertionWrapper assertion = SAMLUtils.createAssertion(message);

    QName rootName = DOMUtils.getElementQName(payloadDoc.getDocumentElement());
    if (rootName.equals(envelopeQName)) {
        docEl.appendChild(assertion.toDOM(payloadDoc));
        return payloadDoc;
    }

    Document newDoc = DOMUtils.createDocument();

    Element root =
        newDoc.createElementNS(envelopeQName.getNamespaceURI(),
                envelopeQName.getPrefix() + ":" + envelopeQName.getLocalPart());
    newDoc.appendChild(root);

    Element assertionEl = assertion.toDOM(newDoc);
    root.appendChild(assertionEl);

    payloadDoc.removeChild(docEl);
    newDoc.adoptNode(docEl);
    root.appendChild(docEl);

    if (signLater) {
        // It appears adopting and removing nodes
        // leaves some stale refs/state with adopted nodes and thus the digest ends up
        // being wrong on the server side if XML sig is applied later in the enveloped mode
        // TODO: this is not critical now - but figure out if we can avoid copying
        // DOMs
        CachedOutputStream bos = new CachedOutputStream();
        StaxUtils.writeTo(newDoc, bos);
        return StaxUtils.read(bos.getInputStream());
    }
    return newDoc;
}
 
Example 13
Source File: JAXBEncoderDecoderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMarshallExceptionWithOrder() throws Exception {
    Document doc = DOMUtils.getEmptyDocument();
    Element elNode = doc.createElementNS("http://cxf.apache.org",  "ExceptionRoot");

    OrderException exception = new OrderException("Mymessage");
    exception.setAValue("avalue");
    exception.setDetail("detail");
    exception.setInfo1("info1");
    exception.setInfo2("info2");
    exception.setIntVal(10000);

    QName elName = new QName("http://cxf.apache.org", "OrderException");
    ServiceInfo serviceInfo = new ServiceInfo();
    InterfaceInfo interfaceInfo = new InterfaceInfo(serviceInfo, null);
    OperationInfo op = interfaceInfo.addOperation(new QName("http://cxf.apache.org", "operation"));
    MessageInfo message = new MessageInfo(op, null, null);
    MessagePartInfo part = new MessagePartInfo(elName, message);
    part.setElement(true);
    part.setElementQName(elName);
    part.setTypeClass(OrderException.class);

    //just need a simple generic context to handle the exceptions internal primitives
    JAXBContext exceptionContext = JAXBContext.newInstance(new Class[] {
        String.class,
    });
    JAXBEncoderDecoder.marshallException(exceptionContext.createMarshaller(), exception, part, elNode);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    StaxUtils.writeTo(elNode, bout);
    int a = bout.toString().lastIndexOf("aValue");
    int b = bout.toString().lastIndexOf("detail");
    int c = bout.toString().lastIndexOf("info1");
    int d = bout.toString().lastIndexOf("info2");
    int e = bout.toString().lastIndexOf("intVal");
    assertTrue(a < b);
    assertTrue(b < c);
    assertTrue(c < d);
    assertTrue(d < e);
    assertTrue(bout.toString().indexOf("transientValue") < 0);
    assertTrue(bout.toString(), bout.toString().indexOf("mappedField=\"MappedField\"") > 0);
}
 
Example 14
Source File: WSDLServiceBuilderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testImport() throws Exception {
    // rewrite the schema1.xsd to import schema2.xsd with absolute path.
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(this.getClass().getResourceAsStream("./s1/s2/schema2.xsd"));
    Element schemaImport = null;

    Node node = doc.getFirstChild();
    while (node != null) {
        if (node instanceof Element) {
            schemaImport = DOMUtils.getFirstElement(node);
        }
        node = node.getNextSibling();
    }

    if (schemaImport == null) {
        fail("Can't find import element");
    }
    String filePath = this.getClass().getResource("./s1/s2/s4/schema4.xsd").toURI().getPath();
    String importPath = schemaImport.getAttributeNode("schemaLocation").getValue();
    if (!new URI(URLEncoder.encode(importPath, "utf-8")).isAbsolute()) {
        schemaImport.getAttributeNode("schemaLocation").setNodeValue("file:" + filePath);
        String fileStr = this.getClass().getResource("./s1/s2/schema2.xsd").toURI().getPath();
        fileStr = URLDecoder.decode(fileStr, "utf-8");
        File file = new File(fileStr);
        if (file.exists()) {
            file.delete();
        }
        FileOutputStream fout = new FileOutputStream(file);
        StaxUtils.writeTo(doc, fout);
        fout.flush();
        fout.close();
    }
    setUpWSDL(IMPORT_WSDL_PATH, 0);
    assertNotNull(serviceInfo.getSchemas());
    Element ele = serviceInfo.getSchemas().iterator().next().getElement();
    assertNotNull(ele);
    Schema schema = EndpointReferenceUtils.getSchema(serviceInfo, null);
    assertNotNull(schema);
    control.verify();
}