[77] | 1 | package com.framsticks.communication; |
---|
| 2 | |
---|
| 3 | import com.framsticks.communication.queries.*; |
---|
| 4 | |
---|
| 5 | import java.util.Collection; |
---|
| 6 | import java.util.regex.Matcher; |
---|
| 7 | import java.util.regex.Pattern; |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * Class stores information about query sent to manager. |
---|
| 11 | */ |
---|
| 12 | public abstract class Request { |
---|
| 13 | |
---|
| 14 | public static void quoteValue(StringBuilder builder, String value) { |
---|
| 15 | String quote = ((value.indexOf(' ') > 0) || (value.length() == 0) ? "\"" : ""); |
---|
| 16 | builder.append(quote).append(value).append(quote); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | public abstract String getCommand(); |
---|
| 20 | |
---|
| 21 | protected abstract StringBuilder construct(StringBuilder buffer); |
---|
| 22 | //private static Pattern queryPattern = Pattern.compile("^(\\S+)\\s+(\\S+)(?:\\s+(\\S+))?$"); |
---|
| 23 | |
---|
| 24 | public static Request createRequestByTypeString(String type) { |
---|
| 25 | if (type.equals("get")) { |
---|
| 26 | return new GetRequest(); |
---|
| 27 | } |
---|
| 28 | if (type.equals("set")) { |
---|
| 29 | return new SetRequest(); |
---|
| 30 | } |
---|
| 31 | if (type.equals("info")) { |
---|
| 32 | return new InfoRequest(); |
---|
| 33 | } |
---|
| 34 | if (type.equals("call")) { |
---|
| 35 | return new CallRequest(); |
---|
| 36 | } |
---|
| 37 | if (type.equals("reg")) { |
---|
| 38 | return new RegistrationRequest(); |
---|
| 39 | } |
---|
| 40 | if (type.equals("use")) { |
---|
| 41 | return new UseRequest(); |
---|
| 42 | } |
---|
| 43 | if (type.equals("version")) { |
---|
| 44 | return new VersionRequest(); |
---|
| 45 | } |
---|
| 46 | return null; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public abstract void parseRest(String rest); |
---|
| 50 | |
---|
| 51 | @Override |
---|
| 52 | public String toString() { |
---|
| 53 | return construct(new StringBuilder().append(getCommand())).toString(); |
---|
| 54 | } |
---|
| 55 | } |
---|