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