blob: ac1a8bb6b5fa6bb0a7a03204ccc6c498c3b0189f [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
Simon Hunt40927332016-01-22 15:29:47 -080065 int p1s = split(index, 0, INJECT_JS_START) - INJECT_JS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080066 int p1e = split(index, p1s, INJECT_JS_END);
Simon Hunt40927332016-01-22 15:29:47 -080067 int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080068 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
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080081 // Produces an input stream including JS injections from all extensions.
82 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070083 Builder<InputStream> builder = ImmutableList.builder();
Simon Hunt40927332016-01-22 15:29:47 -080084 service.getExtensions().forEach(ext -> {
85 add(builder, ext.js());
86 add(builder, new NewlineInputStream());
87 });
88 return new SequenceInputStream(new StreamEnumeration(builder.build()));
89 }
90
91 // Produces an input stream including CSS injections from all extensions.
92 private InputStream includeCss(UiExtensionService service) {
93 Builder<InputStream> builder = ImmutableList.builder();
94 service.getExtensions().forEach(ext -> {
95 add(builder, ext.css());
96 add(builder, new NewlineInputStream());
97 });
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080098 return new SequenceInputStream(new StreamEnumeration(builder.build()));
99 }
100
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700101 // Safely adds the stream to the list builder only if stream is not null.
102 private void add(Builder<InputStream> builder, InputStream inputStream) {
103 if (inputStream != null) {
104 builder.add(inputStream);
105 }
106 }
107
Simon Hunt40927332016-01-22 15:29:47 -0800108 private static final String NL = String.format("%n");
109 private static final byte[] NL_BYTES = NL.getBytes();
110
111 private static class NewlineInputStream extends InputStream {
112 private int index = 0;
113
114 @Override
115 public int read() throws IOException {
116 if (index == NL_BYTES.length) {
117 return -1;
118 }
119 return NL_BYTES[index++];
120 }
121 }
122
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800123}