Search in sources :

Example 1 with Container

use of org.jboss.arquillian.container.spi.Container in project tomee by apache.

the class MicroProfileJWTTCKArchiveProcessor method process.

@Override
public void process(final Archive<?> applicationArchive, final TestClass testClass) {
    if (!(applicationArchive instanceof WebArchive)) {
        return;
    }
    final WebArchive war = WebArchive.class.cast(applicationArchive);
    // Add Required Libraries
    war.addAsLibrary(JarLocation.jarLocation(TokenUtils.class)).addAsLibrary(JarLocation.jarLocation(JWSSigner.class)).addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    // Provide keys required for tests (vendor specific way)
    war.addClass(JWTAuthContextInfoProvider.class);
    // Spec says that vendor specific ways to load the keys take precedence, so we need to remove it in test
    // cases that use the Config approach.
    Stream.of(PublicKeyAsPEMTest.class, PublicKeyAsPEMLocationTest.class, PublicKeyAsFileLocationURLTest.class, PublicKeyAsJWKTest.class, PublicKeyAsBase64JWKTest.class, PublicKeyAsJWKLocationTest.class, PublicKeyAsJWKLocationURLTest.class, PublicKeyAsJWKSTest.class, PublicKeyAsJWKSLocationTest.class, IssValidationTest.class, ExpClaimValidationTest.class, ExpClaimAllowMissingExpValidationTest.class, org.apache.tomee.microprofile.tck.jwt.config.PublicKeyAsPEMLocationTest.class, org.apache.tomee.microprofile.tck.jwt.config.PublicKeyAsJWKLocationURLTest.class).filter(c -> c.equals(testClass.getJavaClass())).findAny().ifPresent(c -> war.deleteClass(JWTAuthContextInfoProvider.class));
    // MP Config in wrong place - See https://github.com/eclipse/microprofile/issues/46.
    final Map<ArchivePath, Node> content = war.getContent(object -> object.get().matches(".*META-INF/.*"));
    content.forEach((archivePath, node) -> war.addAsResource(node.getAsset(), node.getPath()));
    // Rewrite the correct server port in configuration
    final Container container = containerRegistry.get().getContainer(TargetDescription.DEFAULT);
    if (container.getDeployableContainer() instanceof RemoteTomEEContainer) {
        final RemoteTomEEContainer remoteTomEEContainer = (RemoteTomEEContainer) container.getDeployableContainer();
        final RemoteTomEEConfiguration configuration = remoteTomEEContainer.getConfiguration();
        final String httpPort = configuration.getHttpPort() + "";
        final Map<ArchivePath, Node> microprofileProperties = war.getContent(object -> object.get().matches(".*META-INF/microprofile-config\\.properties"));
        microprofileProperties.forEach((archivePath, node) -> {
            try {
                final Properties properties = new Properties();
                properties.load(node.getAsset().openStream());
                properties.replaceAll((key, value) -> ((String) value).replaceAll("8080", httpPort + "/" + "KeyEndpoint.war".replaceAll("\\.war", "")));
                final StringWriter stringWriter = new StringWriter();
                properties.store(stringWriter, null);
                war.delete(archivePath);
                war.add(new StringAsset(stringWriter.toString()), node.getPath());
            } catch (final IOException e) {
                e.printStackTrace();
            }
        });
    }
    System.out.println(war.toString(true));
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) ExpClaimAllowMissingExpValidationTest(org.apache.tomee.microprofile.tck.jwt.validation.ExpClaimAllowMissingExpValidationTest) Node(org.jboss.shrinkwrap.api.Node) TokenUtils(org.eclipse.microprofile.jwt.tck.util.TokenUtils) Properties(java.util.Properties) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) RemoteTomEEContainer(org.apache.tomee.arquillian.remote.RemoteTomEEContainer) Container(org.jboss.arquillian.container.spi.Container) StringWriter(java.io.StringWriter) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) RemoteTomEEConfiguration(org.apache.tomee.arquillian.remote.RemoteTomEEConfiguration) IOException(java.io.IOException) RemoteTomEEContainer(org.apache.tomee.arquillian.remote.RemoteTomEEContainer) ExpClaimValidationTest(org.apache.tomee.microprofile.tck.jwt.validation.ExpClaimValidationTest)

Example 2 with Container

use of org.jboss.arquillian.container.spi.Container in project wildfly-swarm by wildfly-swarm.

the class SwarmURLResourceProvider method doLookup.

@Override
public Object doLookup(ArquillianResource resource, Annotation... __) {
    Container container = containerInstance.get();
    String javaVmArguments = null;
    String portDefinedInProperties = null;
    String offsetDefinedInProperties = null;
    if (container != null) {
        javaVmArguments = container.getContainerConfiguration().getContainerProperties().get("javaVmArguments");
    }
    if (javaVmArguments != null) {
        if (javaVmArguments.contains(SwarmProperties.HTTP_PORT) || javaVmArguments.contains(SwarmProperties.PORT_OFFSET)) {
            String[] properties = javaVmArguments.split("=| ");
            // each property must have a value
            if (properties.length % 2 != 0) {
                throw new IllegalArgumentException("Cannot parse java VM arguments " + javaVmArguments);
            }
            for (int i = 0; i < properties.length; i++) {
                if (properties[i].contains(SwarmProperties.HTTP_PORT)) {
                    portDefinedInProperties = properties[i + 1];
                }
                if (properties[i].contains(SwarmProperties.PORT_OFFSET)) {
                    offsetDefinedInProperties = properties[i + 1];
                }
            }
        }
    }
    // first cut - try to get the data from the sysprops
    // this will fail if the user sets any of these via code
    String host = System.getProperty(SwarmProperties.BIND_ADDRESS);
    if (host == null || host.equals("0.0.0.0")) {
        host = "localhost";
    }
    int port = 8080;
    final String portString = portDefinedInProperties != null ? portDefinedInProperties : System.getProperty(SwarmProperties.HTTP_PORT);
    final String portOffset = offsetDefinedInProperties != null ? offsetDefinedInProperties : System.getProperty(SwarmProperties.PORT_OFFSET);
    if (portString != null) {
        port = Integer.parseInt(portString);
    }
    if (portOffset != null) {
        port = port + Integer.parseInt(portOffset);
    }
    String contextPath = System.getProperty(SwarmProperties.CONTEXT_PATH);
    DeploymentContext deploymentContext = this.deploymentContext.get();
    if (deploymentContext != null && deploymentContext.isActive()) {
        if (deploymentContext.getObjectStore().get(ContextRoot.class) != null) {
            contextPath = deploymentContext.getObjectStore().get(ContextRoot.class).context();
        }
    }
    if (contextPath == null) {
        contextPath = "/";
    }
    if (!contextPath.startsWith("/")) {
        contextPath = "/" + contextPath;
    }
    try {
        return new URI("http", null, host, port, contextPath, null, null).toURL();
    } catch (MalformedURLException | URISyntaxException e) {
        throw new RuntimeException(e);
    }
}
Also used : DeploymentContext(org.jboss.arquillian.container.spi.context.DeploymentContext) Container(org.jboss.arquillian.container.spi.Container) MalformedURLException(java.net.MalformedURLException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Aggregations

Container (org.jboss.arquillian.container.spi.Container)2 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Properties (java.util.Properties)1 RemoteTomEEConfiguration (org.apache.tomee.arquillian.remote.RemoteTomEEConfiguration)1 RemoteTomEEContainer (org.apache.tomee.arquillian.remote.RemoteTomEEContainer)1 ExpClaimAllowMissingExpValidationTest (org.apache.tomee.microprofile.tck.jwt.validation.ExpClaimAllowMissingExpValidationTest)1 ExpClaimValidationTest (org.apache.tomee.microprofile.tck.jwt.validation.ExpClaimValidationTest)1 TokenUtils (org.eclipse.microprofile.jwt.tck.util.TokenUtils)1 DeploymentContext (org.jboss.arquillian.container.spi.context.DeploymentContext)1 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)1 Node (org.jboss.shrinkwrap.api.Node)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)1