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 | /** |
| 36 | * Resource for serving the dynamically composed onos.js. |
| 37 | */ |
| 38 | @Path("/") |
| 39 | public class MainExtResource extends AbstractInjectionResource { |
| 40 | |
| 41 | private static final String MAIN_JS = "/onos-template.js"; |
| 42 | private static final String NAV_HTML = "/nav-template.html"; |
| 43 | |
| 44 | private static final String INJECT_VIEW_IDS = "// {INJECTED-VIEW-IDS}"; |
| 45 | private static final String INJECT_VIEW_ITEMS = "<!-- {INJECTED-VIEW-NAV} -->"; |
| 46 | |
| 47 | private static final String NAV_FORMAT = |
| 48 | " <li> <a ng-click=\"navCtrl.hideNav()\" href=\"#/%s\">%s</a></li>"; |
| 49 | |
| 50 | @Path("/onos.js") |
| 51 | @GET |
| 52 | @Produces(MediaType.TEXT_HTML) |
| 53 | public Response getMainModule() throws IOException { |
| 54 | UiExtensionService service = get(UiExtensionService.class); |
| 55 | InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS); |
| 56 | String js = new String(toByteArray(jsTemplate)); |
| 57 | |
| 58 | int p1 = split(js, 0, INJECT_VIEW_IDS); |
| 59 | int p2 = split(js, p1, null); |
| 60 | |
| 61 | StreamEnumeration streams = |
| 62 | new StreamEnumeration(of(stream(js, 0, p1), |
| 63 | includeViewIds(service), |
| 64 | stream(js, p1, p2))); |
| 65 | |
| 66 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 67 | } |
| 68 | |
| 69 | // Produces an input stream including view id injections from all extensions. |
| 70 | private InputStream includeViewIds(UiExtensionService service) { |
| 71 | StringBuilder sb = new StringBuilder("\n"); |
| 72 | for (UiExtension extension : service.getExtensions()) { |
| 73 | for (UiView view : extension.views()) { |
| 74 | sb.append(" '").append(view.id()).append("',"); |
| 75 | } |
| 76 | } |
| 77 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 78 | } |
| 79 | |
| 80 | @Path("/nav/nav.html") |
| 81 | @GET |
| 82 | @Produces(MediaType.TEXT_HTML) |
| 83 | public Response getNavigation() throws IOException { |
| 84 | UiExtensionService service = get(UiExtensionService.class); |
| 85 | InputStream navTemplate = getClass().getClassLoader().getResourceAsStream(NAV_HTML); |
| 86 | String js = new String(toByteArray(navTemplate)); |
| 87 | |
| 88 | int p1 = split(js, 0, INJECT_VIEW_ITEMS); |
| 89 | int p2 = split(js, p1, null); |
| 90 | |
| 91 | StreamEnumeration streams = |
| 92 | new StreamEnumeration(of(stream(js, 0, p1), |
| 93 | includeNavItems(service), |
| 94 | stream(js, p1, p2))); |
| 95 | |
| 96 | return Response.ok(new SequenceInputStream(streams)).build(); |
| 97 | } |
| 98 | |
| 99 | // Produces an input stream including nav item injections from all extensions. |
| 100 | private InputStream includeNavItems(UiExtensionService service) { |
| 101 | StringBuilder sb = new StringBuilder("\n"); |
| 102 | for (UiExtension extension : service.getExtensions()) { |
| 103 | for (UiView view : extension.views()) { |
| 104 | sb.append(String.format(NAV_FORMAT, view.id(), view.label())); |
| 105 | } |
| 106 | } |
| 107 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 108 | } |
| 109 | } |