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 | |
| 18 | import org.onosproject.ui.UiExtension; |
| 19 | import org.onosproject.ui.UiExtensionService; |
| 20 | import org.onosproject.ui.UiView; |
| 21 | |
| 22 | import javax.ws.rs.GET; |
| 23 | import javax.ws.rs.Path; |
| 24 | import javax.ws.rs.Produces; |
| 25 | import javax.ws.rs.core.MediaType; |
| 26 | import javax.ws.rs.core.Response; |
| 27 | import java.io.ByteArrayInputStream; |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.SequenceInputStream; |
| 31 | |
| 32 | import static com.google.common.collect.ImmutableList.of; |
| 33 | import static com.google.common.io.ByteStreams.toByteArray; |
| 34 | |
| 35 | /** |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 36 | * Resource for serving the dynamically composed nav.html. |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 37 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 38 | @Path("/") |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 39 | public class MainNavResource extends AbstractInjectionResource { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 41 | private static final String NAV_HTML = "nav.html"; |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 42 | |
| 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 46 | |
| 47 | private static final String NAV_FORMAT = |
Thomas Vachuska | 9730ec9 | 2015-03-07 17:25:21 -0800 | [diff] [blame] | 48 | "<a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a>\n"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 49 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 50 | @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 Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 57 | 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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 60 | |
| 61 | StreamEnumeration streams = |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 62 | new StreamEnumeration(of(stream(js, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 63 | includeNavItems(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 64 | stream(js, p1e, p2s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 65 | |
| 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 | } |