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 | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 21 | import org.onosproject.ui.UiExtensionService; |
| 22 | |
| 23 | import javax.ws.rs.GET; |
| 24 | import javax.ws.rs.Path; |
| 25 | import javax.ws.rs.Produces; |
| 26 | import javax.ws.rs.core.MediaType; |
| 27 | import javax.ws.rs.core.Response; |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.SequenceInputStream; |
| 31 | |
| 32 | import static com.google.common.collect.ImmutableList.of; |
| 33 | import static com.google.common.io.ByteStreams.toByteArray; |
| 34 | |
| 35 | /** |
| 36 | * Resource for serving the dynamically composed index.html. |
| 37 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 38 | @Path("/") |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 39 | public class MainIndexResource extends AbstractInjectionResource { |
| 40 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 41 | private static final String INDEX = "index.html"; |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 42 | private static final String NOT_READY = "not-ready.html"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 43 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 44 | private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->"; |
| 45 | private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 46 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 47 | private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->"; |
| 48 | private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->"; |
| 49 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 50 | @GET |
| 51 | @Produces(MediaType.TEXT_HTML) |
| 52 | public Response getMainIndex() throws IOException { |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 53 | ClassLoader classLoader = getClass().getClassLoader(); |
| 54 | UiExtensionService service; |
| 55 | try { |
| 56 | service = get(UiExtensionService.class); |
| 57 | } catch (ServiceNotFoundException e) { |
| 58 | return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build(); |
| 59 | } |
| 60 | |
| 61 | InputStream indexTemplate = classLoader.getResourceAsStream(INDEX); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 62 | String index = new String(toByteArray(indexTemplate)); |
| 63 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 64 | int p1s = split(index, 0, INJECT_JS_START); |
| 65 | int p1e = split(index, p1s, INJECT_JS_END); |
| 66 | int p2s = split(index, p1e, INJECT_CSS_START); |
| 67 | int p2e = split(index, p2s, INJECT_CSS_END); |
| 68 | int p3s = split(index, p2e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | |
| 70 | StreamEnumeration streams = |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 71 | new StreamEnumeration(of(stream(index, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 72 | includeJs(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 73 | stream(index, p1e, p2s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 74 | includeCss(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 75 | stream(index, p2e, p3s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 76 | |
| 77 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 78 | } |
| 79 | |
| 80 | // Produces an input stream including CSS injections from all extensions. |
| 81 | private InputStream includeCss(UiExtensionService service) { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 82 | Builder<InputStream> builder = ImmutableList.builder(); |
| 83 | service.getExtensions().forEach(ext -> add(builder, ext.css())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 84 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 85 | } |
| 86 | |
| 87 | // Produces an input stream including JS injections from all extensions. |
| 88 | private InputStream includeJs(UiExtensionService service) { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 89 | Builder<InputStream> builder = ImmutableList.builder(); |
| 90 | service.getExtensions().forEach(ext -> add(builder, ext.js())); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 91 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 92 | } |
| 93 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 94 | // Safely adds the stream to the list builder only if stream is not null. |
| 95 | private void add(Builder<InputStream> builder, InputStream inputStream) { |
| 96 | if (inputStream != null) { |
| 97 | builder.add(inputStream); |
| 98 | } |
| 99 | } |
| 100 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 101 | } |