Thomas Vachuska
Committed by Gerrit Code Review

Added some protection again UiExtensions that return null inclusion streams.

Change-Id: I18545627da38f4c19fc8316d7fd07df44c5c608f
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
16 package org.onosproject.ui; 16 package org.onosproject.ui;
17 17
18 import com.google.common.collect.ImmutableList; 18 import com.google.common.collect.ImmutableList;
19 +import org.slf4j.Logger;
20 +import org.slf4j.LoggerFactory;
19 21
20 import java.io.InputStream; 22 import java.io.InputStream;
21 import java.util.List; 23 import java.util.List;
...@@ -27,6 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -27,6 +29,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
27 */ 29 */
28 public class UiExtension { 30 public class UiExtension {
29 31
32 + private final Logger log = LoggerFactory.getLogger(getClass());
33 +
30 private static final String VIEW_PREFIX = "app/view/"; 34 private static final String VIEW_PREFIX = "app/view/";
31 35
32 private final String prefix; 36 private final String prefix;
...@@ -73,7 +77,7 @@ public class UiExtension { ...@@ -73,7 +77,7 @@ public class UiExtension {
73 * @return CSS inclusion statements 77 * @return CSS inclusion statements
74 */ 78 */
75 public InputStream css() { 79 public InputStream css() {
76 - return classLoader.getResourceAsStream(prefix + "css.html"); 80 + return getStream(prefix + "css.html");
77 } 81 }
78 82
79 /** 83 /**
...@@ -82,7 +86,7 @@ public class UiExtension { ...@@ -82,7 +86,7 @@ public class UiExtension {
82 * @return JavaScript inclusion statements 86 * @return JavaScript inclusion statements
83 */ 87 */
84 public InputStream js() { 88 public InputStream js() {
85 - return classLoader.getResourceAsStream(prefix + "js.html"); 89 + return getStream(prefix + "js.html");
86 } 90 }
87 91
88 /** 92 /**
...@@ -102,8 +106,7 @@ public class UiExtension { ...@@ -102,8 +106,7 @@ public class UiExtension {
102 * @return resource input stream 106 * @return resource input stream
103 */ 107 */
104 public InputStream resource(String viewId, String path) { 108 public InputStream resource(String viewId, String path) {
105 - InputStream is = classLoader.getResourceAsStream(VIEW_PREFIX + viewId + "/" + path); 109 + return getStream(VIEW_PREFIX + viewId + "/" + path);
106 - return is;
107 } 110 }
108 111
109 /** 112 /**
...@@ -114,4 +117,15 @@ public class UiExtension { ...@@ -114,4 +117,15 @@ public class UiExtension {
114 public UiMessageHandlerFactory messageHandlerFactory() { 117 public UiMessageHandlerFactory messageHandlerFactory() {
115 return messageHandlerFactory; 118 return messageHandlerFactory;
116 } 119 }
120 +
121 + // Returns the resource input stream from the specified class-loader.
122 + private InputStream getStream(String path) {
123 + InputStream stream = classLoader.getResourceAsStream(path);
124 + if (stream == null) {
125 + log.warn("Unable to find resource {}", path);
126 + }
127 + return stream;
128 + }
129 +
130 +
117 } 131 }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 package org.onosproject.ui.impl; 16 package org.onosproject.ui.impl;
17 17
18 import com.google.common.collect.ImmutableList; 18 import com.google.common.collect.ImmutableList;
19 -import org.onosproject.ui.UiExtension; 19 +import com.google.common.collect.ImmutableList.Builder;
20 import org.onosproject.ui.UiExtensionService; 20 import org.onosproject.ui.UiExtensionService;
21 21
22 import javax.ws.rs.GET; 22 import javax.ws.rs.GET;
...@@ -70,20 +70,23 @@ public class MainIndexResource extends AbstractInjectionResource { ...@@ -70,20 +70,23 @@ public class MainIndexResource extends AbstractInjectionResource {
70 70
71 // Produces an input stream including CSS injections from all extensions. 71 // Produces an input stream including CSS injections from all extensions.
72 private InputStream includeCss(UiExtensionService service) { 72 private InputStream includeCss(UiExtensionService service) {
73 - ImmutableList.Builder<InputStream> builder = ImmutableList.builder(); 73 + Builder<InputStream> builder = ImmutableList.builder();
74 - for (UiExtension extension : service.getExtensions()) { 74 + service.getExtensions().forEach(ext -> add(builder, ext.css()));
75 - builder.add(extension.css());
76 - }
77 return new SequenceInputStream(new StreamEnumeration(builder.build())); 75 return new SequenceInputStream(new StreamEnumeration(builder.build()));
78 } 76 }
79 77
80 // Produces an input stream including JS injections from all extensions. 78 // Produces an input stream including JS injections from all extensions.
81 private InputStream includeJs(UiExtensionService service) { 79 private InputStream includeJs(UiExtensionService service) {
82 - ImmutableList.Builder<InputStream> builder = ImmutableList.builder(); 80 + Builder<InputStream> builder = ImmutableList.builder();
83 - for (UiExtension extension : service.getExtensions()) { 81 + service.getExtensions().forEach(ext -> add(builder, ext.js()));
84 - builder.add(extension.js());
85 - }
86 return new SequenceInputStream(new StreamEnumeration(builder.build())); 82 return new SequenceInputStream(new StreamEnumeration(builder.build()));
87 } 83 }
88 84
85 + // Safely adds the stream to the list builder only if stream is not null.
86 + private void add(Builder<InputStream> builder, InputStream inputStream) {
87 + if (inputStream != null) {
88 + builder.add(inputStream);
89 + }
90 + }
91 +
89 } 92 }
......