Example usage for org.apache.commons.lang3.reflect FieldUtils readDeclaredStaticField

List of usage examples for org.apache.commons.lang3.reflect FieldUtils readDeclaredStaticField

Introduction

In this page you can find the example usage for org.apache.commons.lang3.reflect FieldUtils readDeclaredStaticField.

Prototype

public static Object readDeclaredStaticField(final Class<?> cls, final String fieldName)
        throws IllegalAccessException 

Source Link

Document

Gets the value of a static Field by name.

Usage

From source file:org.apache.nutch.protocol.httpclient.HttpFormAuthentication.java

/**
 * NUTCH-2280 Set the cookie policy value from httpclient-auth.xml for the
 * Post httpClient action./*from w  ww.  java  2  s  .c o  m*/
 * 
 * @param fromConfigurer
 *          - the httpclient-auth.xml values
 * 
 * @param params
 *          - the HttpMethodParams from the current httpclient instance
 * 
 * @throws NoSuchFieldException
 * @throws SecurityException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 */
private void setCookieParams(HttpFormAuthConfigurer formConfigurer, HttpMethodParams params)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    // NUTCH-2280 - set the HttpClient cookie policy
    if (formConfigurer.getCookiePolicy() != null) {
        String policy = formConfigurer.getCookiePolicy();
        Object p = FieldUtils.readDeclaredStaticField(CookiePolicy.class, policy);
        if (null != p) {
            LOG.debug("reflection of cookie value: " + p.toString());
            params.setParameter(HttpMethodParams.COOKIE_POLICY, p);
        }
    }
}

From source file:org.ops4j.pax.web.service.jetty.internal.JettyFactoryImpl.java

/**
 * {@inheritDoc}/*from   w  w  w .  j av a2s  .  co  m*/
 */
@Override
public ServerConnector createSecureConnector(Server server, String name, int port, String sslKeystore,
        String sslKeystorePassword, String sslKeyPassword, String host, String sslKeystoreType,
        String sslKeyAlias, String trustStore, String trustStorePassword, String trustStoreType,
        boolean isClientAuthNeeded, boolean isClientAuthWanted, List<String> cipherSuitesIncluded,
        List<String> cipherSuitesExcluded, List<String> protocolsIncluded, List<String> protocolsExcluded,
        Boolean sslRenegotiationAllowed, String crlPath, Boolean enableCRLDP, Boolean validateCerts,
        Boolean validatePeerCerts, Boolean enableOCSP, String ocspResponderURL) {

    // SSL Context Factory for HTTPS and SPDY
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(sslKeystore);
    sslContextFactory.setKeyStorePassword(sslKeystorePassword);
    sslContextFactory.setKeyManagerPassword(sslKeyPassword);
    sslContextFactory.setNeedClientAuth(isClientAuthNeeded);
    sslContextFactory.setWantClientAuth(isClientAuthWanted);
    sslContextFactory.setEnableCRLDP(enableCRLDP);
    sslContextFactory.setValidateCerts(validateCerts);
    sslContextFactory.setValidatePeerCerts(validatePeerCerts);
    sslContextFactory.setEnableOCSP(enableOCSP);
    if ((null != crlPath) && (!"".equals(crlPath))) {
        sslContextFactory.setCrlPath(crlPath);
    }
    if ((null != ocspResponderURL) && (!"".equals(ocspResponderURL))) {
        sslContextFactory.setOcspResponderURL(ocspResponderURL);
    }
    if (sslKeystoreType != null) {
        sslContextFactory.setKeyStoreType(sslKeystoreType);
    }
    // Java key stores may contain more than one private key entry.
    // Specifying the alias tells jetty which one to use.
    if ((null != sslKeyAlias) && (!"".equals(sslKeyAlias))) {
        sslContextFactory.setCertAlias(sslKeyAlias);
    }

    // Quite often it is useful to use a certificate trust store other than the JVM default.
    if ((null != trustStore) && (!"".equals(trustStore))) {
        sslContextFactory.setTrustStorePath(trustStore);
    }
    if ((null != trustStorePassword) && (!"".equals(trustStorePassword))) {
        sslContextFactory.setTrustStorePassword(trustStorePassword);
    }
    if ((null != trustStoreType) && (!"".equals(trustStoreType))) {
        sslContextFactory.setTrustStoreType(trustStoreType);
    }

    // In light of well-known attacks against weak encryption algorithms such as RC4,
    // it is usefull to be able to include or exclude certain ciphersuites.
    // Due to the overwhelming number of cipher suites using regex to specify inclusions
    // and exclusions greatly simplifies configuration.
    final String[] cipherSuites;
    try {
        SSLContext context = SSLContext.getDefault();
        SSLSocketFactory sf = context.getSocketFactory();
        cipherSuites = sf.getSupportedCipherSuites();
    } catch (NoSuchAlgorithmException e) {

        throw new RuntimeException("Failed to get supported cipher suites.", e);
    }

    if (cipherSuitesIncluded != null && !cipherSuitesIncluded.isEmpty()) {
        final List<String> cipherSuitesToInclude = new ArrayList<>();
        for (final String cipherSuite : cipherSuites) {
            for (final String includeRegex : cipherSuitesIncluded) {
                if (cipherSuite.matches(includeRegex)) {
                    cipherSuitesToInclude.add(cipherSuite);
                }
            }
        }
        sslContextFactory.setIncludeCipherSuites(
                cipherSuitesToInclude.toArray(new String[cipherSuitesToInclude.size()]));
    }

    if (cipherSuitesExcluded != null && !cipherSuitesExcluded.isEmpty()) {
        final List<String> cipherSuitesToExclude = new ArrayList<>();
        for (final String cipherSuite : cipherSuites) {
            for (final String excludeRegex : cipherSuitesExcluded) {
                if (cipherSuite.matches(excludeRegex)) {
                    cipherSuitesToExclude.add(cipherSuite);
                }
            }
        }
        sslContextFactory.setExcludeCipherSuites(
                cipherSuitesToExclude.toArray(new String[cipherSuitesToExclude.size()]));
    }

    // In light of attacks against SSL 3.0 as "POODLE" it is useful to include or exclude
    // SSL/TLS protocols as needed.
    if ((null != protocolsIncluded) && (!protocolsIncluded.isEmpty())) {
        sslContextFactory.setIncludeProtocols(protocolsIncluded.toArray(new String[protocolsIncluded.size()]));
    }
    if ((null != protocolsExcluded) && (!protocolsExcluded.isEmpty())) {
        sslContextFactory.setExcludeProtocols(protocolsExcluded.toArray(new String[protocolsExcluded.size()]));
    }
    if (sslRenegotiationAllowed != null) {
        sslContextFactory.setRenegotiationAllowed(sslRenegotiationAllowed);
    }

    // HTTP Configuration
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme(HttpScheme.HTTPS.asString());
    httpConfig.setSecurePort(port);
    httpConfig.setOutputBufferSize(32768);

    // HTTPS Configuration
    HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
    httpsConfig.addCustomizer(new SecureRequestCustomizer());

    List<AbstractConnectionFactory> connectionFactories = new ArrayList<>();

    HttpConnectionFactory httpConFactory = new HttpConnectionFactory(httpsConfig);

    SslConnectionFactory sslFactory = null;
    AbstractConnectionFactory http2Factory = null;

    NegotiatingServerConnectionFactory alpnFactory = null;

    if (alpnCLassesAvailable()) {
        log.info("HTTP/2 available, creating HttpSpdyServerConnector for Https");
        // SPDY connector
        try {
            Class<?> comparatorClass = bundle.loadClass("org.eclipse.jetty.http2.HTTP2Cipher");

            Comparator<String> cipherComparator = (Comparator<String>) FieldUtils
                    .readDeclaredStaticField(comparatorClass, "COMPARATOR");
            sslContextFactory.setCipherComparator(cipherComparator);

            sslFactory = new SslConnectionFactory(sslContextFactory, "h2");
            connectionFactories.add(sslFactory);

            //org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory
            Class<?> loadClass = bundle
                    .loadClass("org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory");
            //            
            //            //ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory("spdy/3", "http/1.1");
            //            alpnFactory = (NegotiatingServerConnectionFactory) ConstructorUtils.invokeConstructor(loadClass, (Object) new String[] {"ssl", "http/2", "http/1.1"});
            //            alpnFactory.setDefaultProtocol("http/1.1");
            //            connectionFactories.add(alpnFactory);

            //HTTPSPDYServerConnectionFactory spdy = new HTTPSPDYServerConnectionFactory(SPDY.V3, httpConfig);
            //            loadClass = bundle.loadClass("org.eclipse.jetty.spdy.server.http.HTTPSPDYServerConnectionFactory");
            //            loadClass = bundle.loadClass("org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory");

            http2Factory = (AbstractConnectionFactory) ConstructorUtils.invokeConstructor(loadClass,
                    httpsConfig);
            connectionFactories.add(http2Factory);

        } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException
                | IllegalArgumentException | IllegalAccessException | InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        log.info("SPDY not available, creating standard ServerConnector for Https");
        sslFactory = new SslConnectionFactory(sslContextFactory, "http/1.1");
    }

    //      HttpConnectionFactory httpFactory = new HttpConnectionFactory(httpConfig);
    //      
    //      ServerConnector https = new ServerConnector(server); 

    // HTTPS connector
    ServerConnector https = new ServerConnector(server, sslFactory, httpConFactory);
    for (AbstractConnectionFactory factory : connectionFactories) {
        https.addConnectionFactory(factory);
    }

    https.setPort(port);
    https.setName(name);
    https.setHost(host);
    https.setIdleTimeout(500000);

    /*
             
    SslContextFactory sslContextFactory = new SslContextFactory();
    HttpConfiguration httpConfig = new HttpConfiguration();
            
    SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, "alpn");
    ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory("spdy/3", "http/1.1");
    alpn.setDefaultProtocol("http/1.1");
    HTTPSPDYServerConnectionFactory spdy = new HTTPSPDYServerConnectionFactory(SPDY.V3, httpConfig);
    HttpConnectionFactory http = new HttpConnectionFactory(httpConfig);
            
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server, new ConnectionFactory[]{ssl, alpn, spdy, http});
             
     */

    return https;

}