All Downloads are FREE. Search and download functionalities are using the official Maven repository.

netbout-spi-1.4.3.src.main.java.com.netbout.spi.client.RexslRestResponse Maven / Gradle / Ivy

There is a newer version: 1.5.10
Show newest version
/**
 * Copyright (c) 2009-2011, NetBout.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the following
 * disclaimer. 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution. 3) Neither the name of the NetBout.com nor
 * the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.netbout.spi.client;

import com.rexsl.test.TestResponse;
import com.ymock.util.Logger;
import java.net.URI;
import java.util.List;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriBuilder;
import org.hamcrest.Matchers;

/**
 * Response generated by a client, on top of ReXSL.
 *
 * @author Yegor Bugayenko ([email protected])
 * @version $Id: RexslRestResponse.java 834 2011-12-27 07:22:07Z guard $
 */
final class RexslRestResponse implements RestResponse {

    /**
     * The client.
     */
    private final transient RestClient client;

    /**
     * The response, from ReXSL.
     */
    private final transient TestResponse response;

    /**
     * Public ctor.
     * @param clnt The client
     * @param resp The response
     */
    public RexslRestResponse(final RestClient clnt, final TestResponse resp) {
        this.client = clnt;
        this.response = resp;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void fail(final String cause) {
        this.response.fail(cause);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RestResponse assertStatus(final int code) {
        this.response.assertStatus(code);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RestResponse assertXPath(final String xpath) {
        this.response.assertXPath(xpath);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List xpath(final String query) {
        return this.response.xpath(query);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public URI location() {
        final String header = this.response.getHeaders()
            .getFirst(HttpHeaders.LOCATION);
        final URI uri = UriBuilder.fromUri(header).build();
        Logger.debug(
            this,
            "#location(): returned '%s'",
            uri
        );
        return uri;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String header(final String name) {
        return this.response
            .assertHeader(name, Matchers.notNullValue())
            .getHeaders()
            .getFirst(name);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public RestClient rel(final String rel) {
        String xpath = rel;
        if (xpath.charAt(0) != '/') {
            xpath = Logger.format("/page/links/link[@rel='%s']/@href", rel);
        }
        final URI uri = UriBuilder.fromUri(this.xpath(xpath).get(0)).build();
        Logger.debug(
            this,
            "#rel('%s'): going to '%s'",
            rel,
            uri
        );
        return this.client.copy(uri);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy