blob: 218e2596ebfc12a16855886890e2392c345dc971 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070039import java.net.URI;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040
41import static com.google.common.collect.ImmutableList.of;
42import static com.google.common.io.ByteStreams.toByteArray;
43
44/**
45 * Resource for serving the dynamically composed index.html.
46 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080047@Path("/")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080048public class MainIndexResource extends AbstractInjectionResource {
49
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070050 private static final String INDEX_REDIRECT = "/onos/ui/index.html";
51
Thomas Vachuskae95da772015-02-23 15:50:11 -080052 private static final String INDEX = "index.html";
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070053 private static final String NOT_READY = "not-ready.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080054
Thomas Vachuska0af26912016-03-21 21:37:30 -070055 private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->";
56 private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->";
57
Thomas Vachuskaa0509892015-02-21 22:18:41 -080058 private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
59 private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080060
Thomas Vachuskaa0509892015-02-21 22:18:41 -080061 private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
62 private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
63
Thomas Vachuska0af26912016-03-21 21:37:30 -070064 private static final byte[] SCRIPT_START = "\n<script>\n".getBytes();
Simon Hunt23f21e32016-05-04 14:49:03 -070065 private static final byte[] SCRIPT_END = "</script>\n\n".getBytes();
Thomas Vachuska0af26912016-03-21 21:37:30 -070066
67 @Context
68 private SecurityContext ctx;
69
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080070 @GET
71 @Produces(MediaType.TEXT_HTML)
Thomas Vachuskac0f757a2018-10-23 11:41:34 -070072 public Response getMainIndexRedirect() throws IOException {
73 if (ctx == null || ctx.getUserPrincipal() == null) {
74 return Response.temporaryRedirect(URI.create(INDEX_REDIRECT)).build();
75 }
76 return getMainIndex();
77 }
78
79 @GET
80 @Produces(MediaType.TEXT_HTML)
81 @Path("/index.html")
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080082 public Response getMainIndex() throws IOException {
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070083 ClassLoader classLoader = getClass().getClassLoader();
84 UiExtensionService service;
Simon Hunt1169c952017-06-05 11:20:11 -070085 UiTokenService tokens;
86
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070087 try {
88 service = get(UiExtensionService.class);
Simon Hunt1169c952017-06-05 11:20:11 -070089 tokens = get(UiTokenService.class);
90
Thomas Vachuskaa02fc812015-07-21 18:37:58 -070091 } catch (ServiceNotFoundException e) {
92 return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
93 }
94
95 InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080096 String index = new String(toByteArray(indexTemplate));
97
Thomas Vachuska0af26912016-03-21 21:37:30 -070098 int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length();
99 int p0e = split(index, p0s, INJECT_USER_END);
100 int p1s = split(index, p0e, INJECT_JS_START) - INJECT_JS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -0800101 int p1e = split(index, p1s, INJECT_JS_END);
Simon Hunt40927332016-01-22 15:29:47 -0800102 int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
Thomas Vachuskaa0509892015-02-21 22:18:41 -0800103 int p2e = split(index, p2s, INJECT_CSS_END);
104 int p3s = split(index, p2e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800105
Simon Hunt1169c952017-06-05 11:20:11 -0700106
Thomas Vachuska0af26912016-03-21 21:37:30 -0700107 // FIXME: use global opaque auth token to allow secure failover
Simon Hunt1169c952017-06-05 11:20:11 -0700108
109 // for now, just use the user principal name...
Thomas Vachuskac0f757a2018-10-23 11:41:34 -0700110 String userName = ctx.getUserPrincipal().getName();
Simon Hunt1169c952017-06-05 11:20:11 -0700111
112 // get a session token to use for UI-web-socket authentication
113 UiSessionToken token = tokens.issueToken(userName);
114
115 String auth = "var onosUser='" + userName + "',\n" +
116 " onosAuth='" + token + "';\n";
Thomas Vachuska0af26912016-03-21 21:37:30 -0700117
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800118 StreamEnumeration streams =
Thomas Vachuska0af26912016-03-21 21:37:30 -0700119 new StreamEnumeration(of(stream(index, 0, p0s),
Simon Hunt23f21e32016-05-04 14:49:03 -0700120 new ByteArrayInputStream(SCRIPT_START),
121 stream(auth, 0, auth.length()),
122 userPreferences(userName),
123 userConsoleLog(userName),
124 new ByteArrayInputStream(SCRIPT_END),
125 stream(index, p0e, p1s),
126 includeJs(service),
127 stream(index, p1e, p2s),
128 includeCss(service),
129 stream(index, p2e, p3s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800130
131 return Response.ok(new SequenceInputStream(streams)).build();
132 }
133
Simon Hunt23f21e32016-05-04 14:49:03 -0700134 private InputStream userConsoleLog(String userName) {
135 String code = "console.log('Logging in as user >" + userName + "<');\n";
136 return new ByteArrayInputStream(code.getBytes());
137 }
138
Thomas Vachuska0af26912016-03-21 21:37:30 -0700139 // Produces an input stream including user preferences.
140 private InputStream userPreferences(String userName) {
141 UiPreferencesService service = get(UiPreferencesService.class);
142 ObjectNode prefs = mapper().createObjectNode();
143 service.getPreferences(userName).forEach(prefs::set);
Simon Hunt23f21e32016-05-04 14:49:03 -0700144 String string = "var userPrefs = " + prefs.toString() + ";\n";
Thomas Vachuska0af26912016-03-21 21:37:30 -0700145 return new ByteArrayInputStream(string.getBytes());
146 }
147
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800148 // Produces an input stream including JS injections from all extensions.
149 private InputStream includeJs(UiExtensionService service) {
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700150 Builder<InputStream> builder = ImmutableList.builder();
Simon Hunt40927332016-01-22 15:29:47 -0800151 service.getExtensions().forEach(ext -> {
152 add(builder, ext.js());
153 add(builder, new NewlineInputStream());
154 });
155 return new SequenceInputStream(new StreamEnumeration(builder.build()));
156 }
157
158 // Produces an input stream including CSS injections from all extensions.
159 private InputStream includeCss(UiExtensionService service) {
160 Builder<InputStream> builder = ImmutableList.builder();
161 service.getExtensions().forEach(ext -> {
162 add(builder, ext.css());
163 add(builder, new NewlineInputStream());
164 });
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800165 return new SequenceInputStream(new StreamEnumeration(builder.build()));
166 }
167
Thomas Vachuska3ccb9eb2015-04-29 23:33:56 -0700168 // Safely adds the stream to the list builder only if stream is not null.
169 private void add(Builder<InputStream> builder, InputStream inputStream) {
170 if (inputStream != null) {
171 builder.add(inputStream);
172 }
173 }
174
Simon Hunt40927332016-01-22 15:29:47 -0800175 private static final String NL = String.format("%n");
176 private static final byte[] NL_BYTES = NL.getBytes();
177
178 private static class NewlineInputStream extends InputStream {
179 private int index = 0;
180
181 @Override
182 public int read() throws IOException {
183 if (index == NL_BYTES.length) {
184 return -1;
185 }
186 return NL_BYTES[index++];
187 }
188 }
189
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800190}