blob: a30c047041e13512c6d51e5af513a381cdb86d56 [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 Vachuskaa02fc812015-07-21 18:37:58 -070020import org.onlab.osgi.ServiceNotFoundException;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070021import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080022import org.onosproject.ui.UiExtensionService;
23
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.SequenceInputStream;
32
33import static com.google.common.collect.ImmutableList.of;
34import static com.google.common.io.ByteStreams.toByteArray;
35
36/**
37 * Resource for serving the dynamically composed index.html.
38 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080039@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040public class MainIndexResource extends AbstractInjectionResource {
41
Thomas Vachuskae95da772015-02-23 15:50:11 -080042 private static final String INDEX = "index.html";
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070043 private static final String NOT_READY = "not-ready.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080044
Thomas Vachuskaa0509892015-02-21 22:18:41 -080045 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
46 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047
Thomas Vachuskaa0509892015-02-21 22:18:41 -080048 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
49 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
50
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080051 @GET
52 @Produces(MediaType.TEXT_HTML)
53 public Response getMainIndex() throws IOException {
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070054 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 Vachuskafe8c98a2015-02-04 01:24:32 -080063 String index = new String(toByteArray(indexTemplate));
64
Thomas Vachuskaa0509892015-02-21 22:18:41 -080065 int p1s = split(index, 0, INJECT_JS_START);
66 int p1e = split(index, p1s, INJECT_JS_END);
67 int p2s = split(index, p1e, INJECT_CSS_START);
68 int p2e = split(index, p2s, INJECT_CSS_END);
69 int p3s = split(index, p2e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080070
71 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080072 new StreamEnumeration(of(stream(index, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080073 includeJs(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080074 stream(index, p1e, p2s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080075 includeCss(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080076 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080077
78 return Response.ok(new SequenceInputStream(streams)).build();
79 }
80
81 // Produces an input stream including CSS injections from all extensions.
82 private InputStream includeCss(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070083 Builder<InputStream> builder = ImmutableList.builder();
84 service.getExtensions().forEach(ext -> add(builder, ext.css()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080085 return new SequenceInputStream(new StreamEnumeration(builder.build()));
86 }
87
88 // Produces an input stream including JS injections from all extensions.
89 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070090 Builder<InputStream> builder = ImmutableList.builder();
91 service.getExtensions().forEach(ext -> add(builder, ext.js()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080092 return new SequenceInputStream(new StreamEnumeration(builder.build()));
93 }
94
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070095 // Safely adds the stream to the list builder only if stream is not null.
96 private void add(Builder<InputStream> builder, InputStream inputStream) {
97 if (inputStream != null) {
98 builder.add(inputStream);
99 }
100 }
101
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800102}