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; |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 34 | import static org.onosproject.ui.impl.MainViewResource.SCRIPT; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Resource for serving the dynamically composed onos.js. |
| 38 | */ |
| 39 | @Path("/") |
| 40 | public class MainExtResource extends AbstractInjectionResource { |
| 41 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 42 | private static final String MAIN_JS = "templates/onos-template.js"; |
| 43 | private static final String NAV_HTML = "templates/nav-template.html"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 45 | private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-IDS-START}"; |
| 46 | private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-IDS-END}"; |
| 47 | |
| 48 | private static final String INJECT_VIEW_ITEMS_START = "<!-- {INJECTED-VIEW-NAV-START} -->"; |
| 49 | private static final String INJECT_VIEW_ITEMS_END = "<!-- {INJECTED-VIEW-NAV-END} -->"; |
| 50 | |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 51 | |
| 52 | private static final String NAV_FORMAT = |
| 53 | " <li> <a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a></li>"; |
| 54 | |
| 55 | @Path("/onos.js") |
| 56 | @GET |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 57 | @Produces(SCRIPT) |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 58 | public Response getMainModule() throws IOException { |
| 59 | UiExtensionService service = get(UiExtensionService.class); |
| 60 | InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS); |
| 61 | String js = new String(toByteArray(jsTemplate)); |
| 62 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 63 | int p1s = split(js, 0, INJECT_VIEW_IDS_START); |
| 64 | int p1e = split(js, 0, INJECT_VIEW_IDS_END); |
| 65 | int p2s = split(js, p1e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 66 | |
| 67 | StreamEnumeration streams = |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 68 | new StreamEnumeration(of(stream(js, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 69 | includeViewIds(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 70 | stream(js, p1e, p2s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 71 | |
| 72 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 73 | } |
| 74 | |
| 75 | // Produces an input stream including view id injections from all extensions. |
| 76 | private InputStream includeViewIds(UiExtensionService service) { |
| 77 | StringBuilder sb = new StringBuilder("\n"); |
| 78 | for (UiExtension extension : service.getExtensions()) { |
| 79 | for (UiView view : extension.views()) { |
| 80 | sb.append(" '").append(view.id()).append("',"); |
| 81 | } |
| 82 | } |
| 83 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 84 | } |
| 85 | |
| 86 | @Path("/nav/nav.html") |
| 87 | @GET |
| 88 | @Produces(MediaType.TEXT_HTML) |
| 89 | public Response getNavigation() throws IOException { |
| 90 | UiExtensionService service = get(UiExtensionService.class); |
| 91 | InputStream navTemplate = getClass().getClassLoader().getResourceAsStream(NAV_HTML); |
| 92 | String js = new String(toByteArray(navTemplate)); |
| 93 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 94 | int p1s = split(js, 0, INJECT_VIEW_ITEMS_START); |
| 95 | int p1e = split(js, 0, INJECT_VIEW_ITEMS_END); |
| 96 | int p2s = split(js, p1e, null); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 97 | |
| 98 | StreamEnumeration streams = |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 99 | new StreamEnumeration(of(stream(js, 0, p1s), |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 100 | includeNavItems(service), |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame^] | 101 | stream(js, p1e, p2s))); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 102 | |
| 103 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 104 | } |
| 105 | |
| 106 | // Produces an input stream including nav item injections from all extensions. |
| 107 | private InputStream includeNavItems(UiExtensionService service) { |
| 108 | StringBuilder sb = new StringBuilder("\n"); |
| 109 | for (UiExtension extension : service.getExtensions()) { |
| 110 | for (UiView view : extension.views()) { |
| 111 | sb.append(String.format(NAV_FORMAT, view.id(), view.label())); |
| 112 | } |
| 113 | } |
| 114 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 115 | } |
| 116 | } |