blob: 2797f3352d32eef9f1b18b0a78ed1d8aeca98796 [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 Vachuskafe8c98a2015-02-04 01:24:32 -080021import org.onosproject.ui.UiExtensionService;
22
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.Produces;
26import javax.ws.rs.core.MediaType;
27import javax.ws.rs.core.Response;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.SequenceInputStream;
31
32import static com.google.common.collect.ImmutableList.of;
33import static com.google.common.io.ByteStreams.toByteArray;
34
35/**
36 * Resource for serving the dynamically composed index.html.
37 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080038@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080039public class MainIndexResource extends AbstractInjectionResource {
40
Thomas Vachuskae95da772015-02-23 15:50:11 -080041 private static final String INDEX = "index.html";
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070042 private static final String NOT_READY = "not-ready.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080043
Thomas Vachuskaa0509892015-02-21 22:18:41 -080044 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
45 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080046
Thomas Vachuskaa0509892015-02-21 22:18:41 -080047 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
48 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
49
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080050 @GET
51 @Produces(MediaType.TEXT_HTML)
52 public Response getMainIndex() throws IOException {
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070053 ClassLoader classLoader = getClass().getClassLoader();
54 UiExtensionService service;
55 try {
56 service = get(UiExtensionService.class);
57 } catch (ServiceNotFoundException e) {
58 return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
59 }
60
61 InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080062 String index = new String(toByteArray(indexTemplate));
63
Thomas Vachuskaa0509892015-02-21 22:18:41 -080064 int p1s = split(index, 0, INJECT_JS_START);
65 int p1e = split(index, p1s, INJECT_JS_END);
66 int p2s = split(index, p1e, INJECT_CSS_START);
67 int p2e = split(index, p2s, INJECT_CSS_END);
68 int p3s = split(index, p2e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069
70 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080071 new StreamEnumeration(of(stream(index, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080072 includeJs(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080073 stream(index, p1e, p2s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080074 includeCss(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080075 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080076
77 return Response.ok(new SequenceInputStream(streams)).build();
78 }
79
80 // Produces an input stream including CSS injections from all extensions.
81 private InputStream includeCss(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070082 Builder<InputStream> builder = ImmutableList.builder();
83 service.getExtensions().forEach(ext -> add(builder, ext.css()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080084 return new SequenceInputStream(new StreamEnumeration(builder.build()));
85 }
86
87 // Produces an input stream including JS injections from all extensions.
88 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070089 Builder<InputStream> builder = ImmutableList.builder();
90 service.getExtensions().forEach(ext -> add(builder, ext.js()));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080091 return new SequenceInputStream(new StreamEnumeration(builder.build()));
92 }
93
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070094 // Safely adds the stream to the list builder only if stream is not null.
95 private void add(Builder<InputStream> builder, InputStream inputStream) {
96 if (inputStream != null) {
97 builder.add(inputStream);
98 }
99 }
100
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800101}