| 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; |
| 36 | |
import com.jcabi.http.response.RestResponse; |
| 37 | |
import com.jcabi.http.response.XmlResponse; |
| 38 | |
import com.jcabi.log.Logger; |
| 39 | |
import java.io.IOException; |
| 40 | |
import java.net.HttpURLConnection; |
| 41 | |
import lombok.EqualsAndHashCode; |
| 42 | |
import lombok.ToString; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
@Immutable |
| 53 | |
@Loggable(Loggable.DEBUG) |
| 54 | 0 | @ToString(of = "lck", includeFieldNames = false) |
| 55 | 0 | @EqualsAndHashCode(of = { "lck", "request" }) |
| 56 | |
final class RtLock implements Lock { |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private final transient String lck; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private final transient Request request; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 2 | RtLock(final String name, final Request req) { |
| 74 | 2 | this.lck = name; |
| 75 | 2 | this.request = req; |
| 76 | 2 | } |
| 77 | |
|
| 78 | |
@Override |
| 79 | |
public String name() { |
| 80 | 0 | return this.lck; |
| 81 | |
} |
| 82 | |
|
| 83 | |
@Override |
| 84 | |
public String label() throws IOException { |
| 85 | 0 | final long start = System.currentTimeMillis(); |
| 86 | 0 | final String label = this.front("label") |
| 87 | |
.uri().queryParam("name", this.lck).back() |
| 88 | |
.method(Request.GET) |
| 89 | |
.fetch() |
| 90 | |
.body(); |
| 91 | 0 | Logger.info( |
| 92 | |
this, "label of \"%s\" retrieved in %[ms]s: \"%s\"", |
| 93 | |
this.lck, |
| 94 | |
System.currentTimeMillis() - start, |
| 95 | |
label |
| 96 | |
); |
| 97 | 0 | return label; |
| 98 | |
} |
| 99 | |
|
| 100 | |
@Override |
| 101 | |
public boolean lock(final String label) throws IOException { |
| 102 | 2 | final long start = System.currentTimeMillis(); |
| 103 | 1 | final Response rsp = this.front("lock") |
| 104 | |
.body().formParam("label", label) |
| 105 | |
.formParam("name", this.lck).back() |
| 106 | |
.method(Request.POST) |
| 107 | |
.fetch(); |
| 108 | 0 | final boolean locked = rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; |
| 109 | 0 | Logger.info( |
| 110 | |
this, "lock of \"%s\" is %B in %[ms]s: %s", |
| 111 | |
this.lck, locked, |
| 112 | |
System.currentTimeMillis() - start, |
| 113 | |
rsp.body() |
| 114 | |
); |
| 115 | 0 | return locked; |
| 116 | |
} |
| 117 | |
|
| 118 | |
@Override |
| 119 | |
public boolean unlock(final String label) throws IOException { |
| 120 | 2 | final long start = System.currentTimeMillis(); |
| 121 | 1 | final Response rsp = this.front("unlock") |
| 122 | |
.uri().queryParam("label", label) |
| 123 | |
.queryParam("name", this.lck).back() |
| 124 | |
.method(Request.GET) |
| 125 | |
.fetch(); |
| 126 | 0 | final boolean unlocked = |
| 127 | |
rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; |
| 128 | 0 | Logger.info( |
| 129 | |
this, "unlock of \"%s\" is %B in %[ms]s", |
| 130 | |
this.lck, unlocked, |
| 131 | |
System.currentTimeMillis() - start |
| 132 | |
); |
| 133 | 0 | return unlocked; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
private Request front(final String label) throws IOException { |
| 143 | 2 | return this.request |
| 144 | |
.fetch() |
| 145 | |
.as(RestResponse.class) |
| 146 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
| 147 | |
.as(XmlResponse.class) |
| 148 | |
.rel(String.format("/page/links/link[@rel='%s']/@href", label)); |
| 149 | |
} |
| 150 | |
|
| 151 | |
} |