Thomas Vachuska | 99b7bbe | 2018-02-01 15:29:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015-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.rest.resources; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.rest.AbstractWebResource; |
| 21 | import org.onosproject.ui.UiPreferencesService; |
| 22 | |
| 23 | import javax.ws.rs.DELETE; |
| 24 | import javax.ws.rs.GET; |
| 25 | import javax.ws.rs.PUT; |
| 26 | import javax.ws.rs.Path; |
| 27 | import javax.ws.rs.PathParam; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | import static org.onlab.util.Tools.nullIsNotFound; |
| 34 | |
| 35 | /** |
| 36 | * Manage user preferences. |
| 37 | */ |
| 38 | @Path("ui/preferences") |
| 39 | public class UiPreferencesWebResource extends AbstractWebResource { |
| 40 | |
| 41 | /** |
| 42 | * Gets all user preferences. |
| 43 | * |
| 44 | * @return 200 OK with user preferences JSON |
| 45 | */ |
| 46 | @GET |
| 47 | @Produces(MediaType.APPLICATION_JSON) |
| 48 | public Response download() { |
| 49 | UiPreferencesService service = get(UiPreferencesService.class); |
| 50 | ObjectMapper mapper = new ObjectMapper(); |
| 51 | ObjectNode root = mapper.createObjectNode(); |
| 52 | |
| 53 | service.getUserNames().forEach(user -> { |
| 54 | ObjectNode prefs = mapper.createObjectNode(); |
| 55 | root.set(user, prefs); |
| 56 | service.getPreferences(user).forEach(prefs::set); |
| 57 | }); |
| 58 | return ok(root).build(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets user preferences for the given user. |
| 63 | * |
| 64 | * @param user user name |
| 65 | * @return 200 OK with user preferences JSON |
| 66 | */ |
| 67 | @GET |
| 68 | @Path("{user}") |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | public Response download(@PathParam("user") String user) { |
| 71 | UiPreferencesService service = get(UiPreferencesService.class); |
| 72 | ObjectMapper mapper = new ObjectMapper(); |
| 73 | ObjectNode prefs = mapper.createObjectNode(); |
| 74 | service.getPreferences(user).forEach(prefs::set); |
| 75 | return ok(prefs).build(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the specified user preferences for the given user. |
| 80 | * |
| 81 | * @param user user name |
| 82 | * @param pref preferences name |
| 83 | * @return 200 OK with user preferences JSON |
| 84 | */ |
| 85 | @GET |
| 86 | @Path("{user}/{pref}") |
| 87 | @Produces(MediaType.APPLICATION_JSON) |
| 88 | public Response download(@PathParam("user") String user, |
| 89 | @PathParam("pref") String pref) { |
| 90 | UiPreferencesService service = get(UiPreferencesService.class); |
| 91 | return ok(nullIsNotFound(service.getPreference(user, pref), "No such preference")).build(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets the specified user preferences for the given user. |
| 96 | * |
| 97 | * @param user user name |
| 98 | * @param pref preferences name |
| 99 | * @param request preferences JSON |
| 100 | * @return 200 OK |
| 101 | * @throws IOException if given JSON is invalid |
| 102 | */ |
| 103 | @PUT |
| 104 | @Path("{user}/{pref}") |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
| 106 | public Response upload(@PathParam("user") String user, |
| 107 | @PathParam("pref") String pref, |
| 108 | String request) throws IOException { |
| 109 | UiPreferencesService service = get(UiPreferencesService.class); |
| 110 | ObjectNode json = (ObjectNode) mapper().readTree(request); |
| 111 | service.setPreference(user, pref, json); |
| 112 | return Response.ok().build(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Removes the specified user preferences for the given user. |
| 117 | * |
| 118 | * @param user user name |
| 119 | * @param pref preferences name |
| 120 | * @return 204 no content |
| 121 | */ |
| 122 | @DELETE |
| 123 | @Path("{user}/{pref}") |
| 124 | @Produces(MediaType.APPLICATION_JSON) |
| 125 | public Response remove(@PathParam("user") String user, |
| 126 | @PathParam("pref") String pref) { |
| 127 | UiPreferencesService service = get(UiPreferencesService.class); |
| 128 | service.setPreference(user, pref, null); |
| 129 | return Response.noContent().build(); |
| 130 | } |
| 131 | |
| 132 | } |