Thomas Vachuska
Committed by Gerrit Code Review

Added some protection again UiExtensions that return null inclusion streams.

Change-Id: I18545627da38f4c19fc8316d7fd07df44c5c608f
......@@ -16,6 +16,8 @@
package org.onosproject.ui;
import com.google.common.collect.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.util.List;
......@@ -27,6 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/
public class UiExtension {
private final Logger log = LoggerFactory.getLogger(getClass());
private static final String VIEW_PREFIX = "app/view/";
private final String prefix;
......@@ -73,7 +77,7 @@ public class UiExtension {
* @return CSS inclusion statements
*/
public InputStream css() {
return classLoader.getResourceAsStream(prefix + "css.html");
return getStream(prefix + "css.html");
}
/**
......@@ -82,7 +86,7 @@ public class UiExtension {
* @return JavaScript inclusion statements
*/
public InputStream js() {
return classLoader.getResourceAsStream(prefix + "js.html");
return getStream(prefix + "js.html");
}
/**
......@@ -102,8 +106,7 @@ public class UiExtension {
* @return resource input stream
*/
public InputStream resource(String viewId, String path) {
InputStream is = classLoader.getResourceAsStream(VIEW_PREFIX + viewId + "/" + path);
return is;
return getStream(VIEW_PREFIX + viewId + "/" + path);
}
/**
......@@ -114,4 +117,15 @@ public class UiExtension {
public UiMessageHandlerFactory messageHandlerFactory() {
return messageHandlerFactory;
}
// Returns the resource input stream from the specified class-loader.
private InputStream getStream(String path) {
InputStream stream = classLoader.getResourceAsStream(path);
if (stream == null) {
log.warn("Unable to find resource {}", path);
}
return stream;
}
}
......
......@@ -16,7 +16,7 @@
package org.onosproject.ui.impl;
import com.google.common.collect.ImmutableList;
import org.onosproject.ui.UiExtension;
import com.google.common.collect.ImmutableList.Builder;
import org.onosproject.ui.UiExtensionService;
import javax.ws.rs.GET;
......@@ -70,20 +70,23 @@ public class MainIndexResource extends AbstractInjectionResource {
// Produces an input stream including CSS injections from all extensions.
private InputStream includeCss(UiExtensionService service) {
ImmutableList.Builder<InputStream> builder = ImmutableList.builder();
for (UiExtension extension : service.getExtensions()) {
builder.add(extension.css());
}
Builder<InputStream> builder = ImmutableList.builder();
service.getExtensions().forEach(ext -> add(builder, ext.css()));
return new SequenceInputStream(new StreamEnumeration(builder.build()));
}
// Produces an input stream including JS injections from all extensions.
private InputStream includeJs(UiExtensionService service) {
ImmutableList.Builder<InputStream> builder = ImmutableList.builder();
for (UiExtension extension : service.getExtensions()) {
builder.add(extension.js());
}
Builder<InputStream> builder = ImmutableList.builder();
service.getExtensions().forEach(ext -> add(builder, ext.js()));
return new SequenceInputStream(new StreamEnumeration(builder.build()));
}
// Safely adds the stream to the list builder only if stream is not null.
private void add(Builder<InputStream> builder, InputStream inputStream) {
if (inputStream != null) {
builder.add(inputStream);
}
}
}
......