| 1 | package be.nikiroo.jvcard.remote; |
| 2 | |
| 3 | public class Command { |
| 4 | public enum Verb { |
| 5 | /** VERSION of the protocol */ |
| 6 | VERSION, |
| 7 | /** TIME of the remote server in milliseconds since the Unix epoch */ |
| 8 | TIME, |
| 9 | /** STOP the communication (client stops) */ |
| 10 | STOP, |
| 11 | /** |
| 12 | * LIST all the contacts on the remote server that contain the search |
| 13 | * term, or all contacts if no search term given |
| 14 | */ |
| 15 | LIST, |
| 16 | /** HELP about the protocol for interactive access */ |
| 17 | HELP, |
| 18 | /** GET a remote card */ |
| 19 | GET_CARD, |
| 20 | /** |
| 21 | * PUT mode activation toggle for a card on the remote server (you can |
| 22 | * issue *_CONTACT commands when in PUT mode) |
| 23 | */ |
| 24 | PUT_CARD, |
| 25 | /** POST a new card to the remote server */ |
| 26 | POST_CARD, |
| 27 | /** DELETE an existing contact from the remote server */ |
| 28 | DELETE_CARD, |
| 29 | /** GET a remote contact */ |
| 30 | GET_CONTACT, |
| 31 | /** |
| 32 | * PUT mode activation toggle for a contact on the remote server (you |
| 33 | * can issue *_DATA commands when in PUT mode) |
| 34 | */ |
| 35 | PUT_CONTACT, |
| 36 | /** POST a new contact to the remote server */ |
| 37 | POST_CONTACT, |
| 38 | /** DELETE an existing contact from the remote server */ |
| 39 | DELETE_CONTACT, |
| 40 | /** GET a remote data */ |
| 41 | GET_DATA, |
| 42 | /** POST a new data to the remote server */ |
| 43 | POST_DATA, |
| 44 | /** DELETE an existing data from the remote server */ |
| 45 | DELETE_DATA, |
| 46 | } |
| 47 | |
| 48 | private Verb verb; |
| 49 | private int version; |
| 50 | private String param; |
| 51 | |
| 52 | /** |
| 53 | * Create a new, empty {@link Command} with the given {@link Verb} and |
| 54 | * version. |
| 55 | * |
| 56 | * @param verb |
| 57 | * the {@link Verb} |
| 58 | * @param version |
| 59 | * the version |
| 60 | */ |
| 61 | public Command(Verb verb, int version) { |
| 62 | this(verb, null, version); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Create a new, empty {@link Command} with the given {@link Verb} and |
| 67 | * version. |
| 68 | * |
| 69 | * @param verb |
| 70 | * the {@link Verb} |
| 71 | * @param version |
| 72 | * the version |
| 73 | */ |
| 74 | public Command(Verb verb, String param, int version) { |
| 75 | this.verb = verb; |
| 76 | this.version = version; |
| 77 | this.param = param; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Read a command line (starting with a {@link Verb}) and process its |
| 82 | * content here in a more readable format. |
| 83 | * |
| 84 | * @param input |
| 85 | * the command line |
| 86 | * @param version |
| 87 | * the version (which can be overrided by a {@link Verb#VERSION} |
| 88 | * command) |
| 89 | */ |
| 90 | public Command(String input, int version) { |
| 91 | this.version = version; |
| 92 | |
| 93 | if (input != null) { |
| 94 | String v = input; |
| 95 | int indexSp = input.indexOf(" "); |
| 96 | if (indexSp >= 0) { |
| 97 | v = input.substring(0, indexSp); |
| 98 | } |
| 99 | |
| 100 | for (Verb verb : Verb.values()) { |
| 101 | if (v.equals(verb.name())) { |
| 102 | this.verb = verb; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (verb != null) { |
| 107 | String param = null; |
| 108 | if (indexSp >= 0) |
| 109 | param = input.substring(indexSp + 1); |
| 110 | |
| 111 | this.param = param; |
| 112 | |
| 113 | if (verb == Verb.VERSION) { |
| 114 | try { |
| 115 | version = Integer.parseInt(param); |
| 116 | } catch (NumberFormatException e) { |
| 117 | e.printStackTrace(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Return the version |
| 126 | * |
| 127 | * @return the version |
| 128 | */ |
| 129 | public int getVersion() { |
| 130 | return version; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Return the {@link Verb} |
| 135 | * |
| 136 | * @return the {@link Verb} |
| 137 | */ |
| 138 | public Verb getVerb() { |
| 139 | return verb; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Return the parameter of this {@link Command} if any. |
| 144 | * |
| 145 | * @return the parameter or NULL |
| 146 | */ |
| 147 | public String getParam() { |
| 148 | return param; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String toString() { |
| 153 | if (verb == null) |
| 154 | return "[null command]"; |
| 155 | |
| 156 | switch (verb) { |
| 157 | case VERSION: |
| 158 | return verb.name() + " " + version; |
| 159 | default: |
| 160 | return verb.name() + (param == null ? "" : " " + param); |
| 161 | } |
| 162 | } |
| 163 | } |