blob: 44a8ef044cfc025149fcdea6479f74da65df2e35 [file] [log] [blame]
Thomas Vachuska0af26912016-03-21 21:37:30 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska0af26912016-03-21 21:37:30 -07003 *
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
17package org.onosproject.ui.impl;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.ui.RequestHandler;
22import org.onosproject.ui.UiMessageHandler;
23import org.onosproject.ui.UiPreferencesService;
24
25import java.util.Collection;
26
27import static com.google.common.base.Strings.isNullOrEmpty;
28
29/**
30 * Message handler for intercepting user preferences messages.
31 */
32class UserPreferencesMessageHandler extends UiMessageHandler {
33
34 private static final String UPDATE_PREFS_REQ = "updatePrefReq";
35 private static final String KEY = "key";
36 private static final String VALUE = "value";
37
38 @Override
39 protected Collection<RequestHandler> createRequestHandlers() {
40 return ImmutableSet.of(new UpdatePreferencesRequest());
41 }
42
43 private final class UpdatePreferencesRequest extends RequestHandler {
44 private UpdatePreferencesRequest() {
45 super(UPDATE_PREFS_REQ);
46 }
47
48 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -080049 public void process(ObjectNode payload) {
Thomas Vachuska0af26912016-03-21 21:37:30 -070050 if (!isNullOrEmpty(connection().userName())) {
51 UiPreferencesService service = get(UiPreferencesService.class);
52 service.setPreference(connection().userName(),
Simon Hunt8a0429a2017-01-06 16:52:47 -080053 payload.get(KEY).asText(),
54 (ObjectNode) payload.get(VALUE));
Thomas Vachuska0af26912016-03-21 21:37:30 -070055 }
56 }
57 }
58}