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

org.jboss.resteasy.specimpl.ResteasyHttpHeaders Maven / Gradle / Ivy

There is a newer version: 4.0.0.Beta5
Show newest version
package org.jboss.resteasy.specimpl;

import org.jboss.resteasy.util.CookieParser;
import org.jboss.resteasy.util.DateUtil;
import org.jboss.resteasy.util.MediaTypeHelper;
import org.jboss.resteasy.util.WeightedLanguage;

import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author Bill Burke
 * @version $Revision: 1 $
 */
public class ResteasyHttpHeaders implements HttpHeaders
{

   private MultivaluedMap requestHeaders;
   private MultivaluedMap unmodifiableRequestHeaders;
   private Map cookies;

   public ResteasyHttpHeaders(final MultivaluedMap requestHeaders)
   {
      this(requestHeaders, new HashMap());
   }

   public ResteasyHttpHeaders(final MultivaluedMap requestHeaders, final Map cookies)
   {
      this.requestHeaders = requestHeaders;
      this.unmodifiableRequestHeaders = new UnmodifiableMultivaluedMap<>(requestHeaders);
      this.cookies = (cookies == null ? new HashMap<>() : cookies);
   }

   @Override
   public MultivaluedMap getRequestHeaders()
   {
      return unmodifiableRequestHeaders;
   }

   public MultivaluedMap getMutableHeaders()
   {
      return requestHeaders;
   }

   public void testParsing()
   {
      // test parsing should throw an exception on error
      getAcceptableMediaTypes();
      getMediaType();
      getLanguage();
      getAcceptableLanguages();

   }

   @Override
   public List getRequestHeader(String name)
   {
      List vals = unmodifiableRequestHeaders.get(name);
      return vals == null ? Collections.emptyList() : vals;
   }

   @Override
   public Map getCookies()
   {
      mergeCookies();
      return Collections.unmodifiableMap(cookies);
   }

   public Map getMutableCookies()
   {
      mergeCookies();
      return cookies;
   }

   public void setCookies(Map cookies)
   {
      this.cookies = cookies;
   }

   @Override
   public Date getDate()
   {
      String date = requestHeaders.getFirst(DATE);
      if (date == null) return null;
      return DateUtil.parseDate(date);
   }

   @Override
   public String getHeaderString(String name)
   {
      List vals = requestHeaders.get(name);
      if (vals == null) return null;
      StringBuilder builder = new StringBuilder();
      boolean first = true;
      for (String val : vals)
      {
         if (first) first = false;
         else builder.append(",");
         builder.append(val);
      }
      return builder.toString();
   }

   @Override
   public Locale getLanguage()
   {
      String obj = requestHeaders.getFirst(HttpHeaders.CONTENT_LANGUAGE);
      if (obj == null) return null;
      return new Locale(obj);
   }

   @Override
   public int getLength()
   {
      String obj = requestHeaders.getFirst(HttpHeaders.CONTENT_LENGTH);
      if (obj == null) return -1;
      return Integer.parseInt(obj);
   }

   // because header string map is mutable, we only cache the parsed media type
   // and still do hash lookup
   private String cachedMediaTypeString;
   private MediaType cachedMediaType;
   @Override
   public MediaType getMediaType()
   {
      String obj = requestHeaders.getFirst(HttpHeaders.CONTENT_TYPE);
      if (obj == null) return null;
      if (obj == cachedMediaTypeString) return cachedMediaType;
      cachedMediaTypeString = obj;
      cachedMediaType = MediaType.valueOf(obj);
      return cachedMediaType;
   }

   @Override
   public List getAcceptableMediaTypes()
   {
      String accepts = getHeaderString(ACCEPT);
      List list = new ArrayList();
      if (accepts == null){
         list.add(MediaType.WILDCARD_TYPE);
      }else{
         StringTokenizer tokenizer = new StringTokenizer(accepts, ",");
         while (tokenizer.hasMoreElements())
         {
            String item = tokenizer.nextToken().trim();
            list.add(MediaType.valueOf(item));
         }
         MediaTypeHelper.sortByWeight(list);
      }
      return Collections.unmodifiableList(list);
   }

   @Override
   public List getAcceptableLanguages()
   {
      String accepts = getHeaderString(ACCEPT_LANGUAGE);
      if (accepts == null) return Collections.emptyList();
      List list = new ArrayList();
      List languages = new ArrayList();
      StringTokenizer tokenizer = new StringTokenizer(accepts, ",");
      while (tokenizer.hasMoreElements())
      {
         String item = tokenizer.nextToken().trim();
         languages.add(WeightedLanguage.parse(item));
      }
      Collections.sort(languages);
      for (WeightedLanguage language : languages) list.add(language.getLocale());
      return Collections.unmodifiableList(list);
   }

   private void mergeCookies()
   {
      List cookieHeader = requestHeaders.get(HttpHeaders.COOKIE);
      if (cookieHeader != null && !cookieHeader.isEmpty())
      {
         for (String s : cookieHeader)
         {
            List list = CookieParser.parseCookies(s);
            for (Cookie cookie : list)
            {
               cookies.put(cookie.getName(), cookie);
            }
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy