Coverage Report - co.stateful.RtLock
 
Classes in this File Line Coverage Branch Coverage Complexity
RtLock
43%
10/23
0%
0/24
1
RtLock$AjcClosure1
0%
0/1
N/A
1
RtLock$AjcClosure3
0%
0/1
N/A
1
RtLock$AjcClosure5
100%
1/1
N/A
1
RtLock$AjcClosure7
100%
1/1
N/A
1
 
 1  0
 /**
 2  
  * Copyright (c) 2014-2016, stateful.co
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the stateful.co nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 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  
  * Lock.
 46  
  *
 47  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 48  
  * @version $Id$
 49  
  * @since 0.3
 50  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 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  
      * Its name.
 60  
      */
 61  
     private final transient String lck;
 62  
 
 63  
     /**
 64  
      * Locks home.
 65  
      */
 66  
     private final transient Request request;
 67  
 
 68  
     /**
 69  
      * Ctor.
 70  
      * @param name Name of it
 71  
      * @param req Lock request
 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  
      * Get front request.
 138  
      * @param label Label
 139  
      * @return Request
 140  
      * @throws IOException If fails
 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  
 }