Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.gui2; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.module.SimpleModule; |
| 21 | import org.onlab.rest.BaseResource; |
| 22 | import org.onosproject.ui.UiExtension; |
| 23 | import org.onosproject.ui.UiExtensionService; |
| 24 | import org.onosproject.ui.UiView; |
| 25 | |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.Path; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | |
| 32 | /** |
| 33 | * Resource for serving the list of UIExtensions. |
| 34 | */ |
| 35 | @Path("nav") |
| 36 | public class NavResource extends BaseResource { |
| 37 | |
| 38 | @GET |
| 39 | @Path("uiextensions") |
| 40 | @Produces(MediaType.APPLICATION_JSON) |
| 41 | public Response getNavigation() throws JsonProcessingException { |
| 42 | UiExtensionService service = get(UiExtensionService.class); |
| 43 | UiViewSerializer serializer = new UiViewSerializer(UiView.class); |
| 44 | ObjectMapper mapper = new ObjectMapper(); |
| 45 | |
| 46 | SimpleModule module = |
| 47 | new SimpleModule("UiViewSerializer"); |
| 48 | module.addSerializer(serializer); |
| 49 | mapper.registerModule(module); |
| 50 | StringBuilder sb = new StringBuilder("["); |
| 51 | boolean first = true; |
| 52 | for (UiExtension uiExt : service.getExtensions()) { |
| 53 | for (UiView view : uiExt.views()) { |
| 54 | if (first) { |
| 55 | first = false; |
| 56 | } else { |
| 57 | sb.append(","); |
| 58 | } |
| 59 | sb.append(mapper.writeValueAsString(view)); |
| 60 | } |
| 61 | } |
| 62 | sb.append("]"); |
| 63 | |
| 64 | return ok(sb.toString()).build(); |
| 65 | } |
| 66 | |
| 67 | } |