blob: 7c58250bedbf1f727913b2858d3fc86ff19697d8 [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;
Simon Hunt38c2b6a2015-04-24 13:02:05 -070031import java.util.ArrayList;
32import java.util.List;
33import java.util.stream.Collectors;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080034
35import static com.google.common.collect.ImmutableList.of;
36import static com.google.common.io.ByteStreams.toByteArray;
37
38/**
Thomas Vachuskae95da772015-02-23 15:50:11 -080039 * Resource for serving the dynamically composed nav.html.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080041@Path("/")
Thomas Vachuskae95da772015-02-23 15:50:11 -080042public class MainNavResource extends AbstractInjectionResource {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080043
Thomas Vachuskae95da772015-02-23 15:50:11 -080044 private static final String NAV_HTML = "nav.html";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080045
Simon Hunt38c2b6a2015-04-24 13:02:05 -070046 private static final String INJECT_VIEW_ITEMS_START =
47 "<!-- {INJECTED-VIEW-NAV-START} -->";
48 private static final String INJECT_VIEW_ITEMS_END =
49 "<!-- {INJECTED-VIEW-NAV-END} -->";
Thomas Vachuskaa0509892015-02-21 22:18:41 -080050
Simon Hunt38c2b6a2015-04-24 13:02:05 -070051 private static final String HDR_FORMAT =
52 "<div class=\"nav-hdr\">%s</div>\n";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080053 private static final String NAV_FORMAT =
Thomas Vachuska9730ec92015-03-07 17:25:21 -080054 "<a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a>\n";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080055
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080056 @GET
57 @Produces(MediaType.TEXT_HTML)
58 public Response getNavigation() throws IOException {
59 UiExtensionService service = get(UiExtensionService.class);
Simon Hunt38c2b6a2015-04-24 13:02:05 -070060 InputStream navTemplate =
61 getClass().getClassLoader().getResourceAsStream(NAV_HTML);
62 String html = new String(toByteArray(navTemplate));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080063
Simon Hunt38c2b6a2015-04-24 13:02:05 -070064 int p1s = split(html, 0, INJECT_VIEW_ITEMS_START);
65 int p1e = split(html, 0, INJECT_VIEW_ITEMS_END);
66 int p2s = split(html, p1e, null);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080067
68 StreamEnumeration streams =
Simon Hunt38c2b6a2015-04-24 13:02:05 -070069 new StreamEnumeration(of(stream(html, 0, p1s),
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080070 includeNavItems(service),
Simon Hunt38c2b6a2015-04-24 13:02:05 -070071 stream(html, p1e, p2s)));
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080072
73 return Response.ok(new SequenceInputStream(streams)).build();
74 }
75
Simon Hunt38c2b6a2015-04-24 13:02:05 -070076 // Produces an input stream of nav item injections from all extensions.
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080077 private InputStream includeNavItems(UiExtensionService service) {
Simon Hunt38c2b6a2015-04-24 13:02:05 -070078 List<UiExtension> extensions = service.getExtensions();
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080079 StringBuilder sb = new StringBuilder("\n");
Simon Hunt38c2b6a2015-04-24 13:02:05 -070080
81 for (UiView.Category cat : UiView.Category.values()) {
82 if (cat == UiView.Category.HIDDEN) {
83 continue;
84 }
85
86 List<UiView> catViews = getViewsForCat(extensions, cat);
87 if (!catViews.isEmpty()) {
88 addCatHeader(sb, cat);
89 addCatItems(sb, catViews);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080090 }
91 }
Simon Hunt38c2b6a2015-04-24 13:02:05 -070092
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080093 return new ByteArrayInputStream(sb.toString().getBytes());
94 }
Simon Hunt38c2b6a2015-04-24 13:02:05 -070095
96 private List<UiView> getViewsForCat(List<UiExtension> extensions,
97 UiView.Category cat) {
98 List<UiView> views = new ArrayList<>();
99 for (UiExtension extension : extensions) {
100 views.addAll(extension.views().stream().filter(
101 view -> cat.equals(view.category())
102 ).collect(Collectors.toList()));
103 }
104 return views;
105 }
106
107 private void addCatHeader(StringBuilder sb, UiView.Category cat) {
108 sb.append(String.format(HDR_FORMAT, cat.label()));
109 }
110
111 private void addCatItems(StringBuilder sb, List<UiView> catViews) {
112 for (UiView view : catViews) {
113 sb.append(String.format(NAV_FORMAT, view.id(), view.label()));
114 }
115 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800116}