-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ARQ-1321] Support for HTTPS in URLs injected with @ArquillianResource (
#474) Replaces out of date #43 Signed-off-by: samaxes Signed-off-by: Scott M Stark <[email protected]>
- Loading branch information
Showing
4 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
--builder smart -T1.5C | ||
-T1.5C |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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:[email protected]">Aslak Knutsen</a> | ||
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a> | ||
* @version $Revision: $ | ||
*/ | ||
public class URLResourceProvider extends OperatesOnDeploymentAwareProvider { | ||
|
@@ -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) { | ||
|
@@ -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; | ||
|
@@ -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) { | ||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -34,6 +35,7 @@ | |
* ArquillianTestEnricherTestCase | ||
* | ||
* @author <a href="mailto:[email protected]">Aslak Knutsen</a> | ||
* @author <a href="http://community.jboss.org/people/silenius">Samuel Santos</a> | ||
* @version $Revision: $ | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
|
@@ -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( | ||
|
@@ -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; | ||
|
@@ -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") | ||
|
57 changes: 57 additions & 0 deletions
57
test/api/src/main/java/org/jboss/arquillian/test/api/Secured.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
* @ArquillianResource | ||
* @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; | ||
} |