3333import com .jcabi .aspects .Loggable ;
3434import com .jcabi .http .Request ;
3535import com .jcabi .http .response .RestResponse ;
36+ import com .jcabi .http .response .XmlResponse ;
3637import com .jcabi .log .Logger ;
3738import java .io .IOException ;
3839import java .net .HttpURLConnection ;
5253@ Immutable
5354@ Loggable (Loggable .DEBUG )
5455@ ToString (of = "label" , includeFieldNames = false )
55- @ EqualsAndHashCode (of = { "srequest " , "irequest " })
56+ @ EqualsAndHashCode (of = { "label " , "request " })
5657final class RtCounter implements Counter {
5758
5859 /**
@@ -61,25 +62,18 @@ final class RtCounter implements Counter {
6162 private final transient String label ;
6263
6364 /**
64- * Set request.
65+ * Home request.
6566 */
66- private final transient Request srequest ;
67-
68- /**
69- * Increment request.
70- */
71- private final transient Request irequest ;
67+ private final transient Request request ;
7268
7369 /**
7470 * Ctor.
7571 * @param name Name of it
76- * @param sreq SET request
77- * @param ireq INC request
72+ * @param req Home page request
7873 */
79- RtCounter (final String name , final Request sreq , final Request ireq ) {
74+ RtCounter (final String name , final Request req ) {
8075 this .label = name ;
81- this .srequest = sreq ;
82- this .irequest = ireq ;
76+ this .request = req ;
8377 }
8478
8579 @ Override
@@ -90,7 +84,7 @@ public String name() {
9084 @ Override
9185 public void set (final long value ) throws IOException {
9286 final long start = System .currentTimeMillis ();
93- this .srequest .method (Request .PUT )
87+ this .front ( "set" ) .method (Request .PUT )
9488 .uri ().queryParam ("value" , value ).back ()
9589 .fetch ()
9690 .as (RestResponse .class )
@@ -106,7 +100,7 @@ public void set(final long value) throws IOException {
106100 public long incrementAndGet (final long delta ) throws IOException {
107101 final long start = System .currentTimeMillis ();
108102 final long value = Long .parseLong (
109- this .irequest .method (Request .GET )
103+ this .front ( "increment" ) .method (Request .GET )
110104 .uri ().queryParam ("value" , delta ).back ()
111105 .header (HttpHeaders .ACCEPT , MediaType .TEXT_PLAIN )
112106 .fetch ()
@@ -121,4 +115,25 @@ public long incrementAndGet(final long delta) throws IOException {
121115 );
122116 return value ;
123117 }
118+
119+ /**
120+ * Get front request.
121+ * @param ops Operation
122+ * @return Request
123+ * @throws IOException If fails
124+ */
125+ private Request front (final String ops ) throws IOException {
126+ return this .request .fetch ()
127+ .as (RestResponse .class )
128+ .assertStatus (HttpURLConnection .HTTP_OK )
129+ .as (XmlResponse .class )
130+ .rel (
131+ String .format (
132+ // @checkstyle LineLength (1 line)
133+ "/page/counters/counter[name='%s']/links/link[@rel='%s']/@href" ,
134+ this .label , ops
135+ )
136+ );
137+ }
138+
124139}
0 commit comments