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