blob: da4079f2f3022ae733796f81c30a0e9b539b8d60 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -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
18import com.google.common.collect.ImmutableList;
19import org.onosproject.ui.UiExtension;
20import org.onosproject.ui.UiExtensionService;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
24import javax.ws.rs.Produces;
25import javax.ws.rs.core.MediaType;
26import javax.ws.rs.core.Response;
27import java.io.IOException;
28import java.io.InputStream;
29import java.io.SequenceInputStream;
30
31import static com.google.common.collect.ImmutableList.of;
32import static com.google.common.io.ByteStreams.toByteArray;
33
34/**
35 * Resource for serving the dynamically composed index.html.
36 */
37@Path("/")
38public class MainIndexResource extends AbstractInjectionResource {
39
40 private static final String INDEX = "/index-template.html";
41
42 private static final String INJECT_CSS = "<!-- {INJECTED-STYLESHEETS} -->";
43 private static final String INJECT_JS = "<!-- {INJECTED-JAVASCRIPT} -->";
44
45 @Path("/")
46 @GET
47 @Produces(MediaType.TEXT_HTML)
48 public Response getMainIndex() throws IOException {
49 UiExtensionService service = get(UiExtensionService.class);
50 InputStream indexTemplate = getClass().getClassLoader().getResourceAsStream(INDEX);
51 String index = new String(toByteArray(indexTemplate));
52
53 int p1 = split(index, 0, INJECT_JS);
54 int p2 = split(index, p1, INJECT_CSS);
55 int p3 = split(index, p2, null);
56
57 StreamEnumeration streams =
58 new StreamEnumeration(of(stream(index, 0, p1),
59 includeJs(service),
60 stream(index, p1, p2),
61 includeCss(service),
62 stream(index, p2, p3)));
63
64 return Response.ok(new SequenceInputStream(streams)).build();
65 }
66
67 // Produces an input stream including CSS injections from all extensions.
68 private InputStream includeCss(UiExtensionService service) {
69 ImmutableList.Builder<InputStream> builder = ImmutableList.builder();
70 for (UiExtension extension : service.getExtensions()) {
71 builder.add(extension.css());
72 }
73 return new SequenceInputStream(new StreamEnumeration(builder.build()));
74 }
75
76 // Produces an input stream including JS injections from all extensions.
77 private InputStream includeJs(UiExtensionService service) {
78 ImmutableList.Builder<InputStream> builder = ImmutableList.builder();
79 for (UiExtension extension : service.getExtensions()) {
80 builder.add(extension.js());
81 }
82 return new SequenceInputStream(new StreamEnumeration(builder.build()));
83 }
84
85}