blob: 854c01048ea2115c871b8d1fbd7c1340b1b318db [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08003 *
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;
Simon Hunt1169c952017-06-05 11:20:11 -070025import org.onosproject.ui.UiSessionToken;
26import org.onosproject.ui.UiTokenService;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080027
28import javax.ws.rs.GET;
29import javax.ws.rs.Path;
30import javax.ws.rs.Produces;
Thomas Vachuska0af26912016-03-21 21:37:30 -070031import javax.ws.rs.core.Context;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Thomas Vachuska0af26912016-03-21 21:37:30 -070034import javax.ws.rs.core.SecurityContext;
35import java.io.ByteArrayInputStream;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080036import java.io.IOException;
37import java.io.InputStream;
38import java.io.SequenceInputStream;
39
40import static com.google.common.collect.ImmutableList.of;
41import static com.google.common.io.ByteStreams.toByteArray;
42
43/**
44 * Resource for serving the dynamically composed index.html.
45 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080046@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047public class MainIndexResource extends AbstractInjectionResource {
48
Thomas Vachuskae95da772015-02-23 15:50:11 -080049 private static final String INDEX = "index.html";
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070050 private static final String NOT_READY = "not-ready.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080051
Thomas Vachuska0af26912016-03-21 21:37:30 -070052 private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->";
53 private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->";
54
Thomas Vachuskaa0509892015-02-21 22:18:41 -080055 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
56 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080057
Thomas Vachuskaa0509892015-02-21 22:18:41 -080058 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
59 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
60
Thomas Vachuska0af26912016-03-21 21:37:30 -070061 private static final byte[] SCRIPT_START = "\n<script>\n".getBytes();
Simon Hunt23f21e32016-05-04 14:49:03 -070062 private static final byte[] SCRIPT_END = "</script>\n\n".getBytes();
Thomas Vachuska0af26912016-03-21 21:37:30 -070063
64 @Context
65 private SecurityContext ctx;
66
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080067 @GET
68 @Produces(MediaType.TEXT_HTML)
69 public Response getMainIndex() throws IOException {
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070070 ClassLoader classLoader = getClass().getClassLoader();
71 UiExtensionService service;
Simon Hunt1169c952017-06-05 11:20:11 -070072 UiTokenService tokens;
73
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070074 try {
75 service = get(UiExtensionService.class);
Simon Hunt1169c952017-06-05 11:20:11 -070076 tokens = get(UiTokenService.class);
77
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070078 } catch (ServiceNotFoundException e) {
79 return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
80 }
81
82 InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080083 String index = new String(toByteArray(indexTemplate));
84
Thomas Vachuska0af26912016-03-21 21:37:30 -070085 int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length();
86 int p0e = split(index, p0s, INJECT_USER_END);
87 int p1s = split(index, p0e, INJECT_JS_START) - INJECT_JS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080088 int p1e = split(index, p1s, INJECT_JS_END);
Simon Hunt40927332016-01-22 15:29:47 -080089 int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -080090 int p2e = split(index, p2s, INJECT_CSS_END);
91 int p3s = split(index, p2e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080092
Simon Hunt1169c952017-06-05 11:20:11 -070093
Thomas Vachuska0af26912016-03-21 21:37:30 -070094 // FIXME: use global opaque auth token to allow secure failover
Simon Hunt1169c952017-06-05 11:20:11 -070095
96 // for now, just use the user principal name...
Thomas Vachuska0af26912016-03-21 21:37:30 -070097 String userName = ctx.getUserPrincipal().getName();
Simon Hunt1169c952017-06-05 11:20:11 -070098
99 // get a session token to use for UI-web-socket authentication
100 UiSessionToken token = tokens.issueToken(userName);
101
102 String auth = "var onosUser='" + userName + "',\n" +
103 " onosAuth='" + token + "';\n";
Thomas Vachuska0af26912016-03-21 21:37:30 -0700104
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800105 StreamEnumeration streams =
Thomas Vachuska0af26912016-03-21 21:37:30 -0700106 new StreamEnumeration(of(stream(index, 0, p0s),
Simon Hunt23f21e32016-05-04 14:49:03 -0700107 new ByteArrayInputStream(SCRIPT_START),
108 stream(auth, 0, auth.length()),
109 userPreferences(userName),
110 userConsoleLog(userName),
111 new ByteArrayInputStream(SCRIPT_END),
112 stream(index, p0e, p1s),
113 includeJs(service),
114 stream(index, p1e, p2s),
115 includeCss(service),
116 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800117
118 return Response.ok(new SequenceInputStream(streams)).build();
119 }
120
Simon Hunt23f21e32016-05-04 14:49:03 -0700121 private InputStream userConsoleLog(String userName) {
122 String code = "console.log('Logging in as user >" + userName + "<');\n";
123 return new ByteArrayInputStream(code.getBytes());
124 }
125
Thomas Vachuska0af26912016-03-21 21:37:30 -0700126 // Produces an input stream including user preferences.
127 private InputStream userPreferences(String userName) {
128 UiPreferencesService service = get(UiPreferencesService.class);
129 ObjectNode prefs = mapper().createObjectNode();
130 service.getPreferences(userName).forEach(prefs::set);
Simon Hunt23f21e32016-05-04 14:49:03 -0700131 String string = "var userPrefs = " + prefs.toString() + ";\n";
Thomas Vachuska0af26912016-03-21 21:37:30 -0700132 return new ByteArrayInputStream(string.getBytes());
133 }
134
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800135 // Produces an input stream including JS injections from all extensions.
136 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700137 Builder<InputStream> builder = ImmutableList.builder();
Simon Hunt40927332016-01-22 15:29:47 -0800138 service.getExtensions().forEach(ext -> {
139 add(builder, ext.js());
140 add(builder, new NewlineInputStream());
141 });
142 return new SequenceInputStream(new StreamEnumeration(builder.build()));
143 }
144
145 // Produces an input stream including CSS injections from all extensions.
146 private InputStream includeCss(UiExtensionService service) {
147 Builder<InputStream> builder = ImmutableList.builder();
148 service.getExtensions().forEach(ext -> {
149 add(builder, ext.css());
150 add(builder, new NewlineInputStream());
151 });
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800152 return new SequenceInputStream(new StreamEnumeration(builder.build()));
153 }
154
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700155 // Safely adds the stream to the list builder only if stream is not null.
156 private void add(Builder<InputStream> builder, InputStream inputStream) {
157 if (inputStream != null) {
158 builder.add(inputStream);
159 }
160 }
161
Simon Hunt40927332016-01-22 15:29:47 -0800162 private static final String NL = String.format("%n");
163 private static final byte[] NL_BYTES = NL.getBytes();
164
165 private static class NewlineInputStream extends InputStream {
166 private int index = 0;
167
168 @Override
169 public int read() throws IOException {
170 if (index == NL_BYTES.length) {
171 return -1;
172 }
173 return NL_BYTES[index++];
174 }
175 }
176
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800177}