blob: b18c7280b9572e04a044c46434866dfed7c11f9f [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;
34
35/**
Thomas Vachuskae95da772015-02-23 15:50:11 -080036 * Resource for serving the dynamically composed nav.html.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080037 */
Thomas Vachuskae95da772015-02-23 15:50:11 -080038@Path("/nav/nav.html")
39public class MainNavResource extends AbstractInjectionResource {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040
Thomas Vachuskae95da772015-02-23 15:50:11 -080041 private static final String NAV_HTML = "nav.html";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080042
43 private static final String INJECT_VIEW_ITEMS_START = "<!-- {INJECTED-VIEW-NAV-START} -->";
44 private static final String INJECT_VIEW_ITEMS_END = "<!-- {INJECTED-VIEW-NAV-END} -->";
45
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080046
47 private static final String NAV_FORMAT =
48 " <li> <a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a></li>";
49
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()) {
74 sb.append(String.format(NAV_FORMAT, view.id(), view.label()));
75 }
76 }
77 return new ByteArrayInputStream(sb.toString().getBytes());
78 }
79}