Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
Thomas Vachuska | 0fa2aa1 | 2015-08-18 12:53:04 -0700 | [diff] [blame] | 18 | import org.onosproject.rest.AbstractInjectionResource; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 19 | import org.onosproject.ui.UiExtension; |
| 20 | import org.onosproject.ui.UiExtensionService; |
| 21 | import org.onosproject.ui.UiView; |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 22 | import org.onosproject.ui.lion.LionBundle; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.Path; |
| 26 | import javax.ws.rs.Produces; |
| 27 | import javax.ws.rs.core.MediaType; |
| 28 | import javax.ws.rs.core.Response; |
| 29 | import java.io.ByteArrayInputStream; |
| 30 | import java.io.IOException; |
| 31 | import java.io.InputStream; |
| 32 | import java.io.SequenceInputStream; |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | import java.util.stream.Collectors; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | |
| 37 | import static com.google.common.collect.ImmutableList.of; |
| 38 | import static com.google.common.io.ByteStreams.toByteArray; |
| 39 | |
| 40 | /** |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 41 | * Resource for serving the dynamically composed nav.html. |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 42 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 43 | @Path("/") |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 44 | public class MainNavResource extends AbstractInjectionResource { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 45 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 46 | private static final String NAV_HTML = "nav.html"; |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 47 | |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 48 | private static final String INJECT_VIEW_ITEMS_START = |
| 49 | "<!-- {INJECTED-VIEW-NAV-START} -->"; |
| 50 | private static final String INJECT_VIEW_ITEMS_END = |
| 51 | "<!-- {INJECTED-VIEW-NAV-END} -->"; |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 52 | |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 53 | private static final String HDR_FORMAT = |
Phil Huang | 111bc59 | 2016-01-15 01:55:00 +0800 | [diff] [blame] | 54 | "<div class=\"nav-hdr\">%s</div>%n"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 55 | private static final String NAV_FORMAT = |
Phil Huang | 111bc59 | 2016-01-15 01:55:00 +0800 | [diff] [blame] | 56 | "<a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s %s</a>%n"; |
Simon Hunt | fb35b83 | 2016-05-26 11:43:00 -0700 | [diff] [blame] | 57 | private static final String ICON_FORMAT = |
| 58 | "<div icon icon-id=\"%s\"></div>"; |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 59 | |
| 60 | private static final String BLANK_GLYPH = "unknown"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 61 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 62 | @GET |
| 63 | @Produces(MediaType.TEXT_HTML) |
| 64 | public Response getNavigation() throws IOException { |
| 65 | UiExtensionService service = get(UiExtensionService.class); |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 66 | InputStream navTemplate = |
| 67 | getClass().getClassLoader().getResourceAsStream(NAV_HTML); |
| 68 | String html = new String(toByteArray(navTemplate)); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 70 | int p1s = split(html, 0, INJECT_VIEW_ITEMS_START); |
| 71 | int p1e = split(html, 0, INJECT_VIEW_ITEMS_END); |
| 72 | int p2s = split(html, p1e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 73 | |
| 74 | StreamEnumeration streams = |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 75 | new StreamEnumeration(of(stream(html, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 76 | includeNavItems(service), |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 77 | stream(html, p1e, p2s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 78 | |
| 79 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 80 | } |
| 81 | |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 82 | // Produces an input stream of nav item injections from all extensions. |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 83 | private InputStream includeNavItems(UiExtensionService service) { |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 84 | List<UiExtension> extensions = service.getExtensions(); |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 85 | LionBundle navLion = service.getNavLionBundle(); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 86 | StringBuilder sb = new StringBuilder("\n"); |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 87 | |
| 88 | for (UiView.Category cat : UiView.Category.values()) { |
| 89 | if (cat == UiView.Category.HIDDEN) { |
| 90 | continue; |
| 91 | } |
| 92 | |
| 93 | List<UiView> catViews = getViewsForCat(extensions, cat); |
| 94 | if (!catViews.isEmpty()) { |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 95 | addCatHeader(sb, cat, navLion); |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 96 | addCatItems(sb, catViews); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 99 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 100 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 101 | } |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 102 | |
| 103 | private List<UiView> getViewsForCat(List<UiExtension> extensions, |
| 104 | UiView.Category cat) { |
| 105 | List<UiView> views = new ArrayList<>(); |
| 106 | for (UiExtension extension : extensions) { |
| 107 | views.addAll(extension.views().stream().filter( |
| 108 | view -> cat.equals(view.category()) |
| 109 | ).collect(Collectors.toList())); |
| 110 | } |
| 111 | return views; |
| 112 | } |
| 113 | |
Simon Hunt | 23f9c7b | 2017-07-10 20:00:30 -0700 | [diff] [blame] | 114 | private void addCatHeader(StringBuilder sb, UiView.Category cat, |
| 115 | LionBundle navLion) { |
| 116 | String key = "cat_" + cat.name().toLowerCase(); |
| 117 | sb.append(String.format(HDR_FORMAT, navLion.getValue(key))); |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private void addCatItems(StringBuilder sb, List<UiView> catViews) { |
| 121 | for (UiView view : catViews) { |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 122 | sb.append(String.format(NAV_FORMAT, view.id(), icon(view), view.label())); |
Simon Hunt | 38c2b6a | 2015-04-24 13:02:05 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 125 | |
| 126 | private String icon(UiView view) { |
| 127 | String gid = view.iconId() == null ? BLANK_GLYPH : view.iconId(); |
Simon Hunt | fb35b83 | 2016-05-26 11:43:00 -0700 | [diff] [blame] | 128 | return String.format(ICON_FORMAT, gid); |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 129 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 130 | } |