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