Java Code Examples for org.apache.cxf.helpers.DOMUtils#getEmptyDocument()

The following examples show how to use org.apache.cxf.helpers.DOMUtils#getEmptyDocument() . 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: IssueSamlUnitTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private UseKeyType createUseKey(Crypto crypto, String alias) throws Exception {
    CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
    cryptoType.setAlias(alias);
    X509Certificate[] certs = crypto.getX509Certificates(cryptoType);
    Document doc = DOMUtils.getEmptyDocument();
    Element x509Data = doc.createElementNS(WSS4JConstants.SIG_NS, "ds:X509Data");
    x509Data.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:ds", WSS4JConstants.SIG_NS);
    Element x509Cert = doc.createElementNS(WSS4JConstants.SIG_NS, "ds:X509Certificate");
    Text certText = doc.createTextNode(Base64.getMimeEncoder().encodeToString(certs[0].getEncoded()));
    x509Cert.appendChild(certText);
    x509Data.appendChild(x509Cert);

    UseKeyType useKey = new UseKeyType();
    useKey.setAny(x509Data);

    return useKey;
}
 
Example 2
Source File: AbstractTokenInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected Header findSecurityHeader(SoapMessage message, boolean create) {
    for (Header h : message.getHeaders()) {
        QName n = h.getName();
        if ("Security".equals(n.getLocalPart())
            && (n.getNamespaceURI().equals(WSS4JConstants.WSSE_NS)
                || n.getNamespaceURI().equals(WSS4JConstants.WSSE11_NS))) {
            return h;
        }
    }
    if (!create) {
        return null;
    }
    Document doc = DOMUtils.getEmptyDocument();
    Element el = doc.createElementNS(WSS4JConstants.WSSE_NS, "wsse:Security");
    el.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsse", WSS4JConstants.WSSE_NS);
    SoapHeader sh = new SoapHeader(new QName(WSS4JConstants.WSSE_NS, "Security"), el);
    sh.setMustUnderstand(true);
    message.getHeaders().add(sh);
    return sh;
}
 
Example 3
Source File: FragmentDialect.java    From cxf with Apache License 2.0 6 votes vote down vote up
/**
 * Generates Value element from Node.
 * @param node
 * @return
 */
private JAXBElement<ValueType> generateGetResponseNode(Node node) {
    Document doc = DOMUtils.getEmptyDocument();
    ValueType resultValue = new ValueType();
    if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        Element attrNodeEl = doc.createElementNS(
                FragmentDialectConstants.FRAGMENT_2011_03_IRI,
                FragmentDialectConstants.FRAGMENT_ATTR_NODE_NAME);
        attrNodeEl.setAttribute(
                FragmentDialectConstants.FRAGMENT_ATTR_NODE_NAME_ATTR,
                node.getNodeName()
        );
        attrNodeEl.setTextContent(node.getNodeValue());
        resultValue.getContent().add(attrNodeEl);
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        Element textNodeEl = doc.createElementNS(
                FragmentDialectConstants.FRAGMENT_2011_03_IRI,
                FragmentDialectConstants.FRAGMENT_TEXT_NODE_NAME);
        textNodeEl.setNodeValue(node.getNodeValue());
        resultValue.getContent().add(textNodeEl);
    } else if (node.getNodeType() == Node.ELEMENT_NODE) {
        resultValue.getContent().add(node);
    }
    ObjectFactory objectFactory = new ObjectFactory();
    return objectFactory.createValue(resultValue);
}
 
Example 4
Source File: CallbackHandlerLoginHandler.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
public UserSubject createSubject(Client client, String user, String pass) {
    Document doc = DOMUtils.getEmptyDocument();
    UsernameToken token = new UsernameToken(false, doc,
                                            WSS4JConstants.PASSWORD_TEXT);
    token.setName(user);
    token.setPassword(pass);

    Credential credential = new Credential();
    credential.setUsernametoken(token);

    RequestData data = new RequestData();
    data.setMsgContext(PhaseInterceptorChain.getCurrentMessage());
    data.setCallbackHandler(callbackHandler);
    UsernameTokenValidator validator = new UsernameTokenValidator();

    try {
        credential = validator.validate(credential, data);

        UserSubject subject = new UserSubject();
        subject.setLogin(user);
        return subject;
    } catch (Exception ex) {
        throw ExceptionUtils.toInternalServerErrorException(ex, null);
    }
}
 
Example 5
Source File: CorbaStreamFaultInInterceptor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void createFaultDetail(Document faultData, FaultInfo faultInfo, Fault faultEx) {
    MessagePartInfo partInfo = faultInfo.getMessageParts().get(0);
    QName partInfoName = partInfo.getElementQName();
    Document faultDoc = DOMUtils.getEmptyDocument();
    Element faultElement = faultDoc.createElement("detail");
    Element partElement =
        faultDoc.createElementNS(partInfoName.getNamespaceURI(), partInfoName.getLocalPart());

    Element faultDataElement = (Element) faultData.getFirstChild();
    Node node = faultDataElement.getFirstChild();
    while (node != null) {
        Node importedFaultData = faultDoc.importNode(node, true);
        partElement.appendChild(importedFaultData);
        node = node.getNextSibling();
    }
    faultElement.appendChild(partElement);
    faultEx.setDetail(faultElement);
}
 
Example 6
Source File: IssueSamlClaimsUnitTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Element createSecondaryParameters() {
    Document doc = DOMUtils.getEmptyDocument();
    Element secondary = doc.createElementNS(STSConstants.WST_NS_05_12, "SecondaryParameters");
    secondary.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns", STSConstants.WST_NS_05_12);

    Element claims = doc.createElementNS(STSConstants.WST_NS_05_12, "Claims");
    claims.setAttributeNS(null, "Dialect", STSConstants.IDT_NS_05_05);

    Element claimType = createClaimsType(doc);
    claims.appendChild(claimType);
    Element claimValue = createClaimValue(doc);
    claims.appendChild(claimValue);
    secondary.appendChild(claims);

    return secondary;
}
 
Example 7
Source File: IssueSamlRealmUnitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Element createAppliesToElement(String addressUrl) {
    Document doc = DOMUtils.getEmptyDocument();
    Element appliesTo = doc.createElementNS(STSConstants.WSP_NS, "wsp:AppliesTo");
    appliesTo.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsp", STSConstants.WSP_NS);
    Element endpointRef = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:EndpointReference");
    endpointRef.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    Element address = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:Address");
    address.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    address.setTextContent(addressUrl);
    endpointRef.appendChild(address);
    appliesTo.appendChild(endpointRef);
    return appliesTo;
}
 
Example 8
Source File: FragmentFault.java    From cxf with Apache License 2.0 5 votes vote down vote up
public FragmentFault(String reason, String detail, QName faultCode) {
    super(reason, faultCode);
    if (detail != null) {
        Document doc = DOMUtils.getEmptyDocument();
        Element detailEl = doc.createElement("detail");
        detailEl.setTextContent(detail);
        setDetail(detailEl);
    }
}
 
Example 9
Source File: IssueSamlUnitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Element createAppliesToElement(String addressUrl) {
    Document doc = DOMUtils.getEmptyDocument();
    Element appliesTo = doc.createElementNS(STSConstants.WSP_NS, "wsp:AppliesTo");
    appliesTo.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsp", STSConstants.WSP_NS);
    appliesTo.appendChild(createEndpointReference(doc, addressUrl));
    return appliesTo;
}
 
Example 10
Source File: IssueJWTRealmUnitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Element createAppliesToElement(String addressUrl) {
    Document doc = DOMUtils.getEmptyDocument();
    Element appliesTo = doc.createElementNS(STSConstants.WSP_NS, "wsp:AppliesTo");
    appliesTo.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsp", STSConstants.WSP_NS);
    Element endpointRef = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:EndpointReference");
    endpointRef.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    Element address = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:Address");
    address.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    address.setTextContent(addressUrl);
    endpointRef.appendChild(address);
    appliesTo.appendChild(endpointRef);
    return appliesTo;
}
 
Example 11
Source File: ValidateTokenTransformationUnitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Element createClaimsElementInSecondaryParameters() {
    Document doc = DOMUtils.getEmptyDocument();
    Element secondary = doc.createElementNS(STSConstants.WST_NS_05_12, "SecondaryParameters");
    secondary.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns", STSConstants.WST_NS_05_12);

    Element claims = doc.createElementNS(STSConstants.WST_NS_05_12, "Claims");
    claims.setAttributeNS(null, "Dialect", STSConstants.IDT_NS_05_05);

    Element claimType = createClaimsType(doc);

    claims.appendChild(claimType);
    secondary.appendChild(claims);

    return secondary;
}
 
Example 12
Source File: ServiceModelPolicyUpdater.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void addPolicyRef(Extensible ext, Policy p) {
    Document doc = DOMUtils.getEmptyDocument();
    Element el = doc.createElementNS(p.getNamespace(), Constants.ELEM_POLICY_REF);
    el.setPrefix(Constants.ATTR_WSP);
    el.setAttribute(Constants.ATTR_URI, "#" + p.getId());

    UnknownExtensibilityElement uee = new UnknownExtensibilityElement();
    uee.setElementType(new QName(p.getNamespace(), Constants.ELEM_POLICY_REF));
    uee.setElement(el);
    uee.setRequired(true);

    ext.addExtensor(uee);
}
 
Example 13
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 14
Source File: IssueUnitTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Element createAppliesToElement(String addressUrl) {
    Document doc = DOMUtils.getEmptyDocument();
    Element appliesTo = doc.createElementNS(STSConstants.WSP_NS, "wsp:AppliesTo");
    appliesTo.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsp", STSConstants.WSP_NS);
    Element endpointRef = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:EndpointReference");
    endpointRef.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    Element address = doc.createElementNS(STSConstants.WSA_NS_05, "wsa:Address");
    address.setAttributeNS(WSS4JConstants.XMLNS_NS, "xmlns:wsa", STSConstants.WSA_NS_05);
    address.setTextContent(addressUrl);
    endpointRef.appendChild(address);
    appliesTo.appendChild(endpointRef);
    return appliesTo;
}
 
Example 15
Source File: ClaimsCallbackHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Claims Element for a "role"
 */
private Element createClaims() {
    Document doc = DOMUtils.getEmptyDocument();
    Element claimsElement =
        doc.createElementNS("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "Claims");
    claimsElement.setAttributeNS(null, "Dialect", "http://schemas.xmlsoap.org/ws/2005/05/identity");
    Element claimType =
        doc.createElementNS("http://schemas.xmlsoap.org/ws/2005/05/identity", "ClaimType");
    claimType.setAttributeNS(null, "Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/role");
    claimsElement.appendChild(claimType);
    return claimsElement;
}
 
Example 16
Source File: CustomUsernameTokenProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public TokenProviderResponse createToken(TokenProviderParameters tokenParameters) {
    try {
        Document doc = DOMUtils.getEmptyDocument();

        // Mock up a UsernameToken
        UsernameToken usernameToken = new UsernameToken(true, doc, WSS4JConstants.PASSWORD_TEXT);
        usernameToken.setName("alice");
        usernameToken.setPassword("password");
        String id = "UT-1234";
        usernameToken.addWSSENamespace();
        usernameToken.addWSUNamespace();
        usernameToken.setID(id);

        TokenProviderResponse response = new TokenProviderResponse();
        response.setToken(usernameToken.getElement());
        response.setTokenId(id);

        // Store the token in the cache
        if (tokenParameters.getTokenStore() != null) {
            SecurityToken securityToken = new SecurityToken(usernameToken.getID());
            securityToken.setToken(usernameToken.getElement());
            int hashCode = usernameToken.hashCode();
            String identifier = Integer.toString(hashCode);
            securityToken.setTokenHash(hashCode);
            tokenParameters.getTokenStore().add(identifier, securityToken);
        }

        return response;
    } catch (Exception e) {
        e.printStackTrace();
        throw new STSException("Can't serialize SAML assertion", e, STSException.REQUEST_FAILED);
    }
}
 
Example 17
Source File: FragmentPutAddTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test(expected = SOAPFaultException.class)
public void addExistingAttributeTest() throws XMLStreamException {
    String content = "<a foo=\"1\"/>";
    ResourceManager resourceManager = new MemoryResourceManager();
    ReferenceParametersType refParams = resourceManager.create(getRepresentation(content));
    Server resource = createLocalResource(resourceManager);
    Resource client = createClient(refParams);

    Put request = new Put();
    request.setDialect(FragmentDialectConstants.FRAGMENT_2011_03_IRI);
    Fragment fragment = new Fragment();
    ExpressionType expression = new ExpressionType();
    expression.setLanguage(FragmentDialectConstants.XPATH10_LANGUAGE_IRI);
    expression.setMode(FragmentDialectConstants.FRAGMENT_MODE_ADD);
    expression.getContent().add("/a");

    Document doc = DOMUtils.getEmptyDocument();
    Element addedAttr = doc.createElementNS(
            FragmentDialectConstants.FRAGMENT_2011_03_IRI,
            FragmentDialectConstants.FRAGMENT_ATTR_NODE_NAME
    );
    addedAttr.setAttributeNS(
            FragmentDialectConstants.FRAGMENT_2011_03_IRI,
            FragmentDialectConstants.FRAGMENT_ATTR_NODE_NAME_ATTR,
            "foo"
    );
    addedAttr.setTextContent("2");
    ValueType value = new ValueType();
    value.getContent().add(addedAttr);
    fragment.setExpression(expression);
    fragment.setValue(value);
    request.getAny().add(fragment);

    client.put(request);

    resource.destroy();
}
 
Example 18
Source File: JWTTokenValidatorRealmTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Element createTokenWrapper(String token) {
    Document doc = DOMUtils.getEmptyDocument();
    Element tokenWrapper = doc.createElementNS(null, "TokenWrapper");
    tokenWrapper.setTextContent(token);
    return tokenWrapper;
}
 
Example 19
Source File: PolicyAnnotationListener.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Element addPolicy(ServiceInfo service, Policy p, Class<?> cls, String defName) {
    String uri = p.uri();
    String ns = Constants.URI_POLICY_NS;

    if (p.includeInWSDL()) {
        Element element = loadPolicy(uri, defName);
        if (element == null) {
            return null;
        }

        // might have been updated on load policy
        uri = getPolicyId(element);
        ns = element.getNamespaceURI();

        if (service.getDescription() == null && cls != null) {
            service.setDescription(new DescriptionInfo());
            URL u = cls.getResource("/");
            if (u != null) {
                service.getDescription().setBaseURI(u.toString());
            }
        }

        // if not already added to service add it, otherwise ignore
        // and just create the policy reference.
        if (!isExistsPolicy(service.getDescription().getExtensors().get(), uri)) {
            UnknownExtensibilityElement uee = new UnknownExtensibilityElement();
            uee.setElement(element);
            uee.setRequired(true);
            uee.setElementType(DOMUtils.getElementQName(element));
            service.getDescription().addExtensor(uee);
        }

        uri = "#" + uri;
    }

    Document doc = DOMUtils.getEmptyDocument();
    Element el = doc.createElementNS(ns, "wsp:" + Constants.ELEM_POLICY_REF);
    Attr att = doc.createAttributeNS(null, "URI");
    att.setValue(uri);
    el.setAttributeNodeNS(att);
    return el;
}
 
Example 20
Source File: JWTTokenValidatorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Element createTokenWrapper(String token) {
    Document doc = DOMUtils.getEmptyDocument();
    Element tokenWrapper = doc.createElementNS(null, "TokenWrapper");
    tokenWrapper.setTextContent(token);
    return tokenWrapper;
}