Java Code Examples for org.apache.cxf.common.util.ReflectionUtil

The following examples show how to use org.apache.cxf.common.util.ReflectionUtil. These examples are extracted from open source projects. 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 Project: openwebbeans-meecrowave   Source File: CxfCdiAutoSetup.java    License: Apache License 2.0 6 votes vote down vote up
private void dump(final LogFacade log, final ServerProviderFactory spf, final String description, final String fieldName) {
    final Field field = ReflectionUtil.getDeclaredField(ProviderFactory.class, fieldName);
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    try {
        final Collection<ProviderInfo<?>> providers = Collection.class.cast(field.get(spf));
        log.info("     " + description);
        providers.stream().map(ProviderInfo::getProvider).forEach(o -> {
            try {
                log.info("       - " + o);
            } catch (final RuntimeException re) {
                // no-op: maybe cdi context is not active
            }
        });
    } catch (IllegalAccessException e) {
        // ignore, not that a big deal
    }
}
 
Example 2
Source Project: cxf   Source File: JAXBDataBinding.java    License: Apache License 2.0 6 votes vote down vote up
private static Field getElField(String partName, final Class<?> wrapperType) {
    String fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
    Field[] fields = ReflectionUtil.getDeclaredFields(wrapperType);
    for (Field field : fields) {
        XmlElement el = field.getAnnotation(XmlElement.class);
        if (el != null
            && partName.equals(el.name())) {
            return field;
        }

        XmlElementRef xmlElementRefAnnotation = field.getAnnotation(XmlElementRef.class);
        if (xmlElementRefAnnotation != null && partName.equals(xmlElementRefAnnotation.name())) {
            return field;
        }

        if (field.getName().equals(fieldName)) {
            return field;
        }
    }
    return null;
}
 
Example 3
Source Project: cxf   Source File: DataWriterImpl.java    License: Apache License 2.0 6 votes vote down vote up
private static void setContextualNamespaceDecls(Object mapper, Map<String, String> nsctxt) {
    try {
        Method m = ReflectionUtil.getDeclaredMethod(mapper.getClass(),
                                                    "setContextualNamespaceDecls", new Class<?>[]{String[].class});
        String[] args = new String[nsctxt.size() * 2];
        int ai = 0;
        for (Entry<String, String> nsp : nsctxt.entrySet()) {
            args[ai++] = nsp.getValue();
            args[ai++] = nsp.getKey();
        }
        m.invoke(mapper, new Object[]{args});
    } catch (Exception e) {
        // ignore
        LOG.log(Level.WARNING, "Failed to set the contextual namespace map", e);
    }

}
 
Example 4
Source Project: cxf   Source File: JAXBUtilsTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSetNamespaceMapper() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(GreetMe.class);
    Marshaller marshaller = ctx.createMarshaller();
    Map<String, String> nspref = new HashMap<>();
    nspref.put("http://cxf.apache.org/hello_world_soap_http/types", "x");
    JAXBUtils.setNamespaceMapper(nspref, marshaller);
    String mapperkey = null;
    if (marshaller.getClass().getName().contains(".internal.")) {
        mapperkey = "com.sun.xml.internal.bind.namespacePrefixMapper";
    } else if (marshaller.getClass().getName().contains("com.sun")) {
        mapperkey = "com.sun.xml.bind.namespacePrefixMapper";
    } else if (marshaller.getClass().getName().contains("eclipse")) {
        mapperkey = "eclipselink.namespace-prefix-mapper";
    }
    if (mapperkey != null) {
        Object mapper = marshaller.getProperty(mapperkey);
        assertNotNull(mapper);

        // also verify this mapper has setContextualNamespaceDecls
        Method m = ReflectionUtil.getDeclaredMethod(mapper.getClass(),
                                                    "setContextualNamespaceDecls", new Class<?>[]{String[].class});
        assertNotNull(m);
    }

}
 
Example 5
Source Project: cxf   Source File: JAXRSUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Response copyResponseIfNeeded(Response response) {
    if (!(response instanceof ResponseImpl)) {
        Response r = fromResponse(response).build();
        Field[] declaredFields = ReflectionUtil.getDeclaredFields(response.getClass());
        for (Field f : declaredFields) {
            Class<?> declClass = f.getType();
            if (declClass == Annotation[].class) {
                try {
                    Annotation[] fieldAnnotations =
                        ReflectionUtil.accessDeclaredField(f, response, Annotation[].class);
                    ((ResponseImpl)r).setEntityAnnotations(fieldAnnotations);
                } catch (Throwable ex) {
                    LOG.warning("Custom annotations if any can not be copied");
                }
                break;
            }
        }
        return r;
    }
    return response;
}
 
Example 6
Source Project: cxf   Source File: AbstractResourceInfo.java    License: Apache License 2.0 6 votes vote down vote up
private void findContextFields(Class<?> cls, Object provider) {
    if (cls == Object.class || cls == null) {
        return;
    }
    for (Field f : ReflectionUtil.getDeclaredFields(cls)) {
        for (Annotation a : f.getAnnotations()) {
            if (a.annotationType() == Context.class
                && (f.getType().isInterface() || f.getType() == Application.class)) {
                contextFields = addContextField(contextFields, f);
                checkContextClass(f.getType());
                if (!InjectionUtils.VALUE_CONTEXTS.contains(f.getType().getName())) {
                    addToMap(getFieldProxyMap(true), f, getFieldThreadLocalProxy(f, provider));
                }
            }
        }
    }
    findContextFields(cls.getSuperclass(), provider);
}
 
Example 7
Source Project: cxf   Source File: BeanResourceInfo.java    License: Apache License 2.0 6 votes vote down vote up
private void setParamField(Class<?> cls) {
    if (Object.class == cls || cls == null) {
        return;
    }
    for (Field f : ReflectionUtil.getDeclaredFields(cls)) {
        for (Annotation a : f.getAnnotations()) {
            if (AnnotationUtils.isParamAnnotationClass(a.annotationType())) {
                if (paramFields == null) {
                    paramFields = new ArrayList<>();
                }
                paramsAvailable = true;
                paramFields.add(f);
            }
        }
    }
    setParamField(cls.getSuperclass());
}
 
Example 8
Source Project: cxf   Source File: ClientProxyImpl.java    License: Apache License 2.0 6 votes vote down vote up
protected boolean getValuesFromBeanParamField(Object bean,
                                            Field f,
                                            Class<? extends Annotation> annClass,
                                            Map<String, BeanPair> values) {
    boolean jaxrsParamAnnAvailable = false;
    Annotation fieldAnnotation = f.getAnnotation(annClass);
    if (fieldAnnotation != null) {
        jaxrsParamAnnAvailable = true;
        Object value = ReflectionUtil.accessDeclaredField(f, bean, Object.class);
        if (value != null) {
            String annotationValue = AnnotationUtils.getAnnotationValue(fieldAnnotation);
            values.put(annotationValue, new BeanPair(value, f.getAnnotations()));
        }
    }
    return jaxrsParamAnnAvailable;
}
 
Example 9
Source Project: cxf   Source File: URLConnectionHTTPConduit.java    License: Apache License 2.0 6 votes vote down vote up
private OutputStream connectAndGetOutputStream(Boolean b) throws IOException {
    OutputStream cout = null;

    if (b != null && b) {
        String method = connection.getRequestMethod();
        connection.connect();
        try {
            java.lang.reflect.Field f = ReflectionUtil.getDeclaredField(HttpURLConnection.class, "method");
            ReflectionUtil.setAccessible(f).set(connection, "POST");
            cout = connection.getOutputStream();
            ReflectionUtil.setAccessible(f).set(connection, method);
        } catch (Throwable t) {
            logStackTrace(t);
        }

    } else {
        cout = connection.getOutputStream();
    }
    return cout;
}
 
Example 10
Source Project: cxf   Source File: JettyContinuationProviderFactory.java    License: Apache License 2.0 6 votes vote down vote up
public ContinuationProvider createContinuationProvider(Message inMessage,
                                                       HttpServletRequest req,
                                                       HttpServletResponse resp) {
    if (!disableJettyContinuations) {
        ServletRequest r2 = req;
        while (r2 instanceof ServletRequestWrapper) {
            r2 = ((ServletRequestWrapper)r2).getRequest();
        }
        if (!r2.getClass().getName().contains("jetty")) {
            return null;
        }

        try {
            Method m = r2.getClass().getMethod("isAsyncSupported");
            Object o = ReflectionUtil.setAccessible(m).invoke(r2);
            if (((Boolean)o).booleanValue()) {
                return new JettyContinuationProvider(req, resp, inMessage);
            }
        } catch (Throwable t) {
            //ignore - either not a proper Jetty request object or classloader issue
            //or similar.
        }
    }
    return null;
}
 
Example 11
Source Project: cxf   Source File: ResourceInjector.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean processable(Class<?> cls, Object o) {
    if (cls.getName().startsWith("java.")
        || cls.getName().startsWith("javax.")) {
        return false;
    }
    NoJSR250Annotations njsr = cls.getAnnotation(NoJSR250Annotations.class);
    if (njsr != null) {
        for (String s : njsr.unlessNull()) {
            try {
                Field f = getField(cls, s);
                ReflectionUtil.setAccessible(f);
                if (f.get(o) == null) {
                    return true;
                }
            } catch (Exception ex) {
                return true;
            }
        }
        return false;
    }
    return true;
}
 
Example 12
Source Project: cxf   Source File: JAXBUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Class<?> getValidClass(Class<?> cls) {
    if (cls.isEnum() || cls.isArray()) {
        return cls;
    }

    if (cls == Object.class || cls == String.class || cls.isPrimitive() || cls.isAnnotation()
        || "javax.xml.ws.Holder".equals(cls.getName())) {
        return null;
    } else if (cls.isInterface()
        || "javax.xml.ws.wsaddressing.W3CEndpointReference".equals(cls.getName())) {
        return cls;
    }

    Constructor<?> cons = ReflectionUtil.getDeclaredConstructor(cls);
    if (cons == null) {
        cons = ReflectionUtil.getConstructor(cls);
        if (cons == null) {
            return null;
        }
    }
    return cls;
}
 
Example 13
Source Project: steady   Source File: SecurityTokenServiceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example 14
Source Project: steady   Source File: SecurityTokenServiceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example 15
Source Project: steady   Source File: SecurityTokenServiceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example 16
Source Project: steady   Source File: SecurityTokenServiceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  " 
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with 
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example 17
Source Project: cxf   Source File: JAXBDataBinding.java    License: Apache License 2.0 5 votes vote down vote up
private void hackInNewInternalizationLogic(SchemaCompiler schemaCompiler,
                                           final OASISCatalogManager catalog,
                                           Options opts) {
    try {
        Field f = schemaCompiler.getClass().getDeclaredField("forest");
        ReflectionUtil.setAccessible(f);
        XMLSchemaInternalizationLogic logic = new XMLSchemaInternalizationLogic() {
            public XMLFilterImpl createExternalReferenceFinder(DOMForest parent) {
                return new ReferenceFinder(parent, catalog);
            }
        };

        Constructor<DOMForest> c = null;
        DOMForest forest = null;

        try {
            c = DOMForest.class.getConstructor(InternalizationLogic.class, Options.class);
            forest = c.newInstance(logic, opts);
        } catch (Throwable t) {
            c = DOMForest.class.getConstructor(InternalizationLogic.class);
            forest = c.newInstance(logic);
        }
        forest.setErrorHandler((ErrorReceiver)schemaCompiler);
        f.set(schemaCompiler, forest);
    } catch (Throwable ex)  {
        //ignore
    }
}
 
Example 18
Source Project: cxf   Source File: TypesCodeWriter.java    License: Apache License 2.0 5 votes vote down vote up
private void setEncoding(String s) {
    if (s != null) {
        try {
            //requires XJC 2.2.5 or newer
            Field f = CodeWriter.class.getDeclaredField("encoding");
            ReflectionUtil.setAccessible(f).set(this, s);
        } catch (Throwable t) {
            //ignore - should be caught in JAXBDataBinding.checkEncoding already
        }
    }
}
 
Example 19
Source Project: cxf   Source File: JAXBDataBindingTest.java    License: Apache License 2.0 5 votes vote down vote up
void doNamespaceMappingTest(boolean internal, boolean asm) throws Exception {
    if (internal) {
        try {
            Class.forName("com.sun.xml.internal.bind.v2.ContextFactory");
        } catch (Throwable t) {
            //on a JVM (likely IBM's) that doesn't rename the ContextFactory package to include "internal"
            return;
        }
    }
    try {
        if (!asm) {
            ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM"))
                .set(null, Boolean.TRUE);
        }

        JAXBDataBinding db = createJaxbContext(internal);

        DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
        XMLOutputFactory writerFactory = XMLOutputFactory.newInstance();
        StringWriter stringWriter = new StringWriter();
        XMLStreamWriter xmlWriter = writerFactory.createXMLStreamWriter(stringWriter);
        QualifiedBean bean = new QualifiedBean();
        bean.setAriadne("spider");
        writer.write(bean, xmlWriter);
        xmlWriter.flush();
        String xml = stringWriter.toString();
        assertTrue("Failed to map namespace " + xml, xml.contains("greenland=\"uri:ultima:thule"));
    } finally {
        if (!asm) {
            ReflectionUtil.setAccessible(ReflectionUtil.getDeclaredField(ASMHelper.class, "badASM"))
                .set(null, Boolean.FALSE);
        }
    }
}
 
Example 20
Source Project: cxf   Source File: CXFService.java    License: Apache License 2.0 5 votes vote down vote up
private ServiceImpl findDelegate() {
    for (Field f : ReflectionUtil.getDeclaredFields(Service.class)) {
        if (ServiceDelegate.class.equals(f.getType())) {
            ServiceDelegate del = ReflectionUtil.accessDeclaredField(f, this, ServiceDelegate.class);
            if (del instanceof ServiceImpl) {
                return (ServiceImpl)del;
            }
            throw new WebServiceException("Delegate of class " + del.getClass() + " is not a CXF delegate.  "
                                          + " Check the classpath to make sure CXF is loaded first.");
        }
    }
    throw new WebServiceException("Could not find CXF service delegate");
}
 
Example 21
Source Project: cxf   Source File: JAXWSProviderMethodDispatcher.java    License: Apache License 2.0 5 votes vote down vote up
public JAXWSProviderMethodDispatcher(JaxWsImplementorInfo implInfo) {
    try {
        invoke = ReflectionUtil.getMethod(implInfo.getImplementorClass(), "invoke", 
                                          new Class[] {implInfo.getProviderParameterType()});
        ReflectionUtil.setAccessible(invoke);
    } catch (Exception e1) {
        //fall back to the raw Provider provided invoke method
        try {
            invoke = Provider.class.getMethod("invoke", new Class[] {Object.class});
        } catch (Exception e) {
            throw new ServiceConstructionException(e);
        }
    }
}
 
Example 22
Source Project: cxf   Source File: InjectionUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Field getDeclaredField(Class<?> cls, String fieldName) {
    if (cls == null || cls == Object.class) {
        return null;
    }
    Field f = ReflectionUtil.getDeclaredField(cls, fieldName);
    if (f != null) {
        return f;
    }
    return getDeclaredField(cls.getSuperclass(), fieldName);
}
 
Example 23
Source Project: cxf   Source File: SecurityTokenServiceProvider.java    License: Apache License 2.0 5 votes vote down vote up
private void walkDom(String pfx, Element element, Binder<Node> binder, Object parent) {
    try {
        Object o = binder.getJAXBNode(element);
        if (o instanceof JAXBElement) {
            o = ((JAXBElement<?>)o).getValue();
        }
        //System.out.println(pfx + DOMUtils.getElementQName(element) + " ->  "
        //    + (o == null ? "null" : o.getClass()));
        if (o == null && parent != null) {
            // if it's not able to bind to an object, it's possibly an xsd:any
            // we'll check the parent for the standard "any" and replace with
            // the original element.
            Field f = parent.getClass().getDeclaredField("any");
            if (f.getAnnotation(XmlAnyElement.class) != null) {
                Object old = ReflectionUtil.setAccessible(f).get(parent);
                if (old instanceof Element
                    && DOMUtils.getElementQName(element).equals(DOMUtils.getElementQName((Element)old))) {
                    ReflectionUtil.setAccessible(f).set(parent, element);
                }
            }
        }
        if (o == null) {
            return;
        }
        Node nd = element.getFirstChild();
        while (nd != null) {
            if (nd instanceof Element) {
                walkDom(pfx + "  ", (Element)nd, binder, o);
            }
            nd = nd.getNextSibling();
        }
    } catch (Throwable t) {
        //ignore -this is a complete hack anyway
    }
}
 
Example 24
Source Project: cxf   Source File: CrossOriginResourceSharingFilter.java    License: Apache License 2.0 5 votes vote down vote up
private <T extends Annotation> T  getAnnotation(Method m,
                                                Class<T> annClass) {
    if (m == null) {
        return null;
    }
    return ReflectionUtil.getAnnotationForMethodOrContainingClass(m,  annClass);
}
 
Example 25
Source Project: cxf   Source File: RestClientBean.java    License: Apache License 2.0 5 votes vote down vote up
private void setSSLConfig(CxfTypeSafeClientBuilder builder) {
    ConfigFacade.getOptionalValue(REST_HOSTNAME_VERIFIER_FORMAT, clientInterface, String.class).ifPresent(
        className -> {
            try {
                @SuppressWarnings("unchecked")
                Class<HostnameVerifier> clazz = (Class<HostnameVerifier>) 
                    ClassLoaderUtils.loadClassFromContextLoader(className);
                Constructor<HostnameVerifier> ctor = ReflectionUtil.getConstructor(clazz);
                if (ctor != null) {
                    builder.hostnameVerifier(ctor.newInstance());
                    return;
                }
            } catch (Throwable t) {
                // ignore - will log below
            }
            LOG.log(Level.WARNING, "INVALID_HOSTNAME_VERIFIER_CONFIGURED",
                    new Object[] {className, clientInterface.getName()});
    });

    
    ConfigFacade.getOptionalValue(REST_TRUST_STORE_FORMAT, clientInterface, String.class).ifPresent(
        trustStoreLoc -> {
            initTrustStore(trustStoreLoc, builder);
        }
    );

    ConfigFacade.getOptionalValue(REST_KEY_STORE_FORMAT, clientInterface, String.class).ifPresent(
        keyStoreLoc -> {
            initKeyStore(keyStoreLoc, builder);
        }
    );
    
}
 
Example 26
Source Project: cxf   Source File: DOMUtils.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Method computeValue(Class<?> type) {
    try {
        return ReflectionUtil.getMethod(type, "getDomElement");
    } catch (NoSuchMethodException e) {
        //best effort to try, do nothing if NoSuchMethodException
        return null;
    }
}
 
Example 27
Source Project: cxf   Source File: DOMUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Try to get the DOM Node from the SAAJ Node with JAVA9 afterwards
 * @param node The original node we need check
 * @return The DOM node
 */
public static Node getDomElement(Node node) {
    if (node != null && isJava9SAAJ()) {
        //java9plus hack
        Method method = GET_DOM_ELEMENTS_METHODS.get(node.getClass());
        if (method != null) {
            try {
                return (Node)ReflectionUtil.setAccessible(method).invoke(node);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return node;
}
 
Example 28
Source Project: cxf   Source File: DOMUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Try to get the DOM DocumentFragment from the SAAJ DocumentFragment with JAVA9 afterwards
 * @param fragment The original documentFragment we need to check
 * @return The DOM DocumentFragment
 */
public static DocumentFragment getDomDocumentFragment(DocumentFragment fragment) {
    if (fragment != null && isJava9SAAJ()) {
        //java9 plus hack
        Field f = GET_DOCUMENT_FRAGMENT_FIELDS.get(fragment.getClass());
        if (f != null) {
            return ReflectionUtil.accessDeclaredField(f, fragment, DocumentFragment.class);
        }
    }
    return fragment;
}
 
Example 29
Source Project: cxf   Source File: DefaultSecurityContext.java    License: Apache License 2.0 5 votes vote down vote up
protected boolean checkGroup(Principal principal, String role) {
    if (principal.getName().equals(role)) {
        return true;
    }

    Enumeration<? extends Principal> members;
    try {
        Method m = ReflectionUtil.getMethod(principal.getClass(), "members");
        m.setAccessible(true);
        @SuppressWarnings("unchecked")
        Enumeration<? extends Principal> ms = (Enumeration<? extends Principal>)m.invoke(principal);
        members = ms;
    } catch (Exception e) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Unable to invoke memebers in " + principal.getName() + ":" + e.getMessage());
        }
        return false;
    }
    
    while (members.hasMoreElements()) {
        // this might be a plain role but could represent a group consisting of other groups/roles
        Principal member = members.nextElement();
        if (member.getName().equals(role)
            || isGroupPrincipal(member) 
            && checkGroup((GroupPrincipal)member, role)) {
            return true;
        }
    }
    return false;
}
 
Example 30
Source Project: cxf   Source File: ResourceInjector.java    License: Apache License 2.0 5 votes vote down vote up
private static Field getField(Class<?> cls, String name) {
    if (cls == null) {
        return null;
    }
    try {
        Field f = ReflectionUtil.getDeclaredField(cls, name);
        if (f == null) {
            f = getField(cls.getSuperclass(), name);
        }
        return f;
    } catch (Exception ex) {
        return getField(cls.getSuperclass(), name);
    }
}