| 1 | 0 | |
| 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 co.stateful; |
| 31 | |
|
| 32 | |
import com.jcabi.aspects.Immutable; |
| 33 | |
import com.jcabi.aspects.Loggable; |
| 34 | |
import com.jcabi.http.Request; |
| 35 | |
import com.jcabi.http.response.RestResponse; |
| 36 | |
import com.jcabi.http.response.XmlResponse; |
| 37 | |
import com.jcabi.log.Logger; |
| 38 | |
import java.io.IOException; |
| 39 | |
import java.net.HttpURLConnection; |
| 40 | |
import lombok.EqualsAndHashCode; |
| 41 | |
import lombok.ToString; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
@Immutable |
| 52 | |
@Loggable(Loggable.DEBUG) |
| 53 | 0 | @ToString(of = { }) |
| 54 | 0 | @EqualsAndHashCode(of = "request") |
| 55 | |
final class RtCounters implements Counters { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
private final transient Request request; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | 0 | RtCounters(final Request req) { |
| 67 | 0 | this.request = req; |
| 68 | 0 | } |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public Iterable<String> names() throws IOException { |
| 72 | 0 | return this.request.fetch() |
| 73 | |
.as(RestResponse.class) |
| 74 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
| 75 | |
.as(XmlResponse.class) |
| 76 | |
.xml() |
| 77 | |
.xpath("/page/counters/counter/name/text()"); |
| 78 | |
} |
| 79 | |
|
| 80 | |
@Override |
| 81 | |
public Counter create(final String name) throws IOException { |
| 82 | 0 | final long start = System.currentTimeMillis(); |
| 83 | 0 | this.request.fetch() |
| 84 | |
.as(RestResponse.class) |
| 85 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
| 86 | |
.as(XmlResponse.class) |
| 87 | |
.rel("/page/links/link[@rel='add']/@href") |
| 88 | |
.method(Request.POST) |
| 89 | |
.body().formParam("name", name).back() |
| 90 | |
.fetch() |
| 91 | |
.as(RestResponse.class) |
| 92 | |
.assertStatus(HttpURLConnection.HTTP_SEE_OTHER); |
| 93 | 0 | Logger.info( |
| 94 | |
this, "counter \"%s\" created in %[ms]s", |
| 95 | |
name, System.currentTimeMillis() - start |
| 96 | |
); |
| 97 | 0 | return this.get(name); |
| 98 | |
} |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public void delete(final String name) throws IOException { |
| 102 | 0 | final long start = System.currentTimeMillis(); |
| 103 | 0 | this.request.fetch() |
| 104 | |
.as(RestResponse.class) |
| 105 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
| 106 | |
.as(XmlResponse.class) |
| 107 | |
.rel( |
| 108 | |
String.format( |
| 109 | |
|
| 110 | |
"/page/counters/counter[name='%s']/links/link[@rel='delete']/@href", |
| 111 | |
name |
| 112 | |
) |
| 113 | |
) |
| 114 | |
.uri().queryParam("name", name).back() |
| 115 | |
.fetch() |
| 116 | |
.as(RestResponse.class) |
| 117 | |
.assertStatus(HttpURLConnection.HTTP_SEE_OTHER); |
| 118 | 0 | Logger.info( |
| 119 | |
this, "counter \"%s\" deleted in %[ms]s", |
| 120 | |
name, System.currentTimeMillis() - start |
| 121 | |
); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
@Override |
| 125 | |
public Counter get(final String name) throws IOException { |
| 126 | 0 | return new RtCounter(name, this.request); |
| 127 | |
} |
| 128 | |
} |