Sean Condon | ffd4bf8 | 2018-05-22 23:33:09 +0100 | [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; |
| 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 | |
| 40 | import static com.google.common.collect.ImmutableList.of; |
| 41 | import static com.google.common.io.ByteStreams.toByteArray; |
| 42 | |
| 43 | /** |
| 44 | * Resource for serving the dynamically composed index.html. |
| 45 | */ |
| 46 | @Path("/") |
| 47 | public class MainIndexResource extends AbstractInjectionResource { |
| 48 | |
| 49 | private static final String INDEX = "dist/index.html"; |
| 50 | private static final String NOT_READY = "not-ready.html"; |
| 51 | |
| 52 | private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->"; |
| 53 | private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->"; |
| 54 | |
| 55 | private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->"; |
| 56 | private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->"; |
| 57 | |
| 58 | private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->"; |
| 59 | private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->"; |
| 60 | |
| 61 | private static final byte[] SCRIPT_START = "\n<script>\n".getBytes(); |
| 62 | private static final byte[] SCRIPT_END = "</script>\n\n".getBytes(); |
| 63 | |
| 64 | @Context |
| 65 | private SecurityContext ctx; |
| 66 | |
| 67 | @GET |
| 68 | @Produces(MediaType.TEXT_HTML) |
| 69 | public Response getMainIndex() throws IOException { |
| 70 | ClassLoader classLoader = getClass().getClassLoader(); |
| 71 | UiExtensionService service; |
| 72 | UiTokenService tokens; |
| 73 | |
| 74 | try { |
| 75 | service = get(UiExtensionService.class); |
| 76 | tokens = get(UiTokenService.class); |
| 77 | |
| 78 | } catch (ServiceNotFoundException e) { |
| 79 | return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build(); |
| 80 | } |
| 81 | |
| 82 | InputStream indexTemplate = classLoader.getResourceAsStream(INDEX); |
| 83 | String index = new String(toByteArray(indexTemplate)); |
| 84 | |
| 85 | 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(); |
| 88 | int p1e = split(index, p1s, INJECT_JS_END); |
| 89 | int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length(); |
| 90 | int p2e = split(index, p2s, INJECT_CSS_END); |
| 91 | int p3s = split(index, p2e, null); |
| 92 | |
| 93 | |
| 94 | // FIXME: use global opaque auth token to allow secure failover |
| 95 | |
| 96 | // for now, just use the user principal name... |
| 97 | String userName = ctx.getUserPrincipal().getName(); |
| 98 | |
| 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"; |
| 104 | |
| 105 | StreamEnumeration streams = |
| 106 | new StreamEnumeration(of(stream(index, 0, p0s), |
| 107 | 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))); |
| 117 | |
| 118 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 119 | } |
| 120 | |
| 121 | private InputStream userConsoleLog(String userName) { |
| 122 | String code = "console.log('Logging in as user >" + userName + "<');\n"; |
| 123 | return new ByteArrayInputStream(code.getBytes()); |
| 124 | } |
| 125 | |
| 126 | // 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); |
| 131 | String string = "var userPrefs = " + prefs.toString() + ";\n"; |
| 132 | return new ByteArrayInputStream(string.getBytes()); |
| 133 | } |
| 134 | |
| 135 | // Produces an input stream including JS injections from all extensions. |
| 136 | private InputStream includeJs(UiExtensionService service) { |
| 137 | Builder<InputStream> builder = ImmutableList.builder(); |
| 138 | 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 | }); |
| 152 | return new SequenceInputStream(new StreamEnumeration(builder.build())); |
| 153 | } |
| 154 | |
| 155 | // 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 | |
| 162 | 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 | |
| 177 | } |