blob: 853dd94a4b6435f7fd130de9701ebcce904d0c61 [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;
Simon Hunt1002cd82015-04-23 14:44:03 -070021import org.onosproject.ui.UiViewHidden;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080022
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.Produces;
26import javax.ws.rs.core.MediaType;
27import javax.ws.rs.core.Response;
28import java.io.ByteArrayInputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.SequenceInputStream;
32
33import static com.google.common.collect.ImmutableList.of;
34import static com.google.common.io.ByteStreams.toByteArray;
35
36/**
Thomas Vachuskae95da772015-02-23 15:50:11 -080037 * Resource for serving the dynamically composed nav.html.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080038 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080039@Path("/")
Thomas Vachuskae95da772015-02-23 15:50:11 -080040public class MainNavResource extends AbstractInjectionResource {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080041
Thomas Vachuskae95da772015-02-23 15:50:11 -080042 private static final String NAV_HTML = "nav.html";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080043
44 private static final String INJECT_VIEW_ITEMS_START = "<!-- {INJECTED-VIEW-NAV-START} -->";
45 private static final String INJECT_VIEW_ITEMS_END = "<!-- {INJECTED-VIEW-NAV-END} -->";
46
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080047 private static final String NAV_FORMAT =
Thomas Vachuska9730ec92015-03-07 17:25:21 -080048 "<a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a>\n";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080049
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080050 @GET
51 @Produces(MediaType.TEXT_HTML)
52 public Response getNavigation() throws IOException {
53 UiExtensionService service = get(UiExtensionService.class);
54 InputStream navTemplate = getClass().getClassLoader().getResourceAsStream(NAV_HTML);
55 String js = new String(toByteArray(navTemplate));
56
Thomas Vachuskaa0509892015-02-21 22:18:41 -080057 int p1s = split(js, 0, INJECT_VIEW_ITEMS_START);
58 int p1e = split(js, 0, INJECT_VIEW_ITEMS_END);
59 int p2s = split(js, p1e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080060
61 StreamEnumeration streams =
Thomas Vachuskaa0509892015-02-21 22:18:41 -080062 new StreamEnumeration(of(stream(js, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063 includeNavItems(service),
Thomas Vachuskaa0509892015-02-21 22:18:41 -080064 stream(js, p1e, p2s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080065
66 return Response.ok(new SequenceInputStream(streams)).build();
67 }
68
69 // Produces an input stream including nav item injections from all extensions.
70 private InputStream includeNavItems(UiExtensionService service) {
71 StringBuilder sb = new StringBuilder("\n");
72 for (UiExtension extension : service.getExtensions()) {
73 for (UiView view : extension.views()) {
Simon Hunt1002cd82015-04-23 14:44:03 -070074 if (!(view instanceof UiViewHidden)) {
75 sb.append(String.format(NAV_FORMAT, view.id(), view.label()));
76 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080077 }
78 }
79 return new ByteArrayInputStream(sb.toString().getBytes());
80 }
81}