com.rexsl.page.BasePage Maven / Gradle / Ivy
/**
* Copyright (c) 2011-2013, ReXSL.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 ReXSL.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.rexsl.page;
import com.jcabi.aspects.Loggable;
import com.jcabi.log.Logger;
import com.rexsl.core.XslResolver;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.validation.constraints.NotNull;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.w3c.dom.Element;
/**
* Base page.
*
* Use it as a base class for your own page, for example:
*
*
public class MyBasePage extends BasePage<MyPage> {
* @XmlAnyElement(lax = true)
* @XmlElement
* public String getUser() {
* return "John Doe";
* }
* }
*
* However, it is recommended to use {@link #append(Object)} and
* {@link JaxbGroup}/{@link JaxbBundle} instead of defining own methods
* annotated with {@code @XmlElement}.
*
*
Don't forget to call {@link #init(Resource)} right after the page is
* built by {@link PageBuilder}, for example:
*
*
@Path("/")
* public class MainRs extends BaseResource {
* @GET
* @Produces(MediaTypes.APPLICATION_XML)
* public BasePage front() {
* return new PageBuilder()
* .stylesheet("/xsl/front.xsl")
* .build(BasePage.class)
* .init(this);
* }
* }
*
* JAX-RS resource classes should implement {@link Resource} or even
* extend {@link BaseResource}, which is preferred.
*
*
This class adds {@code date} and {@code ip} attributes to the page, and
* {@code links} and {@code millis} element. Thus, an empty page (if you don't
* {@link #append(Object)} anything to it) will look like:
*
*
<?xml version="1.0" ?>
* <page date="2012-04-15T07:07Z" ip="127.0.0.1">
* <links />
* <millis>234</millis>
* </page>
*
* This functionality is not changeable. If this is not what you need in
* your page - just don't use this class and create your own. However, we
* believe that the majority of web applications need this information in
* their XML pages.
*
*
The class is mutable and thread-safe.
*
* @param Type of page
* @param Type of JAX-RS resource
* @author Yegor Bugayenko ([email protected])
* @version $Id$
* @since 0.3.7
* @see PageBuilder
* @see Resource
* @see BaseResource
*/
@XmlType(name = "com.rexsl.page.BasePage")
@XmlRootElement(name = "page")
@XmlAccessorType(XmlAccessType.NONE)
@SuppressWarnings({ "unchecked", "PMD.ExcessiveImports" })
@ToString
@EqualsAndHashCode(callSuper = false, of = "resource")
@Loggable(Loggable.DEBUG)
public class BasePage, R extends Resource> {
/**
* The resource.
*/
private transient R resource;
/**
* Collection of links.
*/
private final transient Collection links =
new CopyOnWriteArrayList();
/**
* Collection of elements.
*/
private final transient Collection