Coverage Report - com.thindeck.cockpit.deck.FkDeck
 
Classes in this File Line Coverage Branch Coverage Complexity
FkDeck
40%
2/5
N/A
1.833
FkDeck$1
50%
1/2
N/A
1.833
FkDeck$2
0%
0/3
N/A
1.833
FkDeck$3
0%
0/6
0%
0/2
1.833
 
 1  
 /**
 2  
  * Copyright (c) 2014-2015, Thindeck.com
 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 thindeck.com 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 com.thindeck.cockpit.deck;
 31  
 
 32  
 import java.io.IOException;
 33  
 import java.net.HttpURLConnection;
 34  
 import org.takes.Request;
 35  
 import org.takes.Response;
 36  
 import org.takes.Take;
 37  
 import org.takes.facets.fork.FkRegex;
 38  
 import org.takes.facets.fork.FkWrap;
 39  
 import org.takes.facets.fork.Fork;
 40  
 import org.takes.facets.fork.RqRegex;
 41  
 import org.takes.facets.fork.TkRegex;
 42  
 import org.takes.facets.forward.RsForward;
 43  
 import org.takes.misc.Opt;
 44  
 import org.takes.rq.RqHref;
 45  
 import org.takes.rq.RqWithHeader;
 46  
 
 47  
 /**
 48  
  * Deck fork.
 49  
  *
 50  
  * @author Yegor Bugayenko (yegor@teamed.io)
 51  
  * @version $Id$
 52  
  * @since 0.5
 53  
  * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 54  
  */
 55  0
 final class FkDeck extends FkWrap {
 56  
 
 57  
     /**
 58  
      * Ctor.
 59  
      * @param regex Regular expression
 60  
      * @param take Take
 61  
      */
 62  
     FkDeck(final String regex, final Take take) {
 63  4
         super(
 64  4
             new Fork() {
 65  
                 @Override
 66  
                 public Opt<Response> route(final Request req)
 67  
                     throws IOException {
 68  0
                     return FkDeck.route(regex, take, req);
 69  
                 }
 70  
             }
 71  
         );
 72  4
     }
 73  
 
 74  
     /**
 75  
      * Route.
 76  
      * @param regex Regular expression
 77  
      * @param take Take
 78  
      * @param req Request
 79  
      * @return Response or empty
 80  
      * @throws IOException If fails
 81  
      */
 82  
     private static Opt<Response> route(final String regex, final Take take,
 83  
         final Request req) throws IOException {
 84  0
         return new FkRegex(
 85  
             String.format("/d/([a-z\\-]+)%s", regex),
 86  0
             new TkRegex() {
 87  
                 @Override
 88  
                 public Response act(final RqRegex rreq) throws IOException {
 89  0
                     final String name = rreq.matcher().group(1);
 90  0
                     return FkDeck.redirect(name, take).act(
 91  
                         new RqWithHeader(rreq, "X-Thindeck-Deck", name)
 92  
                     );
 93  
                 }
 94  
             }
 95  
         ).route(req);
 96  
     }
 97  
 
 98  
     /**
 99  
      * Redirect to the bout.
 100  
      * @param deck Deck name
 101  
      * @param take Take
 102  
      * @return New take
 103  
      */
 104  
     private static Take redirect(final String deck, final Take take) {
 105  0
         return new Take() {
 106  
             @Override
 107  
             public Response act(final Request req) throws IOException {
 108  
                 try {
 109  0
                     return take.act(req);
 110  0
                 } catch (final RsForward ex) {
 111  0
                     if (ex.code() == HttpURLConnection.HTTP_SEE_OTHER) {
 112  0
                         throw new RsForward(
 113  
                             ex,
 114  
                             new RqHref.Smart(
 115  
                                 new RqHref.Base(req)
 116  
                             ).home().path("d").path(deck)
 117  
                         );
 118  
                     }
 119  0
                     throw ex;
 120  
                 }
 121  
             }
 122  
         };
 123  
     }
 124  
 
 125  
 }