View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.tapedeck;
21  
22  import java.nio.charset.Charset;
23  import java.util.LinkedList;
24  
25  import org.apache.mina.core.buffer.IoBuffer;
26  import org.apache.mina.core.filterchain.IoFilter.NextFilter;
27  import org.apache.mina.core.session.IoSession;
28  import org.apache.mina.filter.codec.ProtocolDecoder;
29  import org.apache.mina.filter.codec.ProtocolDecoderOutput;
30  import org.apache.mina.filter.codec.textline.LineDelimiter;
31  import org.apache.mina.filter.codec.textline.TextLineDecoder;
32  
33  /**
34   * MINA {@link ProtocolDecoder} which decodes bytes into {@link Command}
35   * objects.
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   */
39  public class CommandDecoder extends TextLineDecoder {
40      
41      public CommandDecoder() {
42          super(Charset.forName("UTF8"), LineDelimiter.WINDOWS);
43      }
44      
45      private Object parseCommand(String line) throws CommandSyntaxException {
46          String[] temp = line.split(" +", 2);
47          String cmd = temp[0].toLowerCase();
48          String arg = temp.length > 1 ? temp[1] : null;
49          
50          if (LoadCommand.NAME.equals(cmd)) {
51              if (arg == null) {
52                  throw new CommandSyntaxException("No tape number specified");
53              }
54              try {
55                  return new LoadCommand(Integer.parseInt(arg));
56              } catch (NumberFormatException nfe) {
57                  throw new CommandSyntaxException("Illegal tape number: " + arg);
58              }
59          } else if (PlayCommand.NAME.equals(cmd)) {
60              return new PlayCommand();
61          } else if (PauseCommand.NAME.equals(cmd)) {
62              return new PauseCommand();
63          } else if (StopCommand.NAME.equals(cmd)) {
64              return new StopCommand();
65          } else if (ListCommand.NAME.equals(cmd)) {
66              return new ListCommand();
67          } else if (EjectCommand.NAME.equals(cmd)) {
68              return new EjectCommand();
69          } else if (QuitCommand.NAME.equals(cmd)) {
70              return new QuitCommand();
71          } else if (InfoCommand.NAME.equals(cmd)) {
72              return new InfoCommand();
73          } else if (UserCommand.NAME.equals(cmd)) {
74              if (arg == null) {
75                  throw new CommandSyntaxException("No username specified");
76              }
77              return new UserCommand(arg);
78          } else if (PasswordCommand.NAME.equals(cmd)) {
79              if (arg == null) {
80                  throw new CommandSyntaxException("No password specified");
81              }
82              return new PasswordCommand(arg);
83          }
84          
85          throw new CommandSyntaxException("Unknown command: " + cmd);
86      }
87  
88      @Override
89      public void decode(IoSession session, IoBuffer in, final ProtocolDecoderOutput out) 
90              throws Exception {
91          
92          final LinkedList<String> lines = new LinkedList<String>();
93          super.decode(session, in, new ProtocolDecoderOutput() {
94              public void write(Object message) {
95                  lines.add((String) message);
96              }
97              public void flush(NextFilter nextFilter, IoSession session) {}
98          });
99          
100         for (String s: lines) {
101             out.write(parseCommand(s));
102         }
103     }
104 
105 }