3232import com .jcabi .aspects .Immutable ;
3333import com .jcabi .aspects .Loggable ;
3434import com .jcabi .http .Request ;
35+ import com .jcabi .http .response .RestResponse ;
36+ import com .jcabi .http .response .XmlResponse ;
3537import com .jcabi .log .Logger ;
3638import com .jcabi .manifests .Manifests ;
3739import java .io .IOException ;
5254@ Immutable
5355@ Loggable (Loggable .DEBUG )
5456@ ToString (of = "lck" , includeFieldNames = false )
55- @ EqualsAndHashCode (of = { "lck" , "lrequest" , "urequest " })
57+ @ EqualsAndHashCode (of = { "lck" , "request " })
5658final class RtLock implements Lock {
5759
5860 /**
@@ -61,25 +63,18 @@ final class RtLock implements Lock {
6163 private final transient String lck ;
6264
6365 /**
64- * Lock request .
66+ * Locks home .
6567 */
66- private final transient Request lrequest ;
67-
68- /**
69- * Unlock request.
70- */
71- private final transient Request urequest ;
68+ private final transient Request request ;
7269
7370 /**
7471 * Ctor.
7572 * @param name Name of it
76- * @param lreq Lock request
77- * @param ureq Unlock request
73+ * @param req Lock request
7874 */
79- RtLock (final String name , final Request lreq , final Request ureq ) {
75+ RtLock (final String name , final Request req ) {
8076 this .lck = name ;
81- this .lrequest = lreq ;
82- this .urequest = ureq ;
77+ this .request = req ;
8378 }
8479
8580 @ Override
@@ -110,7 +105,7 @@ public void unlock() throws IOException {
110105 @ Override
111106 public boolean lock (final String label ) throws IOException {
112107 final long start = System .currentTimeMillis ();
113- final boolean locked = this .lrequest
108+ final boolean locked = this .front ( "lock" )
114109 .body ().formParam ("label" , label ).back ()
115110 .fetch ()
116111 .status () == HttpURLConnection .HTTP_SEE_OTHER ;
@@ -125,7 +120,7 @@ public boolean lock(final String label) throws IOException {
125120 @ Override
126121 public boolean unlock (final String label ) throws IOException {
127122 final long start = System .currentTimeMillis ();
128- final boolean unlocked = this .urequest
123+ final boolean unlocked = this .front ( "unlock" )
129124 .uri ().queryParam ("label" , label ).back ()
130125 .fetch ()
131126 .status () == HttpURLConnection .HTTP_SEE_OTHER ;
@@ -136,4 +131,21 @@ public boolean unlock(final String label) throws IOException {
136131 return unlocked ;
137132 }
138133
134+ /**
135+ * Get front request.
136+ * @param label Label
137+ * @return Request
138+ * @throws IOException If fails
139+ */
140+ private Request front (final String label ) throws IOException {
141+ return this .request
142+ .fetch ()
143+ .as (RestResponse .class )
144+ .assertStatus (HttpURLConnection .HTTP_OK )
145+ .as (XmlResponse .class )
146+ .rel (String .format ("/page/links/link[@rel='%s']/@href" , label ))
147+ .method (Request .GET )
148+ .uri ().queryParam ("name" , this .lck ).back ();
149+ }
150+
139151}
0 commit comments