| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
package com.thindeck.cockpit.deck; |
| 31 | |
|
| 32 | |
import com.jcabi.xml.XML; |
| 33 | |
import com.thindeck.api.Base; |
| 34 | |
import com.thindeck.api.Deck; |
| 35 | |
import com.thindeck.api.Decks; |
| 36 | |
import com.thindeck.cockpit.RqUser; |
| 37 | |
import java.io.IOException; |
| 38 | |
import java.security.SecureRandom; |
| 39 | |
import java.util.Arrays; |
| 40 | |
import java.util.Date; |
| 41 | |
import java.util.Random; |
| 42 | |
import org.apache.commons.lang3.time.DateFormatUtils; |
| 43 | |
import org.takes.Request; |
| 44 | |
import org.takes.Response; |
| 45 | |
import org.takes.Take; |
| 46 | |
import org.takes.facets.flash.RsFlash; |
| 47 | |
import org.takes.facets.forward.RsForward; |
| 48 | |
import org.takes.rq.RqHref; |
| 49 | |
import org.xembly.Directive; |
| 50 | |
import org.xembly.Directives; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
@SuppressWarnings("PMD.AvoidDuplicateLiterals") |
| 62 | |
public final class TkCommand implements Take { |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | 1 | private static final Random RND = new SecureRandom(); |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
private final transient Base base; |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | 1 | TkCommand(final Base bse) { |
| 79 | 1 | this.base = bse; |
| 80 | 1 | } |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public Response act(final Request req) throws IOException { |
| 84 | 0 | final Decks decks = new RqUser(req, this.base).get().decks(); |
| 85 | 0 | final String deck = new RqDeck(this.base, req).deck().name(); |
| 86 | 0 | final String cmd = new RqHref.Smart(new RqHref.Base(req)) |
| 87 | |
.single("command"); |
| 88 | 0 | final Deck.Smart smart = new Deck.Smart(decks.get(deck)); |
| 89 | 0 | smart.update(TkCommand.answer(smart.xml(), cmd)); |
| 90 | 0 | throw new RsForward(new RsFlash("thanks!")); |
| 91 | |
} |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
private static Iterable<Directive> answer(final XML deck, final String cmd) |
| 101 | |
throws IOException { |
| 102 | 0 | final Directives dirs = new Directives().xpath("/deck"); |
| 103 | 0 | final String[] parts = cmd.trim().split("\\s+"); |
| 104 | 0 | if ("domain".equals(parts[0])) { |
| 105 | 0 | dirs.append( |
| 106 | |
TkCommand.domain( |
| 107 | |
Arrays.copyOfRange(parts, 1, parts.length) |
| 108 | |
) |
| 109 | |
); |
| 110 | 0 | } else if ("repo".equals(parts[0])) { |
| 111 | 0 | dirs.append( |
| 112 | |
TkCommand.repo( |
| 113 | |
deck, |
| 114 | |
Arrays.copyOfRange(parts, 1, parts.length) |
| 115 | |
) |
| 116 | |
); |
| 117 | 0 | } else if ("container".equals(parts[0])) { |
| 118 | 0 | dirs.append( |
| 119 | |
TkCommand.container( |
| 120 | |
Arrays.copyOfRange(parts, 1, parts.length) |
| 121 | |
) |
| 122 | |
); |
| 123 | 0 | } else if ("image".equals(parts[0])) { |
| 124 | 0 | dirs.append( |
| 125 | |
TkCommand.image( |
| 126 | |
Arrays.copyOfRange(parts, 1, parts.length) |
| 127 | |
) |
| 128 | |
); |
| 129 | |
} |
| 130 | 0 | return dirs; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
private static Iterable<Directive> domain(final String... args) |
| 140 | |
throws IOException { |
| 141 | 0 | if (args.length == 0) { |
| 142 | 0 | throw new RsForward( |
| 143 | |
new RsFlash( |
| 144 | |
"'domain' command supports 'add' and 'remove'" |
| 145 | |
) |
| 146 | |
); |
| 147 | |
} |
| 148 | 0 | final Directives dirs = new Directives(); |
| 149 | 0 | if ("add".equals(args[0])) { |
| 150 | 0 | dirs.addIf("domains").add("domain").set(args[1]); |
| 151 | 0 | } else if ("remove".equals(args[0])) { |
| 152 | 0 | dirs.xpath( |
| 153 | |
String.format( |
| 154 | |
"/deck/domains/domain[.='%s']", args[1] |
| 155 | |
) |
| 156 | |
).remove(); |
| 157 | |
} else { |
| 158 | 0 | throw new RsForward( |
| 159 | |
new RsFlash( |
| 160 | |
String.format( |
| 161 | |
"should be either 'add' or 'remove': '%s' is wrong", |
| 162 | |
args[0] |
| 163 | |
) |
| 164 | |
) |
| 165 | |
); |
| 166 | |
} |
| 167 | 0 | return dirs; |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
private static Iterable<Directive> repo(final XML deck, |
| 178 | |
final String... args) throws IOException { |
| 179 | 0 | if (args.length == 0) { |
| 180 | 0 | throw new RsForward( |
| 181 | |
new RsFlash( |
| 182 | |
"'repo' command supports 'put'" |
| 183 | |
) |
| 184 | |
); |
| 185 | |
} |
| 186 | 0 | final Directives dirs = new Directives(); |
| 187 | 0 | if ("put".equals(args[0])) { |
| 188 | 0 | if (deck.nodes("/deck/images/image").size() > 2) { |
| 189 | 0 | throw new IllegalArgumentException( |
| 190 | |
"there are too many images as is, waste a few first" |
| 191 | |
); |
| 192 | |
} |
| 193 | 0 | final String today = DateFormatUtils.ISO_DATETIME_FORMAT.format( |
| 194 | |
new Date() |
| 195 | |
); |
| 196 | 0 | dirs.xpath("/deck").add("repo") |
| 197 | |
.attr("added", today) |
| 198 | |
.add("name") |
| 199 | |
.set(String.format("%08x", TkCommand.RND.nextInt())).up() |
| 200 | |
.add("uri").set(args[1]); |
| 201 | 0 | } else if ("remove".equals(args[0])) { |
| 202 | 0 | dirs.xpath("/deck/repo").remove(); |
| 203 | |
} else { |
| 204 | 0 | throw new IllegalArgumentException( |
| 205 | |
String.format( |
| 206 | |
"should be only 'put': '%s' is wrong", |
| 207 | |
args[0] |
| 208 | |
) |
| 209 | |
); |
| 210 | |
} |
| 211 | 0 | return dirs; |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
private static Iterable<Directive> container(final String... args) |
| 221 | |
throws IOException { |
| 222 | 0 | if (args.length == 0) { |
| 223 | 0 | throw new RsForward( |
| 224 | |
new RsFlash( |
| 225 | |
"'container' command supports 'waste'" |
| 226 | |
) |
| 227 | |
); |
| 228 | |
} |
| 229 | 0 | final Directives dirs = new Directives(); |
| 230 | 0 | if ("waste".equals(args[0])) { |
| 231 | 0 | final String today = DateFormatUtils.ISO_DATETIME_FORMAT.format( |
| 232 | |
new Date() |
| 233 | |
); |
| 234 | 0 | dirs.xpath( |
| 235 | |
String.format( |
| 236 | |
"/deck/containers/container[name='%s']", |
| 237 | |
args[1] |
| 238 | |
) |
| 239 | |
).attr("waste", today); |
| 240 | 0 | } else { |
| 241 | 0 | throw new IllegalArgumentException( |
| 242 | |
String.format( |
| 243 | |
"should be only 'waste': '%s' is wrong", |
| 244 | |
args[0] |
| 245 | |
) |
| 246 | |
); |
| 247 | |
} |
| 248 | 0 | return dirs; |
| 249 | |
} |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
private static Iterable<Directive> image(final String... args) |
| 258 | |
throws IOException { |
| 259 | 0 | if (args.length == 0) { |
| 260 | 0 | throw new RsForward( |
| 261 | |
new RsFlash( |
| 262 | |
"'image' command supports 'waste'" |
| 263 | |
) |
| 264 | |
); |
| 265 | |
} |
| 266 | 0 | final Directives dirs = new Directives(); |
| 267 | 0 | if ("waste".equals(args[0])) { |
| 268 | 0 | final String today = DateFormatUtils.ISO_DATETIME_FORMAT.format( |
| 269 | |
new Date() |
| 270 | |
); |
| 271 | 0 | dirs.xpath( |
| 272 | |
String.format( |
| 273 | |
"/deck/images/image[name='%s']", |
| 274 | |
args[1] |
| 275 | |
) |
| 276 | |
).attr("waste", today); |
| 277 | 0 | } else { |
| 278 | 0 | throw new IllegalArgumentException( |
| 279 | |
String.format( |
| 280 | |
"should be only 'waste': '%s' is wrong argument", |
| 281 | |
args[0] |
| 282 | |
) |
| 283 | |
); |
| 284 | |
} |
| 285 | 0 | return dirs; |
| 286 | |
} |
| 287 | |
|
| 288 | |
} |