blob: 4f54b39c7c45d9cdf5aa0368599dc7d1ce7222a7 [file] [log] [blame]
Thomas Vachuskae95da772015-02-23 15:50:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskae95da772015-02-23 15:50:11 -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 Vachuska0fa2aa12015-08-18 12:53:04 -070018import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskae95da772015-02-23 15:50:11 -080019import org.onosproject.ui.UiExtension;
20import org.onosproject.ui.UiExtensionService;
21import org.onosproject.ui.UiView;
22
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.Produces;
26import javax.ws.rs.core.Response;
27import java.io.ByteArrayInputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.SequenceInputStream;
31
32import static com.google.common.collect.ImmutableList.of;
33import static com.google.common.io.ByteStreams.toByteArray;
34import static org.onosproject.ui.impl.MainViewResource.SCRIPT;
35
36/**
37 * Resource for serving the dynamically composed onos.js.
38 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080039@Path("/")
Thomas Vachuskae95da772015-02-23 15:50:11 -080040public class MainModuleResource extends AbstractInjectionResource {
41
42 private static final String MAIN_JS = "onos.js";
43
44 private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-IDS-START}";
45 private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-IDS-END}";
Simon Hunt40927332016-01-22 15:29:47 -080046 private static final String PREFIX = " '";
47 private static final String SUFFIX = String.format("',%n");
Thomas Vachuskae95da772015-02-23 15:50:11 -080048
49 @GET
50 @Produces(SCRIPT)
51 public Response getMainModule() throws IOException {
52 UiExtensionService service = get(UiExtensionService.class);
53 InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS);
54 String js = new String(toByteArray(jsTemplate));
55
Simon Hunt40927332016-01-22 15:29:47 -080056 int p1s = split(js, 0, INJECT_VIEW_IDS_START) - INJECT_VIEW_IDS_START.length();
Thomas Vachuskae95da772015-02-23 15:50:11 -080057 int p1e = split(js, 0, INJECT_VIEW_IDS_END);
58 int p2s = split(js, p1e, null);
59
60 StreamEnumeration streams =
61 new StreamEnumeration(of(stream(js, 0, p1s),
62 includeViewIds(service),
63 stream(js, p1e, p2s)));
64
65 return Response.ok(new SequenceInputStream(streams)).build();
66 }
67
68 // Produces an input stream including view id injections from all extensions.
69 private InputStream includeViewIds(UiExtensionService service) {
70 StringBuilder sb = new StringBuilder("\n");
71 for (UiExtension extension : service.getExtensions()) {
72 for (UiView view : extension.views()) {
Simon Hunt40927332016-01-22 15:29:47 -080073 sb.append(PREFIX).append(view.id()).append(SUFFIX);
Thomas Vachuskae95da772015-02-23 15:50:11 -080074 }
75 }
76 return new ByteArrayInputStream(sb.toString().getBytes());
77 }
78
79}