Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 David Berkman
*
* This file is part of the SmallMind Code Project.
*
* The SmallMind Code Project is free software, you can redistribute
* it and/or modify it under either, at your discretion...
*
* 1) The terms of GNU Affero General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* ...or...
*
* 2) The terms of the Apache License, Version 2.0.
*
* The SmallMind Code Project is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License or Apache License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* and the Apache License along with the SmallMind Code Project. If not, see
* or .
*
* Additional permission under the GNU Affero GPL version 3 section 7
* ------------------------------------------------------------------
* If you modify this Program, or any covered work, by linking or
* combining it with other code, such other code is not for that reason
* alone subject to any of the requirements of the GNU Affero GPL
* version 3.
*/
package org.smallmind.web.jersey.ssl;
import java.net.URI;
import java.security.AccessController;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.internal.ServiceFinder;
import org.glassfish.jersey.internal.util.PropertiesHelper;
import org.glassfish.jersey.internal.util.ReflectionHelper;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.spi.TestContainer;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.After;
import org.junit.Before;
/*
@Override
protected Client getClient ()
throws NoSuchAlgorithmException, KeyManagementException {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, new TrustManager[] {new NaiveTrustManager()}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
clientBuilder.sslContext(ctx);
clientBuilder.hostnameVerifier(new NaiveHostNameVerifier());
JERSEY_HTTPS_CLIENT = clientBuilder
.withConfig(new ClientConfig())
.register(JacksonFeature.class).build();
}
*/
public abstract class SSLJerseyTest {
private static final Logger LOGGER = Logger.getLogger(JerseyTest.class.getName());
private static Class extends TestContainerFactory> defaultTestContainerFactoryClass;
private final DeploymentContext context;
private final AtomicReference client = new AtomicReference<>(null);
private final Map propertyMap = new HashMap<>();
private final Map forcedPropertyMap = new HashMap<>();
private final Map logLevelMap = new IdentityHashMap<>();
private TestContainerFactory testContainerFactory;
private TestContainer testContainer;
public SSLJerseyTest () {
// Note: this must be the first call in the constructor to allow setting config
// properties (especially around logging) in the configure() or configureDeployment()
// method overridden in subclass, otherwise the properties set in the subclass would
// not be set soon enough
this.context = configureDeployment();
this.testContainerFactory = getTestContainerFactory();
}
public SSLJerseyTest (final TestContainerFactory testContainerFactory) {
// Note: this must be the first call in the constructor to allow setting config
// properties (especially around logging) in the configure() or configureDeployment()
// method overridden in subclass, otherwise the properties set in the subclass would
// not be set soon enough
this.context = configureDeployment();
this.testContainerFactory = testContainerFactory;
}
public SSLJerseyTest (final Application jaxrsApplication) {
this.context = DeploymentContext.newInstance(jaxrsApplication);
this.testContainerFactory = getTestContainerFactory();
}
private static String getSystemProperty (final String propertyName) {
final Properties systemProperties = AccessController.doPrivileged(PropertiesHelper.getSystemProperties());
return systemProperties.getProperty(propertyName);
}
private static synchronized TestContainerFactory getDefaultTestContainerFactory () {
if (defaultTestContainerFactoryClass == null) {
final String factoryClassName = getSystemProperty(TestProperties.CONTAINER_FACTORY);
if (factoryClassName != null) {
LOGGER.log(Level.CONFIG,
"Loading test container factory '{0}' specified in the '{1}' system property.",
new Object[] {factoryClassName, TestProperties.CONTAINER_FACTORY});
defaultTestContainerFactoryClass = loadFactoryClass(factoryClassName);
} else {
final TestContainerFactory[] factories = ServiceFinder.find(TestContainerFactory.class).toArray();
if (factories.length > 0) {
// if there is only one factory instance, just return it
if (factories.length == 1) {
// cache the class for future reuse
defaultTestContainerFactoryClass = factories[0].getClass();
LOGGER.log(
Level.CONFIG,
"Using the single found TestContainerFactory service provider '{0}'",
defaultTestContainerFactoryClass.getName());
return factories[0];
}
// if default factory is present, use it.
for (final TestContainerFactory tcf : factories) {
if (TestProperties.DEFAULT_CONTAINER_FACTORY.equals(tcf.getClass().getName())) {
// cache the class for future reuse
defaultTestContainerFactoryClass = tcf.getClass();
LOGGER.log(
Level.CONFIG,
"Found multiple TestContainerFactory service providers, using the default found '{0}'",
TestProperties.DEFAULT_CONTAINER_FACTORY);
return tcf;
}
}
// default factory is not in the list - log warning and return the first found factory instance
// cache the class for future reuse
defaultTestContainerFactoryClass = factories[0].getClass();
LOGGER.log(
Level.WARNING,
"Found multiple TestContainerFactory service providers, using the first found '{0}'",
defaultTestContainerFactoryClass.getName());
return factories[0];
}
LOGGER.log(
Level.CONFIG,
"No TestContainerFactory configured, trying to load and instantiate the default implementation '{0}'",
TestProperties.DEFAULT_CONTAINER_FACTORY);
defaultTestContainerFactoryClass = loadFactoryClass(TestProperties.DEFAULT_CONTAINER_FACTORY);
}
}
try {
return defaultTestContainerFactoryClass.newInstance();
} catch (final Exception ex) {
throw new TestContainerException(String.format(
"Could not instantiate test container factory '%s'", defaultTestContainerFactoryClass.getName()), ex);
}
}
private static Class extends TestContainerFactory> loadFactoryClass (final String factoryClassName) {
Class extends TestContainerFactory> factoryClass;
final Class