001 /*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020 package org.crsh.shell.impl.remoting;
021
022 import org.crsh.cli.impl.completion.CompletionMatch;
023 import org.crsh.shell.ErrorType;
024 import org.crsh.shell.ShellResponse;
025
026 import java.io.IOException;
027 import java.io.ObjectInputStream;
028 import java.io.ObjectOutputStream;
029 import java.io.Serializable;
030
031 public class ServerMessage implements Serializable {
032
033 public static class Welcome extends ServerMessage {
034
035 /** . */
036 public final String value;
037
038 public Welcome(String value) {
039 this.value = value;
040 }
041 }
042
043 public static class Prompt extends ServerMessage {
044
045 /** . */
046 public final String value;
047
048 public Prompt(String value) {
049 this.value = value;
050 }
051 }
052
053 public static class Completion extends ServerMessage {
054
055 /** . */
056 public final CompletionMatch value;
057
058 public Completion(CompletionMatch value) {
059 this.value = value;
060 }
061 }
062
063 public static class UseMainBuffer extends ServerMessage {
064
065 }
066
067 public static class UseAlternateBuffer extends ServerMessage {
068
069 }
070
071 public static class GetSize extends ServerMessage {
072
073 }
074
075 public static class ReadLine extends ServerMessage {
076
077 }
078
079 public static class Chunk extends ServerMessage {
080
081 /** . */
082 public final org.crsh.text.Chunk payload;
083
084 public Chunk(org.crsh.text.Chunk payload) {
085 this.payload = payload;
086 }
087 }
088
089 public static class Flush extends ServerMessage {
090
091 }
092
093 public static class End extends ServerMessage {
094
095 /** . */
096 public ShellResponse response;
097
098 public End(ShellResponse response) {
099 if (response == null) {
100 throw new NullPointerException("No null response accepted");
101 }
102
103 //
104 this.response = response;
105 }
106
107 private void writeObject(ObjectOutputStream oos) throws IOException {
108 if (response instanceof ShellResponse.Error) {
109 oos.writeBoolean(false);
110 ShellResponse.Error error = (ShellResponse.Error)response;
111 oos.writeObject(error.getType());
112 oos.writeObject(error.getMessage());
113 oos.writeObject(error.getThrowable().getMessage());
114 oos.writeObject(error.getThrowable().getStackTrace());
115 } else {
116 oos.writeBoolean(true);
117 oos.writeObject(response);
118 }
119 }
120
121 private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
122 if (ois.readBoolean()) {
123 response = (ShellResponse)ois.readObject();
124 } else {
125 ErrorType type = (ErrorType)ois.readObject();
126 String message = (String)ois.readObject();
127 String errorMessage = (String)ois.readObject();
128 StackTraceElement[] errorTrace = (StackTraceElement[])ois.readObject();
129 Exception ex = new Exception(errorMessage);
130 ex.setStackTrace(errorTrace);
131 response = ShellResponse.error(type, message, ex);
132 }
133 }
134 }
135 }