blob: dd345b4fb85862123ec973678a07d353abf9af5f [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
Thomas Vachuska0af26912016-03-21 21:37:30 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080019import com.google.common.collect.ImmutableList;
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -070020import com.google.common.collect.ImmutableList.Builder;
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070021import org.onlab.osgi.ServiceNotFoundException;
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070022import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080023import org.onosproject.ui.UiExtensionService;
Thomas Vachuska0af26912016-03-21 21:37:30 -070024import org.onosproject.ui.UiPreferencesService;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080025
26import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.Produces;
Thomas Vachuska0af26912016-03-21 21:37:30 -070029import javax.ws.rs.core.Context;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080030import javax.ws.rs.core.MediaType;
31import javax.ws.rs.core.Response;
Thomas Vachuska0af26912016-03-21 21:37:30 -070032import javax.ws.rs.core.SecurityContext;
33import java.io.ByteArrayInputStream;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080034import java.io.IOException;
35import java.io.InputStream;
36import java.io.SequenceInputStream;
37
38import static com.google.common.collect.ImmutableList.of;
39import static com.google.common.io.ByteStreams.toByteArray;
40
41/**
42 * Resource for serving the dynamically composed index.html.
43 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080044@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080045public class MainIndexResource extends AbstractInjectionResource {
46
Thomas Vachuskae95da772015-02-23 15:50:11 -080047 private static final String INDEX = "index.html";
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070048 private static final String NOT_READY = "not-ready.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080049
Thomas Vachuska0af26912016-03-21 21:37:30 -070050 private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->";
51 private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->";
52
Thomas Vachuskaa0509892015-02-21 22:18:41 -080053 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
54 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080055
Thomas Vachuskaa0509892015-02-21 22:18:41 -080056 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
57 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
58
Thomas Vachuska0af26912016-03-21 21:37:30 -070059 private static final byte[] SCRIPT_START = "\n<script>\n".getBytes();
60 private static final byte[] SCRIPT_END = "\n</script>\n\n".getBytes();
61
62 @Context
63 private SecurityContext ctx;
64
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080065 @GET
66 @Produces(MediaType.TEXT_HTML)
67 public Response getMainIndex() throws IOException {
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070068 ClassLoader classLoader = getClass().getClassLoader();
69 UiExtensionService service;
70 try {
71 service = get(UiExtensionService.class);
72 } catch (ServiceNotFoundException e) {
73 return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
74 }
75
76 InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080077 String index = new String(toByteArray(indexTemplate));
78
Thomas Vachuska0af26912016-03-21 21:37:30 -070079 int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length();
80 int p0e = split(index, p0s, INJECT_USER_END);
81 int p1s = split(index, p0e, INJECT_JS_START) - INJECT_JS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080082 int p1e = split(index, p1s, INJECT_JS_END);
Simon Hunt40927332016-01-22 15:29:47 -080083 int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080084 int p2e = split(index, p2s, INJECT_CSS_END);
85 int p3s = split(index, p2e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080086
Thomas Vachuska0af26912016-03-21 21:37:30 -070087 // FIXME: use global opaque auth token to allow secure failover
88 String userName = ctx.getUserPrincipal().getName();
89 String auth = "var onosAuth='" + userName + "';\n";
90
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080091 StreamEnumeration streams =
Thomas Vachuska0af26912016-03-21 21:37:30 -070092 new StreamEnumeration(of(stream(index, 0, p0s),
93 new ByteArrayInputStream(SCRIPT_START),
94 stream(auth, 0, auth.length()),
95 userPreferences(userName),
96 new ByteArrayInputStream(SCRIPT_END),
97 stream(index, p0e, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080098 includeJs(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080099 stream(index, p1e, p2s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800100 includeCss(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -0800101 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800102
103 return Response.ok(new SequenceInputStream(streams)).build();
104 }
105
Thomas Vachuska0af26912016-03-21 21:37:30 -0700106 // Produces an input stream including user preferences.
107 private InputStream userPreferences(String userName) {
108 UiPreferencesService service = get(UiPreferencesService.class);
109 ObjectNode prefs = mapper().createObjectNode();
110 service.getPreferences(userName).forEach(prefs::set);
111 String string = "var userPrefs = " + prefs.toString() + ";";
112 return new ByteArrayInputStream(string.getBytes());
113 }
114
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800115 // Produces an input stream including JS injections from all extensions.
116 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700117 Builder<InputStream> builder = ImmutableList.builder();
Simon Hunt40927332016-01-22 15:29:47 -0800118 service.getExtensions().forEach(ext -> {
119 add(builder, ext.js());
120 add(builder, new NewlineInputStream());
121 });
122 return new SequenceInputStream(new StreamEnumeration(builder.build()));
123 }
124
125 // Produces an input stream including CSS injections from all extensions.
126 private InputStream includeCss(UiExtensionService service) {
127 Builder<InputStream> builder = ImmutableList.builder();
128 service.getExtensions().forEach(ext -> {
129 add(builder, ext.css());
130 add(builder, new NewlineInputStream());
131 });
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800132 return new SequenceInputStream(new StreamEnumeration(builder.build()));
133 }
134
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700135 // Safely adds the stream to the list builder only if stream is not null.
136 private void add(Builder<InputStream> builder, InputStream inputStream) {
137 if (inputStream != null) {
138 builder.add(inputStream);
139 }
140 }
141
Simon Hunt40927332016-01-22 15:29:47 -0800142 private static final String NL = String.format("%n");
143 private static final byte[] NL_BYTES = NL.getBytes();
144
145 private static class NewlineInputStream extends InputStream {
146 private int index = 0;
147
148 @Override
149 public int read() throws IOException {
150 if (index == NL_BYTES.length) {
151 return -1;
152 }
153 return NL_BYTES[index++];
154 }
155 }
156
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800157}