blob: 1bf3d426093fcd8b4d23db8360fdb6c5e5f683f3 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.ui.impl;
17
18import org.onosproject.ui.UiExtension;
19import org.onosproject.ui.UiExtensionService;
20import org.onosproject.ui.UiView;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
24import javax.ws.rs.Produces;
25import javax.ws.rs.core.MediaType;
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;
Thomas Vachuskaa0509892015-02-21 22:18:41 -080034import static org.onosproject.ui.impl.MainViewResource.SCRIPT;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080035
36/**
37 * Resource for serving the dynamically composed onos.js.
38 */
39@Path("/")
40public class MainExtResource extends AbstractInjectionResource {
41
Thomas Vachuskaa0509892015-02-21 22:18:41 -080042 private static final String MAIN_JS = "templates/onos-template.js";
43 private static final String NAV_HTML = "templates/nav-template.html";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080044
Thomas Vachuskaa0509892015-02-21 22:18:41 -080045 private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-IDS-START}";
46 private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-IDS-END}";
47
48 private static final String INJECT_VIEW_ITEMS_START = "<!-- {INJECTED-VIEW-NAV-START} -->";
49 private static final String INJECT_VIEW_ITEMS_END = "<!-- {INJECTED-VIEW-NAV-END} -->";
50
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080051
52 private static final String NAV_FORMAT =
53 " <li> <a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a></li>";
54
55 @Path("/onos.js")
56 @GET
Thomas Vachuskaa0509892015-02-21 22:18:41 -080057 @Produces(SCRIPT)
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080058 public Response getMainModule() throws IOException {
59 UiExtensionService service = get(UiExtensionService.class);
60 InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS);
61 String js = new String(toByteArray(jsTemplate));
62
Thomas Vachuskaa0509892015-02-21 22:18:41 -080063 int p1s = split(js, 0, INJECT_VIEW_IDS_START);
64 int p1e = split(js, 0, INJECT_VIEW_IDS_END);
65 int p2s = split(js, p1e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080066
67 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080068 new StreamEnumeration(of(stream(js, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080069 includeViewIds(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080070 stream(js, p1e, p2s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080071
72 return Response.ok(new SequenceInputStream(streams)).build();
73 }
74
75 // Produces an input stream including view id injections from all extensions.
76 private InputStream includeViewIds(UiExtensionService service) {
77 StringBuilder sb = new StringBuilder("\n");
78 for (UiExtension extension : service.getExtensions()) {
79 for (UiView view : extension.views()) {
80 sb.append(" '").append(view.id()).append("',");
81 }
82 }
83 return new ByteArrayInputStream(sb.toString().getBytes());
84 }
85
86 @Path("/nav/nav.html")
87 @GET
88 @Produces(MediaType.TEXT_HTML)
89 public Response getNavigation() throws IOException {
90 UiExtensionService service = get(UiExtensionService.class);
91 InputStream navTemplate = getClass().getClassLoader().getResourceAsStream(NAV_HTML);
92 String js = new String(toByteArray(navTemplate));
93
Thomas Vachuskaa0509892015-02-21 22:18:41 -080094 int p1s = split(js, 0, INJECT_VIEW_ITEMS_START);
95 int p1e = split(js, 0, INJECT_VIEW_ITEMS_END);
96 int p2s = split(js, p1e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080097
98 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080099 new StreamEnumeration(of(stream(js, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800100 includeNavItems(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -0800101 stream(js, p1e, p2s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800102
103 return Response.ok(new SequenceInputStream(streams)).build();
104 }
105
106 // Produces an input stream including nav item injections from all extensions.
107 private InputStream includeNavItems(UiExtensionService service) {
108 StringBuilder sb = new StringBuilder("\n");
109 for (UiExtension extension : service.getExtensions()) {
110 for (UiView view : extension.views()) {
111 sb.append(String.format(NAV_FORMAT, view.id(), view.label()));
112 }
113 }
114 return new ByteArrayInputStream(sb.toString().getBytes());
115 }
116}