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