blob: dd1ef9a38212c7728d4130baa9ff64fa6b4f136e [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;
19import org.onosproject.ui.UiExtension;
20import 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 Vachuskae95da772015-02-23 15:50:11 -080037@Path("/index.html")
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) {
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}