Skip to content
Snippets Groups Projects
Commit 141ab6a1 authored by Ryan Izard's avatar Ryan Izard
Browse files

Update Web UI to default to /ui/index.html for any request to /ui/. Also add...

Update Web UI to default to /ui/index.html for any request to /ui/. Also add in a filter for allowing any origin.
parent 1fd8ef35
No related branches found
No related tags found
No related merge requests found
...@@ -29,10 +29,12 @@ import org.restlet.Request; ...@@ -29,10 +29,12 @@ import org.restlet.Request;
import org.restlet.Response; import org.restlet.Response;
import org.restlet.Restlet; import org.restlet.Restlet;
import org.restlet.Server; import org.restlet.Server;
import org.restlet.data.Header;
import org.restlet.data.Parameter; import org.restlet.data.Parameter;
import org.restlet.data.Protocol; import org.restlet.data.Protocol;
import org.restlet.data.Reference; import org.restlet.data.Reference;
import org.restlet.data.Status; import org.restlet.data.Status;
import org.restlet.engine.header.HeaderConstants;
import org.restlet.ext.jackson.JacksonRepresentation; import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.representation.Representation; import org.restlet.representation.Representation;
import org.restlet.routing.Filter; import org.restlet.routing.Filter;
...@@ -60,6 +62,8 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -60,6 +62,8 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
private static String httpsNeedClientAuth = "true"; private static String httpsNeedClientAuth = "true";
private static boolean accessControlAllowAllOrigins = false;
private static boolean useHttps = false; private static boolean useHttps = false;
private static boolean useHttp = false; private static boolean useHttp = false;
...@@ -101,6 +105,53 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -101,6 +105,53 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
} }
}; };
if (accessControlAllowAllOrigins) {
Filter crossAccessAllowAll = new Filter() {
@Override
protected int beforeHandle(Request request, Response response) {
// Initialize response headers
@SuppressWarnings("unchecked")
Series<Header> responseHeaders = (Series<Header>) response
.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
if (responseHeaders == null) {
responseHeaders = new Series<Header>(Header.class);
}
// Request headers
@SuppressWarnings("unchecked")
Series<Header> requestHeaders = (Series<Header>) request
.getAttributes().get(HeaderConstants.ATTRIBUTE_HEADERS);
String requestOrigin = requestHeaders.getFirstValue("Origin",
false, "*");
String rh = requestHeaders.getFirstValue(
"Access-Control-Request-Headers", false, "*");
// Set CORS headers in response
responseHeaders.set(
"Access-Control-Expose-Headers",
"Authorization, Link");
responseHeaders.set("Access-Control-Allow-Credentials", "true");
responseHeaders.set("Access-Control-Allow-Methods",
"GET,POST,PUT,DELETE");
responseHeaders.set("Access-Control-Allow-Origin", requestOrigin);
responseHeaders.set("Access-Control-Allow-Headers", rh);
// Set response headers
response.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS,
responseHeaders);
// Handle HTTP methods
if (org.restlet.data.Method.OPTIONS.equals(request.getMethod())) {
return Filter.STOP;
}
return Filter.CONTINUE;
}
};
crossAccessAllowAll.setNext(slashFilter);
}
slashFilter.setNext(baseRouter); slashFilter.setNext(baseRouter);
return slashFilter; return slashFilter;
...@@ -169,6 +220,15 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -169,6 +220,15 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
} }
} }
/*CorsService corsService = new CorsService();
corsService.setAllowedOrigins(new HashSet<String>(Arrays.asList("*")));
corsService.setAllowedCredentials(true);
corsService.setContext(context);
corsService.setAllowingAllRequestedHeaders(true);
corsService.setEnabled(true);
this.getServices().add(corsService);*/
component.getClients().add(Protocol.CLAP); component.getClients().add(Protocol.CLAP);
component.getDefaultHost().attach(this); component.getDefaultHost().attach(this);
component.start(); component.start();
...@@ -259,6 +319,7 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -259,6 +319,7 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
String useHttps = configOptions.get("useHttps"); String useHttps = configOptions.get("useHttps");
String useHttp = configOptions.get("useHttp"); String useHttp = configOptions.get("useHttp");
String httpsNeedClientAuth = configOptions.get("httpsNeedClientAuthentication"); String httpsNeedClientAuth = configOptions.get("httpsNeedClientAuthentication");
String accessControlAllowOrigin = configOptions.get("accessControlAllowAllOrigins");
/* HTTPS Access (ciphertext) */ /* HTTPS Access (ciphertext) */
if (useHttps == null || path == null || path.isEmpty() || if (useHttps == null || path == null || path.isEmpty() ||
...@@ -305,7 +366,7 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -305,7 +366,7 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
RestApiServer.httpPort = port.trim(); RestApiServer.httpPort = port.trim();
} }
} }
if (RestApiServer.useHttp && RestApiServer.useHttps && RestApiServer.httpPort.equals(RestApiServer.httpsPort)) { if (RestApiServer.useHttp && RestApiServer.useHttps && RestApiServer.httpPort.equals(RestApiServer.httpsPort)) {
logger.error("REST API's HTTP and HTTPS ports cannot be the same. Got " + RestApiServer.httpPort + " for both."); logger.error("REST API's HTTP and HTTPS ports cannot be the same. Got " + RestApiServer.httpPort + " for both.");
throw new IllegalArgumentException("REST API's HTTP and HTTPS ports cannot be the same. Got " + RestApiServer.httpPort + " for both."); throw new IllegalArgumentException("REST API's HTTP and HTTPS ports cannot be the same. Got " + RestApiServer.httpPort + " for both.");
...@@ -327,6 +388,13 @@ public class RestApiServer implements IFloodlightModule, IRestApiService { ...@@ -327,6 +388,13 @@ public class RestApiServer implements IFloodlightModule, IRestApiService {
} else { } else {
logger.warn("HTTP enabled; Allowing unsecure access to REST API on port {}.", RestApiServer.httpPort); logger.warn("HTTP enabled; Allowing unsecure access to REST API on port {}.", RestApiServer.httpPort);
} }
if (accessControlAllowOrigin != null) {
try {
RestApiServer.accessControlAllowAllOrigins = Boolean.parseBoolean(accessControlAllowOrigin);
} catch (Exception e) { }
logger.warn("CORS access control allow ALL origins: {}", RestApiServer.accessControlAllowAllOrigins);
}
} }
@Override @Override
......
...@@ -72,7 +72,9 @@ public class StaticWebRoutable implements RestletRoutable, IFloodlightModule { ...@@ -72,7 +72,9 @@ public class StaticWebRoutable implements RestletRoutable, IFloodlightModule {
@Override @Override
public Restlet getRestlet(Context context) { public Restlet getRestlet(Context context) {
Router router = new Router(context); Router router = new Router(context);
router.attach("", new Directory(context, "clap://classloader/web/")); Directory dir = new Directory(context, "clap://classloader/web/");
dir.setIndexName("index.html"); /* redirect from <ip>:<port>/ui/ --> /ui/index.html */
router.attach("", dir);
context.setClientDispatcher(new Client(context, Protocol.CLAP)); context.setClientDispatcher(new Client(context, Protocol.CLAP));
return router; return router;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment