Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-present Open Networking Foundation |
| 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.gui2; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableList.Builder; |
| 21 | import org.onlab.osgi.ServiceNotFoundException; |
| 22 | import org.onosproject.rest.AbstractInjectionResource; |
| 23 | import org.onosproject.ui.UiExtensionService; |
| 24 | import org.onosproject.ui.UiPreferencesService; |
| 25 | import org.onosproject.ui.UiSessionToken; |
| 26 | import org.onosproject.ui.UiTokenService; |
| 27 | |
| 28 | import javax.ws.rs.GET; |
| 29 | import javax.ws.rs.Path; |
| 30 | import javax.ws.rs.Produces; |
| 31 | import javax.ws.rs.core.Context; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import javax.ws.rs.core.SecurityContext; |
| 35 | import java.io.ByteArrayInputStream; |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.io.SequenceInputStream; |
| 39 | import java.net.URI; |
| 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 for GUI2. |
| 46 | */ |
| 47 | @Path("/") |
| 48 | public class MainIndexResource extends AbstractInjectionResource { |
| 49 | |
| 50 | private static final String INDEX_REDIRECT = "/onos/ui/index.html"; |
| 51 | |
| 52 | private static final String INDEX = "index.html"; |
| 53 | private static final String NOT_READY = "not-ready.html"; |
| 54 | |
| 55 | private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->"; |
| 56 | private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->"; |
| 57 | |
| 58 | private static final byte[] SCRIPT_START = "\n<script>\n".getBytes(); |
| 59 | private static final byte[] SCRIPT_END = "</script>\n\n".getBytes(); |
| 60 | |
| 61 | @Context |
| 62 | private SecurityContext ctx; |
| 63 | |
| 64 | @GET |
| 65 | @Produces(MediaType.TEXT_HTML) |
| 66 | public Response getMainIndexRedirect() throws IOException { |
| 67 | if (ctx == null || ctx.getUserPrincipal() == null) { |
| 68 | return Response.temporaryRedirect(URI.create(INDEX_REDIRECT)).build(); |
| 69 | } |
| 70 | return getMainIndex(); |
| 71 | } |
| 72 | |
| 73 | @GET |
| 74 | @Produces(MediaType.TEXT_HTML) |
| 75 | @Path("/index.html") |
| 76 | public Response getMainIndex() throws IOException { |
| 77 | ClassLoader classLoader = getClass().getClassLoader(); |
| 78 | UiExtensionService service; |
| 79 | UiTokenService tokens; |
| 80 | |
| 81 | try { |
| 82 | service = get(UiExtensionService.class); |
| 83 | tokens = get(UiTokenService.class); |
| 84 | |
| 85 | } catch (ServiceNotFoundException e) { |
| 86 | return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build(); |
| 87 | } |
| 88 | |
| 89 | InputStream indexTemplate = classLoader.getResourceAsStream(INDEX); |
| 90 | String index = new String(toByteArray(indexTemplate)); |
| 91 | |
| 92 | int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length(); |
| 93 | int p0e = split(index, p0s, INJECT_USER_END); |
| 94 | int p3s = split(index, p0e, null); |
| 95 | |
| 96 | |
| 97 | // FIXME: use global opaque auth token to allow secure failover |
| 98 | |
| 99 | // for now, just use the user principal name... |
| 100 | String userName = ctx.getUserPrincipal().getName(); |
| 101 | |
| 102 | // get a session token to use for UI-web-socket authentication |
| 103 | UiSessionToken token = tokens.issueToken(userName); |
| 104 | |
| 105 | String auth = "var onosUser='" + userName + "',\n" + |
| 106 | " onosAuth='" + token + "';\n"; |
| 107 | |
| 108 | StreamEnumeration streams = |
| 109 | new StreamEnumeration(of(stream(index, 0, p0s), |
| 110 | new ByteArrayInputStream(SCRIPT_START), |
| 111 | stream(auth, 0, auth.length()), |
| 112 | userPreferences(userName), |
| 113 | userConsoleLog(userName), |
| 114 | new ByteArrayInputStream(SCRIPT_END), |
| 115 | stream(index, p0e, p3s))); |
| 116 | |
Lakshya Thakur | de3e6f5 | 2020-11-29 23:54:24 +0530 | [diff] [blame] | 117 | return Response.ok(new SequenceInputStream(streams)).build(); |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private InputStream userConsoleLog(String userName) { |
| 121 | String code = "console.log('Logging in as user >" + userName + "<');\n"; |
| 122 | return new ByteArrayInputStream(code.getBytes()); |
| 123 | } |
| 124 | |
| 125 | // Produces an input stream including user preferences. |
| 126 | private InputStream userPreferences(String userName) { |
| 127 | UiPreferencesService service = get(UiPreferencesService.class); |
| 128 | ObjectNode prefs = mapper().createObjectNode(); |
| 129 | service.getPreferences(userName).forEach(prefs::set); |
| 130 | String string = "var userPrefs = " + prefs.toString() + ";\n"; |
| 131 | return new ByteArrayInputStream(string.getBytes()); |
| 132 | } |
| 133 | |
| 134 | private static final String NL = String.format("%n"); |
| 135 | private static final byte[] NL_BYTES = NL.getBytes(); |
| 136 | } |