Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList.Builder; |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 21 | import org.onosproject.rest.AbstractInjectionResource; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 22 | import org.onosproject.ui.UiExtensionService; |
| 23 | |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.io.SequenceInputStream; |
| 32 | |
| 33 | import static com.google.common.collect.ImmutableList.of; |
| 34 | import static com.google.common.io.ByteStreams.toByteArray; |
| 35 | |
| 36 | /** |
| 37 | * Resource for serving the dynamically composed index.html. |
| 38 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 39 | @Path("/") |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | public class MainIndexResource extends AbstractInjectionResource { |
| 41 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 42 | private static final String INDEX = "index.html"; |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 43 | private static final String NOT_READY = "not-ready.html"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 45 | private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->"; |
| 46 | private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 47 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 48 | private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->"; |
| 49 | private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->"; |
| 50 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 51 | @GET |
| 52 | @Produces(MediaType.TEXT_HTML) |
| 53 | public Response getMainIndex() throws IOException { |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 54 | ClassLoader classLoader = getClass().getClassLoader(); |
| 55 | UiExtensionService service; |
| 56 | try { |
| 57 | service = get(UiExtensionService.class); |
| 58 | } catch (ServiceNotFoundException e) { |
| 59 | return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build(); |
| 60 | } |
| 61 | |
| 62 | InputStream indexTemplate = classLoader.getResourceAsStream(INDEX); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 63 | String index = new String(toByteArray(indexTemplate)); |
| 64 | |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 65 | int p1s = split(index, 0, INJECT_JS_START) - INJECT_JS_START.length(); |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 66 | int p1e = split(index, p1s, INJECT_JS_END); |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 67 | int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length(); |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 68 | int p2e = split(index, p2s, INJECT_CSS_END); |
| 69 | int p3s = split(index, p2e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 70 | |
| 71 | StreamEnumeration streams = |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 72 | new StreamEnumeration(of(stream(index, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 73 | includeJs(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 74 | stream(index, p1e, p2s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 75 | includeCss(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 76 | stream(index, p2e, p3s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 77 | |
| 78 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 79 | } |
| 80 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 81 | // Produces an input stream including JS injections from all extensions. |
| 82 | private InputStream includeJs(UiExtensionService service) { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 83 | Builder<InputStream> builder = ImmutableList.builder(); |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 84 | service.getExtensions().forEach(ext -> { |
| 85 | add(builder, ext.js()); |
| 86 | add(builder, new NewlineInputStream()); |
| 87 | }); |
| 88 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 89 | } |
| 90 | |
| 91 | // Produces an input stream including CSS injections from all extensions. |
| 92 | private InputStream includeCss(UiExtensionService service) { |
| 93 | Builder<InputStream> builder = ImmutableList.builder(); |
| 94 | service.getExtensions().forEach(ext -> { |
| 95 | add(builder, ext.css()); |
| 96 | add(builder, new NewlineInputStream()); |
| 97 | }); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 98 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 99 | } |
| 100 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 101 | // Safely adds the stream to the list builder only if stream is not null. |
| 102 | private void add(Builder<InputStream> builder, InputStream inputStream) { |
| 103 | if (inputStream != null) { |
| 104 | builder.add(inputStream); |
| 105 | } |
| 106 | } |
| 107 | |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 108 | private static final String NL = String.format("%n"); |
| 109 | private static final byte[] NL_BYTES = NL.getBytes(); |
| 110 | |
| 111 | private static class NewlineInputStream extends InputStream { |
| 112 | private int index = 0; |
| 113 | |
| 114 | @Override |
| 115 | public int read() throws IOException { |
| 116 | if (index == NL_BYTES.length) { |
| 117 | return -1; |
| 118 | } |
| 119 | return NL_BYTES[index++]; |
| 120 | } |
| 121 | } |
| 122 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 123 | } |