blob: 242f7ba02f6bfd63b65392214a7577d1b2fe7a28 [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
chengfan386620e2016-11-09 17:02:40 +080044 private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-DATA-START}";
45 private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-DATA-END}";
Simon Hunt40927332016-01-22 15:29:47 -080046 private static final String PREFIX = " '";
chengfan386620e2016-11-09 17:02:40 +080047 private static final String MIDFIX = "' : '";
Simon Hunt40927332016-01-22 15:29:47 -080048 private static final String SUFFIX = String.format("',%n");
Thomas Vachuskae95da772015-02-23 15:50:11 -080049
50 @GET
51 @Produces(SCRIPT)
52 public Response getMainModule() throws IOException {
53 UiExtensionService service = get(UiExtensionService.class);
54 InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS);
55 String js = new String(toByteArray(jsTemplate));
56
Simon Hunt40927332016-01-22 15:29:47 -080057 int p1s = split(js, 0, INJECT_VIEW_IDS_START) - INJECT_VIEW_IDS_START.length();
Thomas Vachuskae95da772015-02-23 15:50:11 -080058 int p1e = split(js, 0, INJECT_VIEW_IDS_END);
59 int p2s = split(js, p1e, null);
60
61 StreamEnumeration streams =
62 new StreamEnumeration(of(stream(js, 0, p1s),
63 includeViewIds(service),
64 stream(js, p1e, p2s)));
65
66 return Response.ok(new SequenceInputStream(streams)).build();
67 }
68
69 // Produces an input stream including view id injections from all extensions.
70 private InputStream includeViewIds(UiExtensionService service) {
71 StringBuilder sb = new StringBuilder("\n");
72 for (UiExtension extension : service.getExtensions()) {
73 for (UiView view : extension.views()) {
chengfan386620e2016-11-09 17:02:40 +080074 sb.append(PREFIX)
75 .append(view.id())
76 .append(MIDFIX)
77 .append(sanitizeUrl(view.helpPageUrl()))
78 .append(SUFFIX);
Thomas Vachuskae95da772015-02-23 15:50:11 -080079 }
80 }
81 return new ByteArrayInputStream(sb.toString().getBytes());
82 }
83
chengfan386620e2016-11-09 17:02:40 +080084 private String sanitizeUrl(String url) {
85 // TODO: add logic for validating URL
86 return url;
87 }
88
Thomas Vachuskae95da772015-02-23 15:50:11 -080089}