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.ErrorKind;
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 abstract class Chunk extends ServerMessage {
080    
081        public static class Text extends Chunk {
082    
083          /** . */
084          public final CharSequence payload;
085    
086          public Text(CharSequence payload) {
087            this.payload = payload;
088          }
089        }
090    
091        public static class Style extends Chunk {
092    
093          /** . */
094          public final org.crsh.text.Style payload;
095    
096          public Style(org.crsh.text.Style payload) {
097            this.payload = payload;
098          }
099        }
100    
101        public static class Cls extends Chunk {
102    
103          public Cls() {
104          }
105        }
106      }
107    
108      public static class Flush extends ServerMessage {
109      }
110    
111    
112    
113      public static class End extends ServerMessage {
114    
115        /** . */
116        public ShellResponse response;
117    
118        public End(ShellResponse response) {
119          if (response == null) {
120            throw new NullPointerException("No null response accepted");
121          }
122    
123          //
124          this.response = response;
125        }
126    
127        private void writeObject(ObjectOutputStream oos) throws IOException {
128          if (response instanceof ShellResponse.Error) {
129            oos.writeBoolean(false);
130            ShellResponse.Error error = (ShellResponse.Error)response;
131            oos.writeObject(error.getKind());
132            oos.writeObject(error.getMessage());
133            oos.writeObject(error.getThrowable().getMessage());
134            oos.writeObject(error.getThrowable().getStackTrace());
135          } else {
136            oos.writeBoolean(true);
137            oos.writeObject(response);
138          }
139        }
140    
141        private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
142          if (ois.readBoolean()) {
143            response = (ShellResponse)ois.readObject();
144          } else {
145            ErrorKind type = (ErrorKind)ois.readObject();
146            String message = (String)ois.readObject();
147            String errorMessage = (String)ois.readObject();
148            StackTraceElement[] errorTrace = (StackTraceElement[])ois.readObject();
149            Exception ex = new Exception(errorMessage);
150            ex.setStackTrace(errorTrace);
151            response = ShellResponse.error(type, message, ex);
152          }
153        }
154      }
155    }