Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -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 | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 19 | import org.onosproject.ui.UiExtension; |
| 20 | import org.onosproject.ui.UiExtensionService; |
| 21 | |
| 22 | import javax.ws.rs.GET; |
| 23 | import javax.ws.rs.Path; |
| 24 | import javax.ws.rs.PathParam; |
| 25 | import javax.ws.rs.core.Response; |
| 26 | import java.io.IOException; |
| 27 | |
| 28 | import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; |
| 29 | import static javax.ws.rs.core.MediaType.TEXT_HTML; |
| 30 | |
| 31 | /** |
| 32 | * Resource for serving the dynamically composed onos.js. |
| 33 | */ |
| 34 | @Path("/") |
| 35 | public class MainViewResource extends AbstractInjectionResource { |
| 36 | |
Thomas Vachuska | a050989 | 2015-02-21 22:18:41 -0800 | [diff] [blame] | 37 | static final String CONTENT_TYPE = "Content-Type"; |
| 38 | static final String STYLESHEET = "text/css"; |
| 39 | static final String SCRIPT = "text/javascript"; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 40 | |
| 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 | } |