Coverage Report - com.thindeck.cockpit.TkApp
 
Classes in this File Line Coverage Branch Coverage Complexity
TkApp
87%
7/8
50%
1/2
1.4
TkApp$1
33%
1/3
N/A
1.4
 
 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;
 31  
 
 32  
 import com.jcabi.log.VerboseProcess;
 33  
 import com.jcabi.manifests.Manifests;
 34  
 import com.thindeck.api.Base;
 35  
 import com.thindeck.cockpit.deck.TkDeck;
 36  
 import java.io.File;
 37  
 import java.io.IOException;
 38  
 import java.nio.charset.Charset;
 39  
 import java.util.regex.Pattern;
 40  
 import org.takes.Take;
 41  
 import org.takes.facets.auth.PsByFlag;
 42  
 import org.takes.facets.flash.TkFlash;
 43  
 import org.takes.facets.fork.FkAnonymous;
 44  
 import org.takes.facets.fork.FkAuthenticated;
 45  
 import org.takes.facets.fork.FkFixed;
 46  
 import org.takes.facets.fork.FkHitRefresh;
 47  
 import org.takes.facets.fork.FkParams;
 48  
 import org.takes.facets.fork.FkRegex;
 49  
 import org.takes.facets.fork.TkFork;
 50  
 import org.takes.facets.forward.TkForward;
 51  
 import org.takes.tk.TkClasspath;
 52  
 import org.takes.tk.TkFiles;
 53  
 import org.takes.tk.TkMeasured;
 54  
 import org.takes.tk.TkRedirect;
 55  
 import org.takes.tk.TkVersioned;
 56  
 import org.takes.tk.TkWithHeaders;
 57  
 import org.takes.tk.TkWithType;
 58  
 import org.takes.tk.TkWrap;
 59  
 
 60  
 /**
 61  
  * App.
 62  
  *
 63  
  * @author Yegor Bugayenko (yegor@teamed.io)
 64  
  * @version $Id$
 65  
  * @since 0.5
 66  
  * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
 67  
  * @checkstyle ClassFanOutComplexityCheck (500 lines)
 68  
  * @checkstyle MultipleStringLiteralsCheck (500 lines)
 69  
  */
 70  
 public final class TkApp extends TkWrap {
 71  
 
 72  
     /**
 73  
      * Version of the system, to show in header.
 74  
      */
 75  1
     private static final String VERSION = String.format(
 76  
         "%s %s %s",
 77  
         // @checkstyle MultipleStringLiterals (3 lines)
 78  
         Manifests.read("Thindeck-Version"),
 79  
         Manifests.read("Thindeck-Revision"),
 80  
         Manifests.read("Thindeck-Date")
 81  
     );
 82  
 
 83  
     /**
 84  
      * Ctor.
 85  
      * @param base Base
 86  
      * @throws IOException If fails
 87  
      */
 88  
     public TkApp(final Base base) throws IOException {
 89  1
         super(TkApp.make(base));
 90  1
     }
 91  
 
 92  
     /**
 93  
      * Ctor.
 94  
      * @param base Base
 95  
      * @return Take
 96  
      * @throws IOException If fails
 97  
      */
 98  
     private static Take make(final Base base) throws IOException {
 99  1
         if (!"UTF-8".equals(Charset.defaultCharset().name())) {
 100  0
             throw new IllegalStateException(
 101  
                 String.format(
 102  
                     "default encoding is %s", Charset.defaultCharset()
 103  
                 )
 104  
             );
 105  
         }
 106  1
         return new TkWithHeaders(
 107  
             new TkVersioned(
 108  
                 new TkMeasured(
 109  
                     new TkFlash(
 110  
                         new TkAppFallback(
 111  
                             new TkForward(
 112  
                                 new TkAppAuth(
 113  
                                     TkApp.regex(base)
 114  
                                 )
 115  
                             )
 116  
                         )
 117  
                     )
 118  
                 )
 119  
             ),
 120  
             String.format("X-Thindeck-Version: %s", TkApp.VERSION),
 121  
             "Vary: Cookie"
 122  
         );
 123  
     }
 124  
 
 125  
     /**
 126  
      * Regex takes.
 127  
      * @param base Base
 128  
      * @return Take
 129  
      * @throws IOException If fails
 130  
      */
 131  
     private static Take regex(final Base base) throws IOException {
 132  1
         return new TkFork(
 133  
             new FkParams(
 134  
                 PsByFlag.class.getSimpleName(),
 135  
                 Pattern.compile(".+"),
 136  
                 new TkRedirect()
 137  
             ),
 138  
             new FkRegex("/robots.txt", ""),
 139  
             new FkRegex(
 140  
                 "/xsl/[a-z\\-]+\\.xsl",
 141  
                 new TkWithType(
 142  
                     TkApp.refresh("./src/main/xsl"),
 143  
                     "text/xsl"
 144  
                 )
 145  
             ),
 146  
             new FkRegex(
 147  
                 "/js/[a-z]+\\.js",
 148  
                 new TkWithType(
 149  
                     TkApp.refresh("./src/main/js"),
 150  
                     "text/javascript"
 151  
                 )
 152  
             ),
 153  
             new FkRegex(
 154  
                 "/css/[a-z]+\\.css",
 155  
                 new TkWithType(
 156  
                     TkApp.refresh("./src/main/scss"),
 157  
                     "text/css"
 158  
                 )
 159  
             ),
 160  
             new FkAnonymous(
 161  
                 new TkFork(
 162  
                     new FkRegex("/", new TkIndex(base))
 163  
                 )
 164  
             ),
 165  
             new FkAuthenticated(
 166  
                 new TkFork(
 167  
                     new FkRegex("/", new TkDecks(base)),
 168  
                     new FkRegex("/acc", new TkAccount(base)),
 169  
                     new FkRegex("/add", new TkAddDeck(base)),
 170  
                     new FkRegex("/d/.*", new TkDeck(base))
 171  
                 )
 172  
             )
 173  
         );
 174  
     }
 175  
 
 176  
     /**
 177  
      * Hit refresh fork.
 178  
      * @param path Path of files
 179  
      * @return Fork
 180  
      * @throws IOException If fails
 181  
      */
 182  
     @SuppressWarnings("PMD.DoNotUseThreads")
 183  
     private static Take refresh(final String path) throws IOException {
 184  3
         return new TkFork(
 185  
             new FkHitRefresh(
 186  
                 new File(path),
 187  3
                 new Runnable() {
 188  
                     @Override
 189  
                     public void run() {
 190  0
                         new VerboseProcess(
 191  
                             new ProcessBuilder(
 192  
                                 "mvn",
 193  
                                 "generate-resources"
 194  
                             )
 195  
                         ).stdout();
 196  0
                     }
 197  
                 },
 198  
                 new TkFiles("./target/classes")
 199  
             ),
 200  
             new FkFixed(new TkClasspath())
 201  
         );
 202  
     }
 203  
 
 204  
 }