blob: 9013fedf863a0e94dcc4f023fd488ed9b474a612 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
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 */
16package org.onosproject.ui.impl;
17
18import com.google.common.collect.ImmutableList;
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070019import com.google.common.collect.ImmutableList.Builder;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080020import org.onosproject.ui.UiExtensionService;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
24import javax.ws.rs.Produces;
25import javax.ws.rs.core.MediaType;
26import javax.ws.rs.core.Response;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.SequenceInputStream;
30
31import static com.google.common.collect.ImmutableList.of;
32import static com.google.common.io.ByteStreams.toByteArray;
33
34/**
35 * Resource for serving the dynamically composed index.html.
36 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080037@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080038public class MainIndexResource extends AbstractInjectionResource {
39
Thomas Vachuskae95da772015-02-23 15:50:11 -080040 private static final String INDEX = "index.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080041
Thomas Vachuskaa0509892015-02-21 22:18:41 -080042 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
43 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080044
Thomas Vachuskaa0509892015-02-21 22:18:41 -080045 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
46 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
47
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080048 @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 Vachuskaa0509892015-02-21 22:18:41 -080055 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 Vachuskafe8c98a2015-02-04 01:24:32 -080060
61 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080062 new StreamEnumeration(of(stream(index, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063 includeJs(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080064 stream(index, p1e, p2s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080065 includeCss(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080066 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080067
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) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070073 Builder<InputStream> builder = ImmutableList.builder();
74 service.getExtensions().forEach(ext -> add(builder, ext.css()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080075 return new SequenceInputStream(new StreamEnumeration(builder.build()));
76 }
77
78 // Produces an input stream including JS injections from all extensions.
79 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070080 Builder<InputStream> builder = ImmutableList.builder();
81 service.getExtensions().forEach(ext -> add(builder, ext.js()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080082 return new SequenceInputStream(new StreamEnumeration(builder.build()));
83 }
84
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070085 // 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
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080092}