Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList.Builder; |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 21 | import org.onlab.osgi.ServiceNotFoundException; |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 22 | import org.onosproject.rest.AbstractInjectionResource; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 23 | import org.onosproject.ui.UiExtensionService; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.UiPreferencesService; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.UiSessionToken; |
| 26 | import org.onosproject.ui.UiTokenService; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 27 | |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 31 | import javax.ws.rs.core.Context; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 34 | import javax.ws.rs.core.SecurityContext; |
| 35 | import java.io.ByteArrayInputStream; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.io.SequenceInputStream; |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 39 | import java.net.URI; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | |
| 41 | import static com.google.common.collect.ImmutableList.of; |
| 42 | import static com.google.common.io.ByteStreams.toByteArray; |
| 43 | |
| 44 | /** |
| 45 | * Resource for serving the dynamically composed index.html. |
| 46 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 47 | @Path("/") |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 48 | public class MainIndexResource extends AbstractInjectionResource { |
| 49 | |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 50 | private static final String INDEX_REDIRECT = "/onos/ui/index.html"; |
| 51 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 52 | private static final String INDEX = "index.html"; |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 53 | private static final String NOT_READY = "not-ready.html"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 54 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 55 | private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->"; |
| 56 | private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->"; |
| 57 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 58 | private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->"; |
| 59 | private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 60 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 61 | private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->"; |
| 62 | private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->"; |
| 63 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 64 | private static final byte[] SCRIPT_START = "\n<script>\n".getBytes(); |
Simon Hunt | 23f21e3 | 2016-05-04 14:49:03 -0700 | [diff] [blame] | 65 | private static final byte[] SCRIPT_END = "</script>\n\n".getBytes(); |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 66 | |
| 67 | @Context |
| 68 | private SecurityContext ctx; |
| 69 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 70 | @GET |
| 71 | @Produces(MediaType.TEXT_HTML) |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 72 | 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 82 | public Response getMainIndex() throws IOException { |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 83 | ClassLoader classLoader = getClass().getClassLoader(); |
| 84 | UiExtensionService service; |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 85 | UiTokenService tokens; |
| 86 | |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 87 | try { |
| 88 | service = get(UiExtensionService.class); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 89 | tokens = get(UiTokenService.class); |
| 90 | |
Thomas Vachuska | a02fc81 | 2015-07-21 18:37:58 -0700 | [diff] [blame] | 91 | } catch (ServiceNotFoundException e) { |
| 92 | return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build(); |
| 93 | } |
| 94 | |
| 95 | InputStream indexTemplate = classLoader.getResourceAsStream(INDEX); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 96 | String index = new String(toByteArray(indexTemplate)); |
| 97 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 98 | 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 Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 101 | int p1e = split(index, p1s, INJECT_JS_END); |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 102 | int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length(); |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 103 | int p2e = split(index, p2s, INJECT_CSS_END); |
| 104 | int p3s = split(index, p2e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 105 | |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 106 | |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 107 | // FIXME: use global opaque auth token to allow secure failover |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 108 | |
| 109 | // for now, just use the user principal name... |
Thomas Vachuska | c0f757a | 2018-10-23 11:41:34 -0700 | [diff] [blame] | 110 | String userName = ctx.getUserPrincipal().getName(); |
Simon Hunt | 1169c95 | 2017-06-05 11:20:11 -0700 | [diff] [blame] | 111 | |
| 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 Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 117 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 118 | StreamEnumeration streams = |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 119 | new StreamEnumeration(of(stream(index, 0, p0s), |
Simon Hunt | 23f21e3 | 2016-05-04 14:49:03 -0700 | [diff] [blame] | 120 | 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 130 | |
| 131 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 132 | } |
| 133 | |
Simon Hunt | 23f21e3 | 2016-05-04 14:49:03 -0700 | [diff] [blame] | 134 | 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 Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 139 | // 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 Hunt | 23f21e3 | 2016-05-04 14:49:03 -0700 | [diff] [blame] | 144 | String string = "var userPrefs = " + prefs.toString() + ";\n"; |
Thomas Vachuska | 0af2691 | 2016-03-21 21:37:30 -0700 | [diff] [blame] | 145 | return new ByteArrayInputStream(string.getBytes()); |
| 146 | } |
| 147 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 148 | // Produces an input stream including JS injections from all extensions. |
| 149 | private InputStream includeJs(UiExtensionService service) { |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 150 | Builder<InputStream> builder = ImmutableList.builder(); |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 151 | 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 165 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 166 | } |
| 167 | |
Thomas Vachuska | 3ccb9eb | 2015-04-29 23:33:56 -0700 | [diff] [blame] | 168 | // 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 Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 175 | 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 190 | } |