Skip to content

Commit

Permalink
[422] Allow the HTTPContext to indicate it's a secure context and htt…
Browse files Browse the repository at this point in the history
…ps should be used.

Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Aug 15, 2022
1 parent 93e6790 commit 7ecd26d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,40 @@
public class HTTPContext extends NamedContext {
private final String host;
private final int port;
private final boolean secure;

private final Set<Servlet> servlets;

public HTTPContext(String host, int port) {
this("no-named", host, port);
this("no-named", host, port, false);
}

public HTTPContext(String host, int port, boolean secure) {
this("no-named", host, port, secure);
}

public HTTPContext(String name, String host, int port) {
this(name, host, port, false);
}

/**
* Creates a new HTTP context.
*
* @param name the name for the context
* @param host the host for the context
* @param port the port for the context
* @param secure whether this is a secure context. {@code true} will result in a https protocol, otherwise http will
* be used for the protocol
*/
public HTTPContext(String name, String host, int port, boolean secure) {
super(name);

if (host == null) {
throw new IllegalArgumentException("host must not be null");
}
this.host = host;
this.port = port;
this.secure = secure;
this.servlets = new HashSet<Servlet>();
}

Expand Down Expand Up @@ -85,9 +104,18 @@ public Servlet getServletByName(String name) {
return null;
}

/**
* Indicates whether this is a secure HTTP connection (https).
*
* @return {@code true} if this should be a secure connection, otherwise {@code false}
*/
public boolean isSecure() {
return secure;
}

@Override
public String toString() {
return "HTTPContext [host=" + host + ", port=" + port + ", servlets=" + toString(servlets) + "]";
return "HTTPContext [host=" + host + ", port=" + port + ", secure=" + secure + ", servlets=" + toString(servlets) + "]";
}

private String toString(Set<Servlet> servlets) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
*/
public class Servlet {
static final String HTTP_SCHEME = "http://";
static final String HTTPS_SCHEME = "https://";
static final String ROOT_CONTEXT = "/";

private final String name;

private final String contextRoot;

private String scheme;
private String host;

private int port;
Expand Down Expand Up @@ -68,6 +70,7 @@ void setParent(HTTPContext context) {
if (context.getHost() == null) {
throw new IllegalArgumentException(context.getClass().getSimpleName() + " host must not be null");
}
this.scheme = context.isSecure() ? HTTPS_SCHEME : HTTP_SCHEME;
this.host = context.getHost();
this.port = context.getPort();
}
Expand Down Expand Up @@ -105,7 +108,7 @@ public URI getFullURI() {
}

private String getBaseURIAsString() {
return HTTP_SCHEME + host + ":" + port + contextRoot + "/";
return scheme + host + ":" + port + contextRoot + "/";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private URL toURL(Servlet servlet) {

private URL toURL(HTTPContext context) {
try {
return new URI("http", null, context.getHost(), context.getPort(), null, null, null).toURL();
return new URI(context.isSecure() ? "https" : "http", null, context.getHost(), context.getPort(), 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 @@ -54,6 +54,17 @@ public void shouldBeAbleToInjectBaseContextURL() throws Exception {
Assert.assertEquals("http://TEST:8080", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectBaseContextURLSecure() throws Exception {
URLBaseContextClass test = execute(
URLBaseContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 8080, true)));

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

@Test
public void shouldBeAbleToInjectBaseContextURLQualified() throws Exception {
URLBaseContextClassQualified test = execute(
Expand All @@ -79,6 +90,18 @@ public void shouldBeAbleToInjectServletContextURL() throws Exception {
Assert.assertEquals("http://TEST:8080/test/", test.url.toExternalForm());
}

@Test
public void shouldBeAbleToInjectServletContextURLSecure() throws Exception {
URLServletContextClass test = execute(
URLServletContextClass.class,
ProtocolMetaData.class,
new ProtocolMetaData()
.addContext(new HTTPContext("TEST", 8080, true)
.add(new Servlet(URLServletContextClass.class.getSimpleName(), "/test"))));

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

@Test
public void shouldBeAbleToInjectServletContextURLQualified() throws Exception {
URLServletContextClassQualified test = execute(
Expand Down

0 comments on commit 7ecd26d

Please sign in to comment.