Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -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 | e95da77 | 2015-02-23 15:50:11 -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.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 | import static org.onosproject.ui.impl.MainViewResource.SCRIPT; |
| 35 | |
| 36 | /** |
| 37 | * Resource for serving the dynamically composed onos.js. |
| 38 | */ |
Thomas Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 39 | @Path("/") |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 40 | public class MainModuleResource extends AbstractInjectionResource { |
| 41 | |
| 42 | private static final String MAIN_JS = "onos.js"; |
| 43 | |
chengfan | 386620e | 2016-11-09 17:02:40 +0800 | [diff] [blame] | 44 | private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-DATA-START}"; |
| 45 | private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-DATA-END}"; |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 46 | private static final String PREFIX = " '"; |
chengfan | 386620e | 2016-11-09 17:02:40 +0800 | [diff] [blame] | 47 | private static final String MIDFIX = "' : '"; |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 48 | private static final String SUFFIX = String.format("',%n"); |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 49 | |
| 50 | @GET |
| 51 | @Produces(SCRIPT) |
| 52 | public Response getMainModule() throws IOException { |
| 53 | UiExtensionService service = get(UiExtensionService.class); |
| 54 | InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS); |
| 55 | String js = new String(toByteArray(jsTemplate)); |
| 56 | |
Simon Hunt | 4092733 | 2016-01-22 15:29:47 -0800 | [diff] [blame] | 57 | int p1s = split(js, 0, INJECT_VIEW_IDS_START) - INJECT_VIEW_IDS_START.length(); |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 58 | int p1e = split(js, 0, INJECT_VIEW_IDS_END); |
| 59 | int p2s = split(js, p1e, null); |
| 60 | |
| 61 | StreamEnumeration streams = |
| 62 | new StreamEnumeration(of(stream(js, 0, p1s), |
| 63 | includeViewIds(service), |
| 64 | stream(js, p1e, p2s))); |
| 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()) { |
chengfan | 386620e | 2016-11-09 17:02:40 +0800 | [diff] [blame] | 74 | sb.append(PREFIX) |
| 75 | .append(view.id()) |
| 76 | .append(MIDFIX) |
| 77 | .append(sanitizeUrl(view.helpPageUrl())) |
| 78 | .append(SUFFIX); |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | return new ByteArrayInputStream(sb.toString().getBytes()); |
| 82 | } |
| 83 | |
chengfan | 386620e | 2016-11-09 17:02:40 +0800 | [diff] [blame] | 84 | private String sanitizeUrl(String url) { |
| 85 | // TODO: add logic for validating URL |
| 86 | return url; |
| 87 | } |
| 88 | |
Thomas Vachuska | e95da77 | 2015-02-23 15:50:11 -0800 | [diff] [blame] | 89 | } |