org.takes.rs.xe.XeAppend Java Examples

The following examples show how to use org.takes.rs.xe.XeAppend. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TkMistakes.java    From jpeek with MIT License 6 votes vote down vote up
@Override
public Response act(final Request req) {
    return new RsPage(
        req, "mistakes",
        () -> new IterableOf<>(
            new XeAppend(
                "worst",
                new XeDirectives(
                    new Joined<>(
                        new HeadOf<Iterable<Directive>>(
                            // @checkstyle MagicNumber (1 line)
                            20, new Mistakes().worst()
                        )
                    )
                )
            )
        )
    );
}
 
Example #2
Source File: TkAll.java    From jpeek with MIT License 6 votes vote down vote up
@Override
public Response act(final Request req) {
    return new RsPage(
        req, "all",
        () -> new IterableOf<>(
            new XeAppend(
                "recent",
                new XeDirectives(
                    new Joined<>(
                        new Results().all()
                    )
                )
            )
        )
    );
}
 
Example #3
Source File: XeFacebookLinkTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeFacebookLink can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesCorrectLink() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeFacebookLink(new RqFake(), "abcdef")
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/links/link[@rel='takes:facebook']"
        )
    );
}
 
Example #4
Source File: XeGithubLinkTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeGithubLink can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesCorrectLink() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeGithubLink(new RqFake(), "abcdef")
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/links/link[@rel='takes:github']"
        )
    );
}
 
Example #5
Source File: FuturesTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
public void testSimpleScenario() throws Exception {
    new Assertion<>(
        "Futures returns Response",
        new Futures(
            (artifact, group) -> input -> new RsPage(
                new RqFake(),
                "wait",
                () -> new IterableOf<>(
                    new XeAppend("group", group),
                    new XeAppend("artifact", artifact)
                )
            )
        ).apply("a", "g").get().apply("test"),
        new IsNot<>(new IsEqual<>(null))
    ).affirm();
}
 
Example #6
Source File: XeGoogleLinkTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeGoogleLink can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesCorrectLink() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeGoogleLink(new RqFake(), "abcdef")
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/links/link[@rel='takes:google']"
        )
    );
}
 
Example #7
Source File: TkDomains.java    From jare with MIT License 6 votes vote down vote up
@Override
public Response act(final Request req) throws IOException {
    return new RsPage(
        "/xsl/domains.xsl",
        req,
        new XeAppend(
            "domains",
            new XeTransform<>(
                this.base.user(new RqUser(req).name()).mine(),
                TkDomains::source
            )
        ),
        new XeLink("add", "/add"),
        new XeLink("invalidate", "/invalidate")
    );
}
 
Example #8
Source File: XeIdentityTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeIdentity can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesIdentityInXml() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeIdentity(
                        new RqWithHeader(
                            new RqFake(),
                            TkAuth.class.getSimpleName(),
                            "urn:test:1;name=Jeff"
                        )
                    )
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/identity[urn='urn:test:1']",
            "/root/identity[name='Jeff']"
        )
    );
}
 
Example #9
Source File: XeLogoutLinkTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * XeLogoutLink can create a correct link.
 * @throws IOException If some problem inside
 */
@Test
public void generatesCorrectLink() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeLogoutLink(new RqFake())
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/links/link[@rel='takes:logout']"
        )
    );
}
 
Example #10
Source File: XeFlashTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * XeFlash can accept RsFlash cookie.
 * @throws IOException If some problem inside
 */
@Test
public void rendersViaStandardXsltTemplate() throws IOException {
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXslt(
                new RsXembly(
                    new XeStylesheet(
                        "/org/takes/facets/flash/test_flash.xsl"
                    ),
                    new XeAppend(
                        "page",
                        new XeFlash(
                            new RqWithHeaders(
                                new RqFake(),
                                "Cookie: RsFlash=how are you?/INFO"
                            )
                        )
                    )
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/xhtml:html/xhtml:p[.='how are you?']",
            "/xhtml:html/xhtml:p[@class='flash flash-INFO']"
        )
    );
}
 
Example #11
Source File: XeFlashTest.java    From takes with MIT License 5 votes vote down vote up
/**
 * XeFlash can accept RsFlash cookie.
 * @throws IOException If some problem inside
 */
@Test
public void acceptsRsFlashCookie() throws IOException {
    final Pattern pattern = Pattern.compile(
        "^Set-Cookie: RsFlash=(.*?);Path.*"
    );
    final Iterator<String> itr = new RsFlash("hello").head().iterator();
    final List<String> cookies = new ArrayList<>(0);
    while (itr.hasNext()) {
        final Matcher matcher = pattern.matcher(itr.next());
        if (matcher.find()) {
            cookies.add(matcher.group(1));
        }
    }
    MatcherAssert.assertThat(cookies, Matchers.hasSize(1));
    MatcherAssert.assertThat(
        IOUtils.toString(
            new RsXembly(
                new XeAppend(
                    "root",
                    new XeFlash(
                        new RqWithHeader(
                            new RqFake(),
                            "Cookie",
                            "RsFlash=".concat(cookies.get(0))
                        )
                    )
                )
            ).body(),
            StandardCharsets.UTF_8
        ),
        XhtmlMatchers.hasXPaths(
            "/root/flash[message='hello']",
            "/root/flash[level='INFO']"
        )
    );
}
 
Example #12
Source File: RsPage.java    From takes with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param xsl XSL stylesheet name
 * @param source Xembly source
 */
RsPage(final String xsl, final XeSource source) {
    this.origin = new RsXslt(
        new RsXembly(
            new XeStylesheet(xsl),
            new XeAppend(
                "page",
                new XeMillis(false),
                source,
                new XeMillis(true)
            )
        )
    );
}
 
Example #13
Source File: TkIndex.java    From jare with MIT License 5 votes vote down vote up
@Override
public Response act(final Request req) throws IOException {
    return new RsPage(
        "/xsl/index.xsl",
        req,
        new XeAppend(
            "domains",
            new XeTransform<>(
                this.base.all(),
                TkIndex::source
            )
        )
    );
}
 
Example #14
Source File: RsPage.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Make it.
 * @param req Request
 * @param xsl XSL stylesheet
 * @param src Sources
 * @return Response
 */
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
private static Response make(final Request req, final String xsl,
    final Scalar<Iterable<XeSource>> src) {
    final Response raw = new RsXembly(
        new XeChain(
            new XeStylesheet(
                String.format("/org/jpeek/web/%s.xsl", xsl)
            ),
            new XeAppend(
                "page",
                new XeChain(
                    new XeMillis(),
                    new XeDirectives(new Header()),
                    new XeChain(src),
                    new XeMillis(true)
                )
            )
        )
    );
    return new RsFork(
        req,
        new FkTypes(
            "text/html",
            new RsXslt(new RsWithType(raw, "text/html"))
        ),
        new FkTypes(
            "application/vnd.jpeek+xml",
            new RsPrettyXml(new RsWithType(raw, "text/xml"))
        ),
        new FkTypes("*/*", raw)
    );
}
 
Example #15
Source File: TkIndex.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public Response act(final Request req) {
    return new RsPage(
        req, "index",
        () -> new IterableOf<>(
            new XeAppend(
                "best",
                new XeDirectives(
                    new Joined<>(
                        new HeadOf<>(
                            // @checkstyle MagicNumber (1 line)
                            20, new Results().best()
                        )
                    )
                )
            ),
            new XeAppend(
                "recent",
                new XeDirectives(
                    new Joined<>(
                        new HeadOf<>(
                            // @checkstyle MagicNumber (1 line)
                            25, new Results().recent()
                        )
                    )
                )
            )
        )
    );
}
 
Example #16
Source File: RsPage.java    From jare with MIT License 4 votes vote down vote up
/**
 * Make it.
 * @param xsl XSL
 * @param req Request
 * @param src Source
 * @return Response
 * @throws IOException If fails
 */
private static Response make(final String xsl, final Request req,
    final Iterable<XeSource> src) throws IOException {
    final Response raw = new RsXembly(
        new XeStylesheet(xsl),
        new XeAppend(
            "page",
            new XeMillis(false),
            new XeChain(src),
            new XeLinkHome(req),
            new XeLinkSelf(req),
            new XeMillis(true),
            new XeDate(),
            new XeSla(),
            new XeLocalhost(),
            new XeFlash(req),
            new XeWhen(
                new RqAuth(req).identity().equals(Identity.ANONYMOUS),
                new XeChain(
                    new XeGithubLink(req, Manifests.read("Jare-GithubId"))
                )
            ),
            new XeWhen(
                !new RqAuth(req).identity().equals(Identity.ANONYMOUS),
                new XeChain(
                    new XeIdentity(req),
                    new XeLogoutLink(req),
                    new XeLink("domains", "/domains")
                )
            ),
            new XeAppend(
                "version",
                new XeAppend("name", Manifests.read("Jare-Version")),
                new XeAppend("revision", Manifests.read("Jare-Revision")),
                new XeAppend("date", Manifests.read("Jare-Date")),
                new XeAppend("heroku", RsPage.heroku())
            )
        )
    );
    return new RsFork(
        req,
        new FkTypes(
            "application/xml,text/xml",
            new RsPrettyXml(new RsWithType(raw, "text/xml"))
        ),
        new FkTypes(
            "*/*",
            new RsXslt(new RsWithType(raw, "text/html"))
        )
    );
}
 
Example #17
Source File: Futures.java    From jpeek with MIT License 4 votes vote down vote up
@Override
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public Future<Func<String, Response>> apply(final String group,
    final String artifact) {
    final String target = String.format("%s:%s", group, artifact);
    this.queue.put(target, System.currentTimeMillis());
    // @checkstyle MagicNumber (1 line)
    if (this.times.size() > 1000) {
        this.times.clear();
    }
    return this.service.submit(
        new VerboseCallable<>(
            () -> {
                Func<String, Response> front;
                try {
                    Logger.info(
                        this, "Started processing of %s:%s...",
                        group, artifact
                    );
                    front = this.origin.apply(group, artifact);
                    this.times.add(
                        System.currentTimeMillis() - this.queue.remove(target)
                    );
                    Logger.info(
                        this, "Finished processing of %s:%s",
                        artifact, group
                    );
                // @checkstyle IllegalCatchCheck (4 lines)
                // @checkstyle AvoidCatchingGenericException (4 lines)
                } catch (final Exception ex) {
                    Logger.error(
                        this, "Failure in %s:%s: %s",
                        group, artifact, ex.getMessage()
                    );
                    front = input -> new RsPage(
                        new RqFake(),
                        "exception",
                        () -> new IterableOf<>(
                            new XeAppend("group", group),
                            new XeAppend("artifact", artifact),
                            new XeAppend(
                                "stacktrace",
                                new TextOf(new InputOf(ex)).asString()
                            )
                        )
                    );
                }
                return front;
            },
            true, true
        )
    );
}
 
Example #18
Source File: AsyncReports.java    From jpeek with MIT License 4 votes vote down vote up
@Override
public Func<String, Response> apply(final String group,
    final String artifact) throws IOException {
    final Future<Func<String, Response>> future = new IoCheckedBiFunc<>(
        this.cache
    ).apply(group, artifact);
    final Func<String, Response> output;
    if (future.isCancelled()) {
        output = input -> new RsPage(
            new RqFake(),
            "error",
            () -> new IterableOf<>(
                new XeAppend("group", group),
                new XeAppend("artifact", artifact),
                new XeAppend("future", future.toString())
            )
        );
    } else if (future.isDone()) {
        try {
            output = future.get();
        } catch (final InterruptedException | ExecutionException ex) {
            throw new IllegalStateException(ex);
        }
    } else {
        final long msec = System.currentTimeMillis()
            - this.starts.computeIfAbsent(
                String.format("%s:%s", group, artifact),
                s -> System.currentTimeMillis()
            );
        output = input -> new RsWithStatus(
            new RsPage(
                new RqFake(),
                "wait",
                () -> new IterableOf<>(
                    new XeAppend("group", group),
                    new XeAppend("artifact", artifact),
                    new XeAppend("future", future.toString()),
                    new XeAppend("msec", Long.toString(msec)),
                    new XeAppend(
                        "spent",
                        Logger.format("%[ms]s", msec)
                    )
                )
            ),
            HttpURLConnection.HTTP_NOT_FOUND
        );
    }
    return output;
}