Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RsPage |
|
| 1.6666666666666667;1.667 | ||||
RsPage$1 |
|
| 1.6666666666666667;1.667 |
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.thindeck.api.Base; | |
33 | import java.io.IOException; | |
34 | import java.util.Iterator; | |
35 | import lombok.EqualsAndHashCode; | |
36 | import org.takes.Request; | |
37 | import org.takes.Response; | |
38 | import org.takes.facets.fork.FkTypes; | |
39 | import org.takes.facets.fork.Fork; | |
40 | import org.takes.facets.fork.RsFork; | |
41 | import org.takes.misc.Opt; | |
42 | import org.takes.rq.RqHeaders; | |
43 | import org.takes.rs.RsPrettyXML; | |
44 | import org.takes.rs.RsWithType; | |
45 | import org.takes.rs.RsWrap; | |
46 | import org.takes.rs.RsXSLT; | |
47 | import org.takes.rs.xe.RsXembly; | |
48 | import org.takes.rs.xe.XeSource; | |
49 | import org.takes.rs.xe.XeStylesheet; | |
50 | ||
51 | /** | |
52 | * Default page. | |
53 | * | |
54 | * @author Yegor Bugayenko (yegor@teamed.io) | |
55 | * @version $Id$ | |
56 | * @since 0.5 | |
57 | * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) | |
58 | */ | |
59 | 0 | @EqualsAndHashCode(callSuper = true) |
60 | public final class RsPage extends RsWrap { | |
61 | ||
62 | /** | |
63 | * Ctor. | |
64 | * @param xsl XSL | |
65 | * @param base Base | |
66 | * @param req Request | |
67 | * @param src Source | |
68 | * @throws IOException If fails | |
69 | * @checkstyle ParameterNumberCheck (5 lines) | |
70 | */ | |
71 | public RsPage(final String xsl, final Base base, | |
72 | final Request req, final XeSource... src) throws IOException { | |
73 | 2 | super(RsPage.make(xsl, base, req, src)); |
74 | 2 | } |
75 | ||
76 | /** | |
77 | * Make it. | |
78 | * @param xsl XSL | |
79 | * @param base Base | |
80 | * @param req Request | |
81 | * @param src Source | |
82 | * @return Response | |
83 | * @throws IOException If fails | |
84 | * @checkstyle ParameterNumberCheck (5 lines) | |
85 | */ | |
86 | private static Response make(final String xsl, final Base base, | |
87 | final Request req, final XeSource... src) throws IOException { | |
88 | 2 | final Response xbl = new RsXembly( |
89 | new XeStylesheet(xsl), | |
90 | new XePage(base, req, src) | |
91 | ); | |
92 | 2 | final Response raw = new RsWithType(xbl, "text/xml"); |
93 | 2 | return new RsFork( |
94 | req, | |
95 | 2 | new Fork() { |
96 | @Override | |
97 | public Opt<Response> route(final Request rst) | |
98 | throws IOException { | |
99 | 2 | final RqHeaders hdr = new RqHeaders.Base(rst); |
100 | 2 | final Iterator<String> agent = |
101 | hdr.header("User-Agent").iterator(); | |
102 | final Opt<Response> opt; | |
103 | 2 | if (agent.hasNext() && agent.next().contains("Firefox")) { |
104 | 0 | opt = new Opt.Single<Response>( |
105 | // @checkstyle MultipleStringLiteralsCheck (1 line) | |
106 | new RsXSLT(new RsWithType(raw, "text/html")) | |
107 | ); | |
108 | } else { | |
109 | 2 | opt = new Opt.Empty<>(); |
110 | } | |
111 | 2 | return opt; |
112 | } | |
113 | }, | |
114 | new FkTypes("application/xml,text/xml", new RsPrettyXML(raw)), | |
115 | new FkTypes( | |
116 | "*/*", | |
117 | new RsXSLT(new RsWithType(raw, "text/html")) | |
118 | ) | |
119 | ); | |
120 | } | |
121 | ||
122 | } |