1 /** 2 * Copyright (c) 2014, 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.rest; 31 32 import co.stateful.spi.Counter; 33 import java.io.IOException; 34 import java.math.BigDecimal; 35 import java.net.HttpURLConnection; 36 import javax.ws.rs.GET; 37 import javax.ws.rs.PUT; 38 import javax.ws.rs.Path; 39 import javax.ws.rs.PathParam; 40 import javax.ws.rs.Produces; 41 import javax.ws.rs.QueryParam; 42 import javax.ws.rs.WebApplicationException; 43 import javax.ws.rs.core.MediaType; 44 import javax.ws.rs.core.Response; 45 46 /** 47 * Counter of a user. 48 * 49 * @author Yegor Bugayenko (yegor@tpc2.com) 50 * @version $Id$ 51 */ 52 @Path("/c/{cnt}") 53 public final class CounterRs extends BaseRs { 54 55 /** 56 * The counter we're working with. 57 */ 58 private transient Counter counter; 59 60 /** 61 * Set counter. 62 * @param name Counter name 63 */ 64 @PathParam("cnt") 65 public void setCounter(final String name) { 66 this.counter = this.user().counters().get(name); 67 } 68 69 /** 70 * Set counter. 71 * @param value Value to set 72 * @return The JAX-RS response 73 * @throws IOException If fails due to IO problem 74 */ 75 @PUT 76 @Path("/set") 77 public Response set(@QueryParam("value") final String value) 78 throws IOException { 79 this.counter.set(this.decimal(value)); 80 return Response.ok().build(); 81 } 82 83 /** 84 * Add counter. 85 * @param value Value to add 86 * @return The JAX-RS response 87 * @throws IOException If fails due to IO problem 88 */ 89 @GET 90 @Path("/inc") 91 @Produces(MediaType.TEXT_PLAIN) 92 public Response increment(@QueryParam("value") final String value) 93 throws IOException { 94 return Response.ok() 95 .entity(this.counter.increment(this.decimal(value)).toString()) 96 .build(); 97 } 98 99 /** 100 * Convert string to decimal. 101 * @param text Text to convert 102 * @return Decimal 103 */ 104 private BigDecimal decimal(final String text) { 105 if (!text.matches("-?[0-9]{1,32}")) { 106 throw new WebApplicationException( 107 Response.status(HttpURLConnection.HTTP_BAD_REQUEST) 108 .entity("1-32 integer allowed as a value") 109 .build() 110 ); 111 } 112 return new BigDecimal(text); 113 } 114 115 }