Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ARQ-1321] Support for HTTPS in URLs injected with @ArquillianResource #43

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,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 @@ -52,17 +54,19 @@ 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;
Expand All @@ -88,16 +92,16 @@ private Object locateURL(ArquillianResource resource, TargetsContainer targets)
{
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 Down Expand Up @@ -129,6 +133,18 @@ 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>();
Expand All @@ -139,23 +155,27 @@ 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();
return new URI((secured == null) ? "http" : secured.protocol(), baseURI.getUserInfo(), baseURI.getHost(),
(secured == null) ? baseURI.getPort() : secured.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("http", null, context.getHost(), context.getPort(), null, null, null).toURL();
return new URI((secured == null) ? "http" : secured.protocol(), null, context.getHost(),
(secured == null) ? context.getPort() : secured.port(), null, null, null).toURL();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -36,6 +37,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 All @@ -59,6 +61,30 @@ public void shouldBeAbleToInjectBaseContextURL() throws Exception
Assert.assertEquals("http://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 shouldBeAbleToInjectBaseContextURLQualified() throws Exception
{
Expand All @@ -73,6 +99,34 @@ public void shouldBeAbleToInjectBaseContextURLQualified() throws Exception
Assert.assertEquals("http://TEST-X:8080", 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 shouldBeAbleToInjectServletContextURL() throws Exception
{
Expand All @@ -86,6 +140,32 @@ public void shouldBeAbleToInjectServletContextURL() throws Exception
Assert.assertEquals("http://TEST:8080/test/", 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 shouldBeAbleToInjectServletContextURLQualified() throws Exception
{
Expand Down Expand Up @@ -196,19 +276,55 @@ public static class URLBaseContextClass
@ArquillianResource
public URL url;
}

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

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

public static class URLServletContextClass
{
@ArquillianResource(URLServletContextClass.class)
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 URLBaseContextClassQualified
{
@ArquillianResource @OperateOnDeployment("X")
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, I think @secure may be more appropriate (without the 'd'). Thoughts ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you don't secure it here, you expect to have secured one injected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bartoszmajsak you may close this PR, it's super outdated and this has already been implemented here: https://github.com/arquillian/arquillian-showcase/tree/master/extensions/arquillian-smart-url.
However, this should probably be available in the core instead of the showcase project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just wondering what is the status of this, as it looks like a pretty valid functionality. I will look into the extension then. Thanks for your contribution


/**
* Defines the name of the protocol to use.
*
* @return The protocol name or <code>https</code> if the protocol is undefined
*/
String protocol() 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;
}