Skip to content

Commit

Permalink
[ARQ-1321] Support for HTTPS in URLs injected with @ArquillianResource (
Browse files Browse the repository at this point in the history
#474)

Replaces out of date #43


Signed-off-by: samaxes
Signed-off-by: Scott M Stark <starksm64@gmail.com>
  • Loading branch information
starksm64 authored May 3, 2023
1 parent a5c6162 commit 0394a7c
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .mvn/maven.config
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--builder smart -T1.5C
-T1.5C
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.api.Secured;

/**
* URLResourceProvider
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a>
* @version $Revision: $
*/
public class URLResourceProvider extends OperatesOnDeploymentAwareProvider {
Expand All @@ -48,14 +50,17 @@ public boolean canProvide(Class<?> type) {

@Override
public Object doLookup(ArquillianResource resource, Annotation... qualifiers) {
return locateURL(resource, locateTargetQualification(qualifiers));
return locateURL(resource, qualifiers);
}

private Object locateURL(ArquillianResource resource, TargetsContainer targets) {
private Object locateURL(ArquillianResource resource, Annotation[] qualifiers) {
ProtocolMetaData metaData = protocolMetadata.get();
if (metaData == null) {
return null;
}

TargetsContainer targets = locateTargetQualification(qualifiers);
Secured secured = locateSecureQualification(qualifiers);
if (metaData.hasContext(HTTPContext.class)) {
HTTPContext context = null;
if (targets != null) {
Expand All @@ -74,13 +79,13 @@ private Object locateURL(ArquillianResource resource, TargetsContainer targets)
if (servlet == null) {
return null;
}
return toURL(servlet);
return toURL(servlet, secured);
}
// TODO: evaluate, if all servlets are in the same context, and only one context exists, we can find the context
else if (allInSameContext(context.getServlets())) {
return toURL(context.getServlets().get(0));
return toURL(context.getServlets().get(0), secured);
} else {
return toURL(context);
return toURL(context, secured);
}
}
return null;
Expand All @@ -106,6 +111,15 @@ private TargetsContainer locateTargetQualification(Annotation[] qualifiers) {
return null;
}

private Secured locateSecureQualification(Annotation[] qualifiers) {
for(Annotation qualifier : qualifiers) {
if(Secured.class.isAssignableFrom(qualifier.annotationType())) {
return Secured.class.cast(qualifier);
}
}
return null;
}

private boolean allInSameContext(List<Servlet> servlets) {
Set<String> context = new HashSet<String>();
for (Servlet servlet : servlets) {
Expand All @@ -114,17 +128,24 @@ private boolean allInSameContext(List<Servlet> servlets) {
return context.size() == 1;
}

private URL toURL(Servlet servlet) {
private URL toURL(Servlet servlet, Secured secured) {
try {
return servlet.getBaseURI().toURL();
URI baseURI = servlet.getBaseURI();
String scheme = (secured == null) ? baseURI.getScheme() : secured.scheme();
int port = (secured == null) ? baseURI.getPort() : secured.port();
return new URI(scheme, baseURI.getUserInfo(), baseURI.getHost(),
port, baseURI.getPath(), baseURI.getQuery(),
baseURI.getFragment()).toURL();
} catch (Exception e) {
throw new RuntimeException("Could not convert Servlet to URL, " + servlet, e);
}
}

private URL toURL(HTTPContext context) {
private URL toURL(HTTPContext context, Secured secured) {
try {
return new URI(context.getScheme(), null, context.getHost(), context.getPort(), null, null, null).toURL();
String scheme = (secured == null) ? context.getScheme() : secured.scheme();
int port = (secured == null) ? context.getPort() : secured.port();
return new URI(scheme, null, context.getHost(), port, null, null, null).toURL();
} catch (Exception e) {
throw new RuntimeException("Could not convert HTTPContext to URL, " + context, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.jboss.arquillian.container.test.api.OperateOnDeployment;
import org.jboss.arquillian.container.test.api.TargetsContainer;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.api.Secured;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -34,6 +35,7 @@
* ArquillianTestEnricherTestCase
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a>
* @version $Revision: $
*/
@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -65,6 +67,78 @@ public void shouldBeAbleToInjectBaseContextURLSecure() throws Exception {
Assert.assertEquals("https://TEST:8080", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseSecureContextURL() throws Exception {
SecureURLBaseContextClass test = execute(
SecureURLBaseContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 443)));

Assert.assertEquals("https://TEST", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseSecureContextURLWithPort() throws Exception {
SecureURLWithPortBaseContextClass test = execute(
SecureURLWithPortBaseContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 8443)));

Assert.assertEquals("https://TEST:8443", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseSecureContextURLQualified() throws Exception {
SecureURLBaseContextClassQualified test = execute(
SecureURLBaseContextClassQualified.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST-Y", 443)),
new ProtocolMetaData()
.addContext(new HTTPContext("TEST-X", 443)));

Assert.assertEquals("https://TEST-X", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseSecureContextURLWithPortQualified() throws Exception {
SecureURLWithPortBaseContextClassQualified test = execute(
SecureURLWithPortBaseContextClassQualified.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST-Y", 8443)),
new ProtocolMetaData()
.addContext(new HTTPContext("TEST-X", 8443)));

Assert.assertEquals("https://TEST-X:8443", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectServletSecureContextURL() throws Exception {
SecureURLServletContextClass test = execute(
SecureURLServletContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 443)
.add(new Servlet(SecureURLServletContextClass.class.getSimpleName(), "/test"))));

Assert.assertEquals("https://TEST/test/", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectServletSecureContextURLWithPort() throws Exception {
SecureURLWithPortServletContextClass test = execute(
SecureURLWithPortServletContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 8443)
.add(new Servlet(SecureURLWithPortServletContextClass.class.getSimpleName(), "/test"))));

Assert.assertEquals("https://TEST:8443/test/", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseContextURLQualified() throws Exception {
URLBaseContextClassQualified test = execute(
Expand Down Expand Up @@ -200,6 +274,16 @@ public void shouldThrowExceptionOnUnKnownTargetInDeployment() throws Exception {
.add(new Servlet(URLServletContextClass.class.getSimpleName(), "/test-X"))));
}

public static class SecureURLBaseContextClass {
@ArquillianResource @Secured
public URL url;
}

public static class SecureURLWithPortBaseContextClass {
@ArquillianResource @Secured(port = 8443)
public URL url;
}

public static class URLBaseContextClass {
@ArquillianResource
public URL url;
Expand All @@ -216,6 +300,26 @@ public static class URLBaseContextClassQualified {
public URL url;
}

public static class SecureURLServletContextClass {
@ArquillianResource(SecureURLServletContextClass.class) @Secured
public URL url;
}

public static class SecureURLWithPortServletContextClass {
@ArquillianResource(SecureURLWithPortServletContextClass.class) @Secured(port = 8443)
public URL url;
}

public static class SecureURLBaseContextClassQualified {
@ArquillianResource @OperateOnDeployment("X") @Secured
public URL url;
}

public static class SecureURLWithPortBaseContextClassQualified {
@ArquillianResource @OperateOnDeployment("X") @Secured(port = 8443)
public URL url;
}

public static class URLServletContextClassQualified {
@ArquillianResource(URLServletContextClass.class)
@OperateOnDeployment("X")
Expand Down
57 changes: 57 additions & 0 deletions test/api/src/main/java/org/jboss/arquillian/test/api/Secured.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
* contributors by the @authors tag. See the copyright.txt in the
* distribution for a full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.arquillian.test.api;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Uses the secured protocol for URL and URI injection.
*
* Usage example of field injection:<br />
* <pre><code>
* &#64;ArquillianResource
* &#64;Secured
* private URL url;
* </code></pre>
*
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a>
* @version $Revision: $
*/
@Documented
@Retention(RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
public @interface Secured {

/**
* Defines the name of the protocol to use.
*
* @return The scheme name or <code>https</code> if the scheme is undefined
*/
String scheme() default "https";

/**
* Defines the port number on the host.
*
* @return The port number or <code>-1</code> if the port is undefined
*/
int port() default -1;
}

0 comments on commit 0394a7c

Please sign in to comment.