blob: 42d1dff9df0c919ed3aef8cdce78b2207bfc1017 [file] [log] [blame]
Thomas Vachuskae95da772015-02-23 15:50:11 -08001/*
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 */
16package org.onosproject.ui.impl;
17
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070018import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskae95da772015-02-23 15:50:11 -080019import org.onosproject.ui.UiExtension;
20import org.onosproject.ui.UiExtensionService;
21import org.onosproject.ui.UiView;
22
23import javax.ws.rs.GET;
24import javax.ws.rs.Path;
25import javax.ws.rs.Produces;
26import javax.ws.rs.core.Response;
27import java.io.ByteArrayInputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.SequenceInputStream;
31
32import static com.google.common.collect.ImmutableList.of;
33import static com.google.common.io.ByteStreams.toByteArray;
34import static org.onosproject.ui.impl.MainViewResource.SCRIPT;
35
36/**
37 * Resource for serving the dynamically composed onos.js.
38 */
Thomas Vachuska529db0a2015-02-23 16:42:14 -080039@Path("/")
Thomas Vachuskae95da772015-02-23 15:50:11 -080040public class MainModuleResource extends AbstractInjectionResource {
41
42 private static final String MAIN_JS = "onos.js";
43
44 private static final String INJECT_VIEW_IDS_START = "// {INJECTED-VIEW-IDS-START}";
45 private static final String INJECT_VIEW_IDS_END = "// {INJECTED-VIEW-IDS-END}";
46
47 @GET
48 @Produces(SCRIPT)
49 public Response getMainModule() throws IOException {
50 UiExtensionService service = get(UiExtensionService.class);
51 InputStream jsTemplate = getClass().getClassLoader().getResourceAsStream(MAIN_JS);
52 String js = new String(toByteArray(jsTemplate));
53
54 int p1s = split(js, 0, INJECT_VIEW_IDS_START);
55 int p1e = split(js, 0, INJECT_VIEW_IDS_END);
56 int p2s = split(js, p1e, null);
57
58 StreamEnumeration streams =
59 new StreamEnumeration(of(stream(js, 0, p1s),
60 includeViewIds(service),
61 stream(js, p1e, p2s)));
62
63 return Response.ok(new SequenceInputStream(streams)).build();
64 }
65
66 // Produces an input stream including view id injections from all extensions.
67 private InputStream includeViewIds(UiExtensionService service) {
68 StringBuilder sb = new StringBuilder("\n");
69 for (UiExtension extension : service.getExtensions()) {
70 for (UiView view : extension.views()) {
71 sb.append(" '").append(view.id()).append("',");
72 }
73 }
74 return new ByteArrayInputStream(sb.toString().getBytes());
75 }
76
77}