|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.zeppelin.client; |
| 19 | + |
| 20 | +import com.google.gson.Gson; |
| 21 | +import org.slf4j.Logger; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.HashSet; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +/** |
| 31 | + * Zeppelin websocket massage template class. |
| 32 | + */ |
| 33 | +public class Message implements JsonSerializable { |
| 34 | + /** |
| 35 | + * Representation of event type. |
| 36 | + */ |
| 37 | + public static enum OP { |
| 38 | + GET_HOME_NOTE, // [c-s] load note for home screen |
| 39 | + |
| 40 | + GET_NOTE, // [c-s] client load note |
| 41 | + // @param id note id |
| 42 | + |
| 43 | + NOTE, // [s-c] note info |
| 44 | + // @param note serialized Note object |
| 45 | + |
| 46 | + PARAGRAPH, // [s-c] paragraph info |
| 47 | + // @param paragraph serialized paragraph object |
| 48 | + |
| 49 | + PROGRESS, // [s-c] progress update |
| 50 | + // @param id paragraph id |
| 51 | + // @param progress percentage progress |
| 52 | + |
| 53 | + NEW_NOTE, // [c-s] create new notebook |
| 54 | + DEL_NOTE, // [c-s] delete notebook |
| 55 | + // @param id note id |
| 56 | + REMOVE_FOLDER, |
| 57 | + MOVE_NOTE_TO_TRASH, |
| 58 | + MOVE_FOLDER_TO_TRASH, |
| 59 | + RESTORE_FOLDER, |
| 60 | + RESTORE_NOTE, |
| 61 | + RESTORE_ALL, |
| 62 | + EMPTY_TRASH, |
| 63 | + CLONE_NOTE, // [c-s] clone new notebook |
| 64 | + // @param id id of note to clone |
| 65 | + // @param name name for the cloned note |
| 66 | + IMPORT_NOTE, // [c-s] import notebook |
| 67 | + // @param object notebook |
| 68 | + |
| 69 | + CONVERT_NOTE_NBFORMAT, // [c-s] converting note to nbformat |
| 70 | + CONVERTED_NOTE_NBFORMAT, // [s-c] converting note to nbformat |
| 71 | + |
| 72 | + NOTE_UPDATE, |
| 73 | + |
| 74 | + NOTE_RENAME, |
| 75 | + |
| 76 | + UPDATE_PERSONALIZED_MODE, // [c-s] update personalized mode (boolean) |
| 77 | + // @param note id and boolean personalized mode value |
| 78 | + |
| 79 | + FOLDER_RENAME, |
| 80 | + |
| 81 | + RUN_PARAGRAPH, // [c-s] run paragraph |
| 82 | + // @param id paragraph id |
| 83 | + // @param paragraph paragraph content.ie. script |
| 84 | + // @param config paragraph config |
| 85 | + // @param params paragraph params |
| 86 | + |
| 87 | + COMMIT_PARAGRAPH, // [c-s] commit paragraph |
| 88 | + // @param id paragraph id |
| 89 | + // @param title paragraph title |
| 90 | + // @param paragraph paragraph content.ie. script |
| 91 | + // @param config paragraph config |
| 92 | + // @param params paragraph params |
| 93 | + |
| 94 | + CANCEL_PARAGRAPH, // [c-s] cancel paragraph run |
| 95 | + // @param id paragraph id |
| 96 | + |
| 97 | + MOVE_PARAGRAPH, // [c-s] move paragraph order |
| 98 | + // @param id paragraph id |
| 99 | + // @param index index the paragraph want to go |
| 100 | + |
| 101 | + INSERT_PARAGRAPH, // [c-s] create new paragraph below current paragraph |
| 102 | + // @param target index |
| 103 | + |
| 104 | + COPY_PARAGRAPH, // [c-s] create new para below current para as a copy of current para |
| 105 | + // @param target index |
| 106 | + // @param title paragraph title |
| 107 | + // @param paragraph paragraph content.ie. script |
| 108 | + // @param config paragraph config |
| 109 | + // @param params paragraph params |
| 110 | + |
| 111 | + EDITOR_SETTING, // [c-s] ask paragraph editor setting |
| 112 | + // @param paragraph text keyword written in paragraph |
| 113 | + // ex) spark.spark or spark |
| 114 | + |
| 115 | + COMPLETION, // [c-s] ask completion candidates |
| 116 | + // @param id |
| 117 | + // @param buf current code |
| 118 | + // @param cursor cursor position in code |
| 119 | + |
| 120 | + COMPLETION_LIST, // [s-c] send back completion candidates list |
| 121 | + // @param id |
| 122 | + // @param completions list of string |
| 123 | + |
| 124 | + LIST_NOTES, // [c-s] ask list of note |
| 125 | + RELOAD_NOTES_FROM_REPO, // [c-s] reload notes from repo |
| 126 | + |
| 127 | + NOTES_INFO, // [s-c] list of note infos |
| 128 | + // @param notes serialized List<NoteInfo> object |
| 129 | + |
| 130 | + PARAGRAPH_REMOVE, |
| 131 | + PARAGRAPH_CLEAR_OUTPUT, // [c-s] clear output of paragraph |
| 132 | + PARAGRAPH_CLEAR_ALL_OUTPUT, // [c-s] clear output of all paragraphs |
| 133 | + PARAGRAPH_APPEND_OUTPUT, // [s-c] append output |
| 134 | + PARAGRAPH_UPDATE_OUTPUT, // [s-c] update (replace) output |
| 135 | + PING, |
| 136 | + AUTH_INFO, |
| 137 | + |
| 138 | + ANGULAR_OBJECT_UPDATE, // [s-c] add/update angular object |
| 139 | + ANGULAR_OBJECT_REMOVE, // [s-c] add angular object del |
| 140 | + |
| 141 | + ANGULAR_OBJECT_UPDATED, // [c-s] angular object value updated, |
| 142 | + |
| 143 | + ANGULAR_OBJECT_CLIENT_BIND, // [c-s] angular object updated from AngularJS z object |
| 144 | + |
| 145 | + ANGULAR_OBJECT_CLIENT_UNBIND, // [c-s] angular object unbind from AngularJS z object |
| 146 | + |
| 147 | + LIST_CONFIGURATIONS, // [c-s] ask all key/value pairs of configurations |
| 148 | + CONFIGURATIONS_INFO, // [s-c] all key/value pairs of configurations |
| 149 | + // @param settings serialized Map<String, String> object |
| 150 | + |
| 151 | + CHECKPOINT_NOTE, // [c-s] checkpoint note to storage repository |
| 152 | + // @param noteId |
| 153 | + // @param checkpointName |
| 154 | + |
| 155 | + LIST_REVISION_HISTORY, // [c-s] list revision history of the notebook |
| 156 | + // @param noteId |
| 157 | + NOTE_REVISION, // [c-s] get certain revision of note |
| 158 | + // @param noteId |
| 159 | + // @param revisionId |
| 160 | + SET_NOTE_REVISION, // [c-s] set current notebook head to this revision |
| 161 | + // @param noteId |
| 162 | + // @param revisionId |
| 163 | + NOTE_REVISION_FOR_COMPARE, // [c-s] get certain revision of note for compare |
| 164 | + // @param noteId |
| 165 | + // @param revisionId |
| 166 | + // @param position |
| 167 | + APP_APPEND_OUTPUT, // [s-c] append output |
| 168 | + APP_UPDATE_OUTPUT, // [s-c] update (replace) output |
| 169 | + APP_LOAD, // [s-c] on app load |
| 170 | + APP_STATUS_CHANGE, // [s-c] on app status change |
| 171 | + |
| 172 | + LIST_NOTE_JOBS, // [c-s] get note job management information |
| 173 | + LIST_UPDATE_NOTE_JOBS, // [c-s] get job management information for until unixtime |
| 174 | + UNSUBSCRIBE_UPDATE_NOTE_JOBS, // [c-s] unsubscribe job information for job management |
| 175 | + // @param unixTime |
| 176 | + GET_INTERPRETER_BINDINGS, // [c-s] get interpreter bindings |
| 177 | + SAVE_INTERPRETER_BINDINGS, // [c-s] save interpreter bindings |
| 178 | + INTERPRETER_BINDINGS, // [s-c] interpreter bindings |
| 179 | + |
| 180 | + GET_INTERPRETER_SETTINGS, // [c-s] get interpreter settings |
| 181 | + INTERPRETER_SETTINGS, // [s-c] interpreter settings |
| 182 | + ERROR_INFO, // [s-c] error information to be sent |
| 183 | + SESSION_LOGOUT, // [s-c] error information to be sent |
| 184 | + WATCHER, // [s-c] Change websocket to watcher mode. |
| 185 | + PARAGRAPH_ADDED, // [s-c] paragraph is added |
| 186 | + PARAGRAPH_REMOVED, // [s-c] paragraph deleted |
| 187 | + PARAGRAPH_MOVED, // [s-c] paragraph moved |
| 188 | + NOTE_UPDATED, // [s-c] paragraph updated(name, config) |
| 189 | + RUN_ALL_PARAGRAPHS, // [c-s] run all paragraphs |
| 190 | + PARAGRAPH_EXECUTED_BY_SPELL, // [c-s] paragraph was executed by spell |
| 191 | + RUN_PARAGRAPH_USING_SPELL, // [s-c] run paragraph using spell |
| 192 | + PARAS_INFO, // [s-c] paragraph runtime infos |
| 193 | + SAVE_NOTE_FORMS, // save note forms |
| 194 | + REMOVE_NOTE_FORMS, // remove note forms |
| 195 | + INTERPRETER_INSTALL_STARTED, // [s-c] start to download an interpreter |
| 196 | + INTERPRETER_INSTALL_RESULT, // [s-c] Status of an interpreter installation |
| 197 | + COLLABORATIVE_MODE_STATUS, // [s-c] collaborative mode status |
| 198 | + PATCH_PARAGRAPH, // [c-s][s-c] patch editor text |
| 199 | + NOTE_RUNNING_STATUS, // [s-c] sequential run status will be change |
| 200 | + NOTICE // [s-c] Notice |
| 201 | + } |
| 202 | + |
| 203 | + // these messages will be ignored during the sequential run of the note |
| 204 | + private static final Set<OP> disabledForRunningNoteMessages = Collections |
| 205 | + .unmodifiableSet(new HashSet<>(Arrays.asList( |
| 206 | + OP.COMMIT_PARAGRAPH, |
| 207 | + OP.RUN_PARAGRAPH, |
| 208 | + OP.RUN_PARAGRAPH_USING_SPELL, |
| 209 | + OP.RUN_ALL_PARAGRAPHS, |
| 210 | + OP.PARAGRAPH_CLEAR_OUTPUT, |
| 211 | + OP.PARAGRAPH_CLEAR_ALL_OUTPUT, |
| 212 | + OP.INSERT_PARAGRAPH, |
| 213 | + OP.MOVE_PARAGRAPH, |
| 214 | + OP.COPY_PARAGRAPH, |
| 215 | + OP.PARAGRAPH_REMOVE, |
| 216 | + OP.MOVE_NOTE_TO_TRASH, |
| 217 | + OP.DEL_NOTE, |
| 218 | + OP.PATCH_PARAGRAPH))); |
| 219 | + |
| 220 | + private static final Gson gson = new Gson(); |
| 221 | + public static final Message EMPTY = new Message(null); |
| 222 | + |
| 223 | + public OP op; |
| 224 | + public Map<String, Object> data = new HashMap<>(); |
| 225 | + public String ticket = "anonymous"; |
| 226 | + public String principal = "anonymous"; |
| 227 | + public String roles = ""; |
| 228 | + |
| 229 | + // Unique id generated from client side. to identify message. |
| 230 | + // When message from server is response to the client request |
| 231 | + // includes the msgId in response message, client can pair request and response message. |
| 232 | + // When server send message that is not response to the client request, set null; |
| 233 | + public String msgId = MSG_ID_NOT_DEFINED; |
| 234 | + public static String MSG_ID_NOT_DEFINED = null; |
| 235 | + |
| 236 | + public Message(OP op) { |
| 237 | + this.op = op; |
| 238 | + } |
| 239 | + |
| 240 | + public Message withMsgId(String msgId) { |
| 241 | + this.msgId = msgId; |
| 242 | + return this; |
| 243 | + } |
| 244 | + |
| 245 | + public Message put(String k, Object v) { |
| 246 | + data.put(k, v); |
| 247 | + return this; |
| 248 | + } |
| 249 | + |
| 250 | + public Object get(String k) { |
| 251 | + return data.get(k); |
| 252 | + } |
| 253 | + |
| 254 | + public static boolean isDisabledForRunningNotes(OP eventType) { |
| 255 | + return disabledForRunningNoteMessages.contains(eventType); |
| 256 | + } |
| 257 | + |
| 258 | + public <T> T getType(String key) { |
| 259 | + return (T) data.get(key); |
| 260 | + } |
| 261 | + |
| 262 | + public <T> T getType(String key, Logger LOG) { |
| 263 | + try { |
| 264 | + return getType(key); |
| 265 | + } catch (ClassCastException e) { |
| 266 | + LOG.error("Failed to get " + key + " from message (Invalid type). " , e); |
| 267 | + return null; |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + @Override |
| 272 | + public String toString() { |
| 273 | + final StringBuilder sb = new StringBuilder("Message{"); |
| 274 | + sb.append("data=").append(data); |
| 275 | + sb.append(", op=").append(op); |
| 276 | + sb.append('}'); |
| 277 | + return sb.toString(); |
| 278 | + } |
| 279 | + |
| 280 | + public String toJson() { |
| 281 | + return gson.toJson(this); |
| 282 | + } |
| 283 | + |
| 284 | + public static Message fromJson(String json) { |
| 285 | + return gson.fromJson(json, Message.class); |
| 286 | + } |
| 287 | +} |
0 commit comments