Coverage Report - co.stateful.RtSttc
 
Classes in this File Line Coverage Branch Coverage Complexity
RtSttc
0%
0/12
0%
0/14
1.25
RtSttc$AjcClosure1
0%
0/1
N/A
1.25
RtSttc$AjcClosure3
0%
0/1
N/A
1.25
RtSttc$AjcClosure5
0%
0/1
N/A
1.25
 
 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 co.stateful.mock.MkSttc;
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import com.jcabi.http.Request;
 36  
 import com.jcabi.http.request.JdkRequest;
 37  
 import com.jcabi.http.response.RestResponse;
 38  
 import com.jcabi.http.response.XmlResponse;
 39  
 import com.jcabi.http.wire.CachingWire;
 40  
 import com.jcabi.http.wire.OneMinuteWire;
 41  
 import com.jcabi.http.wire.RetryWire;
 42  
 import com.jcabi.http.wire.VerboseWire;
 43  
 import com.jcabi.urn.URN;
 44  
 import java.io.IOException;
 45  
 import java.net.HttpURLConnection;
 46  
 import javax.ws.rs.core.HttpHeaders;
 47  
 import javax.ws.rs.core.MediaType;
 48  
 import lombok.EqualsAndHashCode;
 49  
 import lombok.ToString;
 50  
 
 51  
 /**
 52  
  * Stateful Entry Point.
 53  
  *
 54  
  * <p>Make an instance of this class and start from there, for example,
 55  
  * if you want to use a cloud lock from
 56  
  * <a href="http://www.stateful.co">stateful.co</a>:
 57  
  *
 58  
  * <pre> Lock lock = new RtSttc(
 59  
  *   new URN("urn:github:12345"), "token"
 60  
  * ).locks().lock("test");</pre>
 61  
  *
 62  
  * <p>You need two arguments to instantiate this class: URN of the user
 63  
  * and your secret token. You can get them at the home page of
 64  
  * <a href="http://www.stateful.co">stateful.co</a>, right on the top.
 65  
  *
 66  
  * <p>It is highly recommended to use
 67  
  * {@link co.stateful.retry.ReSttc} decorator, in production
 68  
  * environment.</p>
 69  
  *
 70  
  * <p>It is also highly recommended to use
 71  
  * {@link co.stateful.cached.CdSttc} decorator, in production
 72  
  * environment.</p>
 73  
  *
 74  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 75  
  * @version $Id$
 76  
  * @since 0.1
 77  
  * @link <a href="http://www.yegor256.com/2014/05/18/cloud-autoincrement-counters.html">Atomic Counters at Stateful.co</a>
 78  
  * @link <a href="http://www.yegor256.com/2014/12/04/synchronization-between-nodes.html">Synchronization Between Nodes</a>
 79  
  */
 80  
 @Immutable
 81  
 @Loggable(Loggable.DEBUG)
 82  0
 @ToString(of = { })
 83  0
 @EqualsAndHashCode(of = "request")
 84  
 public final class RtSttc implements Sttc {
 85  
 
 86  
     /**
 87  
      * Entry request.
 88  
      */
 89  
     private final transient Request request;
 90  
 
 91  
     /**
 92  
      * Ctor.
 93  
      * @param urn Owner URN
 94  
      * @param token Security token
 95  
      */
 96  0
     public RtSttc(final URN urn, final String token) {
 97  0
         this.request = new JdkRequest("http://www.stateful.co")
 98  
             .through(OneMinuteWire.class)
 99  
             .through(RetryWire.class)
 100  
             .through(VerboseWire.class)
 101  
             .through(CachingWire.class, "((POST|PUT|PATCH) .*|.*\\?.*)")
 102  
             .header("X-Sttc-URN", urn.toString())
 103  
             .header("X-Sttc-Token", token)
 104  
             .header(HttpHeaders.ACCEPT, MediaType.TEXT_XML)
 105  
             .header(HttpHeaders.USER_AGENT, "java-sdk.stateful.co");
 106  0
     }
 107  
 
 108  
     /**
 109  
      * Make an instance of it.
 110  
      * @param urn Owner URN
 111  
      * @param token Security token
 112  
      * @return Sttc
 113  
      */
 114  
     public static Sttc make(final URN urn, final String token) {
 115  
         final Sttc sttc;
 116  0
         if (token.matches("[A-F0-9\\-]{19}")) {
 117  0
             sttc = new RtSttc(urn, token);
 118  
         } else {
 119  0
             sttc = new MkSttc();
 120  
         }
 121  0
         return sttc;
 122  
     }
 123  
 
 124  
     @Override
 125  
     public Counters counters() throws IOException {
 126  0
         return new RtCounters(
 127  
             this.request
 128  
                 .fetch()
 129  
                 .as(RestResponse.class)
 130  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 131  
                 .as(XmlResponse.class)
 132  
                 .rel("/page/links/link[@rel='menu:counters']/@href")
 133  
         );
 134  
     }
 135  
 
 136  
     @Override
 137  
     public Locks locks() throws IOException {
 138  0
         return new RtLocks(
 139  
             this.request
 140  
                 .fetch()
 141  
                 .as(RestResponse.class)
 142  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 143  
                 .as(XmlResponse.class)
 144  
                 .rel("/page/links/link[@rel='menu:locks']/@href")
 145  
         );
 146  
     }
 147  
 }