blob: 130a7eab853780bfe7fe4a14da2f38c2f0a68bd5 [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
Thomas Vachuska0fa2aa12015-08-18 12:53:04 -070018import org.onosproject.rest.AbstractInjectionResource;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080019import org.onosproject.ui.UiExtension;
20import org.onosproject.ui.UiExtensionService;
21
22import javax.ws.rs.GET;
23import javax.ws.rs.Path;
24import javax.ws.rs.PathParam;
25import javax.ws.rs.core.Response;
26import java.io.IOException;
27
28import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
29import static javax.ws.rs.core.MediaType.TEXT_HTML;
30
31/**
32 * Resource for serving the dynamically composed onos.js.
33 */
34@Path("/")
35public class MainViewResource extends AbstractInjectionResource {
36
Thomas Vachuskaa0509892015-02-21 22:18:41 -080037 static final String CONTENT_TYPE = "Content-Type";
38 static final String STYLESHEET = "text/css";
39 static final String SCRIPT = "text/javascript";
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080040
41 @Path("{view}/{resource}")
42 @GET
43 public Response getViewResource(@PathParam("view") String viewId,
44 @PathParam("resource") String resource) throws IOException {
45 UiExtensionService service = get(UiExtensionService.class);
46 UiExtension extension = service.getViewExtension(viewId);
47 return extension != null ?
48 Response.ok(extension.resource(viewId, resource))
49 .header(CONTENT_TYPE, contentType(resource)).build() :
50 Response.status(Response.Status.NOT_FOUND).build();
51 }
52
53 static String contentType(String resource) {
54 return resource.endsWith(".html") ? TEXT_HTML :
55 resource.endsWith(".css") ? STYLESHEET :
56 resource.endsWith(".js") ? SCRIPT :
57 APPLICATION_OCTET_STREAM;
58 }
59
60}