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

org.jboss.resteasy.spi.LinkHeaders Maven / Gradle / Ivy

The newest version!
package org.jboss.resteasy.spi;

import org.jboss.resteasy.plugins.delegates.LinkHeaderDelegate;

import javax.ws.rs.core.Link;
import javax.ws.rs.core.MultivaluedMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Abstraction for LInk headers.  Also uses JAXRS classes rather than deprecated old resteasy ones.
 *
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class LinkHeaders
{
   private Map linksByRelationship = new HashMap();
   private Map linksByTitle = new HashMap();
   private List links = new ArrayList();

   public LinkHeaders addLinks(MultivaluedMap headers)
   {
      List values = headers.get("Link");
      if (values == null) return this;
      for (String val : values)
      {
         LinkHeader linkHeader = new LinkHeaderDelegate().fromString(val);
         for (Link link : linkHeader.getLinks())
         {
            addLink(link);
         }
      }
      return this;
   }

   public LinkHeaders addLinkObjects(MultivaluedMap headers, HeaderValueProcessor factory)
   {
      List values = headers.get("Link");
      if (values == null) return this;
      for (Object val : values)
      {
         if (val instanceof Link) addLink((Link)val);
         else
         {
            String str = factory.toHeaderString(val);
            addLink(Link.valueOf(str));
         }
      }
      return this;
   }

   public LinkHeaders addLink(final Link link)
   {
      links.add(link);
      for (String rel : link.getRels())
      {
         linksByRelationship.put(rel, link);
      }
      if (link.getTitle() != null) linksByTitle.put(link.getTitle(), link);
      return this;
   }

   public Link getLinkByTitle(String title)
   {
      return linksByTitle.get(title);
   }

   public Link getLinkByRelationship(String rel)
   {
      return linksByRelationship.get(rel);
   }

   /**
    * Index of links by relationship "rel" or "rev".
    *
    * @return map
    */
   public Map getLinksByRelationship()
   {
      return linksByRelationship;
   }

   /**
    * Index of links by title.
    *
    * @return map
    */
   public Map getLinksByTitle()
   {
      return linksByTitle;
   }

   /**
    * All the links defined.
    *
    * @return links
    */
   public List getLinks()
   {
      return links;
   }

}