| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TkAppFallback |
|
| 1.0;1 | ||||
| TkAppFallback$1 |
|
| 1.0;1 | ||||
| TkAppFallback$2 |
|
| 1.0;1 |
| 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.Logger; | |
| 33 | import com.jcabi.manifests.Manifests; | |
| 34 | import java.io.IOException; | |
| 35 | import java.net.HttpURLConnection; | |
| 36 | import org.apache.commons.lang3.exception.ExceptionUtils; | |
| 37 | import org.takes.Response; | |
| 38 | import org.takes.Take; | |
| 39 | import org.takes.facets.fallback.Fallback; | |
| 40 | import org.takes.facets.fallback.FbChain; | |
| 41 | import org.takes.facets.fallback.FbStatus; | |
| 42 | import org.takes.facets.fallback.RqFallback; | |
| 43 | import org.takes.facets.fallback.TkFallback; | |
| 44 | import org.takes.misc.Opt; | |
| 45 | import org.takes.rs.RsText; | |
| 46 | import org.takes.rs.RsVelocity; | |
| 47 | import org.takes.rs.RsWithStatus; | |
| 48 | import org.takes.rs.RsWithType; | |
| 49 | import org.takes.tk.TkWrap; | |
| 50 | ||
| 51 | /** | |
| 52 | * App fallback. | |
| 53 | * | |
| 54 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 55 | * @version $Id$ | |
| 56 | * @since 0.5 | |
| 57 | * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) | |
| 58 | */ | |
| 59 | 1 | final class TkAppFallback extends TkWrap { |
| 60 | ||
| 61 | /** | |
| 62 | * Version of netbout. | |
| 63 | */ | |
| 64 | 1 | private static final String VERSION = Manifests.read("Thindeck-Version"); |
| 65 | ||
| 66 | /** | |
| 67 | * Ctor. | |
| 68 | * @param take Take | |
| 69 | */ | |
| 70 | TkAppFallback(final Take take) { | |
| 71 | 1 | super(TkAppFallback.make(take)); |
| 72 | 1 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Authenticated. | |
| 76 | * @param takes Take | |
| 77 | * @return Authenticated takes | |
| 78 | */ | |
| 79 | private static Take make(final Take takes) { | |
| 80 | 1 | final Fallback fall = new Fallback() { |
| 81 | @Override | |
| 82 | public Opt<Response> route(final RqFallback req) { | |
| 83 | 0 | return new Opt.Single<Response>( |
| 84 | new RsWithStatus( | |
| 85 | new RsText(req.throwable().getLocalizedMessage()), | |
| 86 | req.code() | |
| 87 | ) | |
| 88 | ); | |
| 89 | } | |
| 90 | }; | |
| 91 | 1 | return new TkFallback( |
| 92 | takes, | |
| 93 | new FbChain( | |
| 94 | new FbStatus(HttpURLConnection.HTTP_NOT_FOUND, fall), | |
| 95 | new FbStatus(HttpURLConnection.HTTP_BAD_REQUEST, fall), | |
| 96 | 1 | new Fallback() { |
| 97 | @Override | |
| 98 | public Opt<Response> route(final RqFallback req) | |
| 99 | throws IOException { | |
| 100 | 1 | return new Opt.Single<>(TkAppFallback.fatal(req)); |
| 101 | } | |
| 102 | } | |
| 103 | ) | |
| 104 | ); | |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * Make a fatal response. | |
| 109 | * @param req Request | |
| 110 | * @return Response | |
| 111 | * @throws IOException If fails | |
| 112 | */ | |
| 113 | private static Response fatal(final RqFallback req) throws IOException { | |
| 114 | 1 | final String err = ExceptionUtils.getStackTrace( |
| 115 | req.throwable() | |
| 116 | ); | |
| 117 | 1 | Logger.error(TkAppFallback.class, "%[exception]s", req.throwable()); |
| 118 | 1 | return new RsWithStatus( |
| 119 | new RsWithType( | |
| 120 | new RsVelocity( | |
| 121 | TkAppFallback.class.getResource("error.html.vm"), | |
| 122 | new RsVelocity.Pair("error", err), | |
| 123 | new RsVelocity.Pair("version", TkAppFallback.VERSION) | |
| 124 | ), | |
| 125 | "text/html" | |
| 126 | ), | |
| 127 | HttpURLConnection.HTTP_INTERNAL_ERROR | |
| 128 | ); | |
| 129 | } | |
| 130 | ||
| 131 | } |