Coverage Report - com.mattunderscore.http.headers.content.type.ContentTypeParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentTypeParser
88%
38/43
82%
23/28
5.167
 
 1  
 /* Copyright © 2012, 2013 Matthew Champion
 2  
 All rights reserved.
 3  
 
 4  
 Redistribution and use in source and binary forms, with or without
 5  
 modification, are permitted provided that the following conditions are met:
 6  
  * Redistributions of source code must retain the above copyright
 7  
       notice, this list of conditions and the following disclaimer.
 8  
  * Redistributions in binary form must reproduce the above copyright
 9  
       notice, this list of conditions and the following disclaimer in the
 10  
       documentation and/or other materials provided with the distribution.
 11  
  * Neither the name of mattunderscore.com nor the
 12  
       names of its contributors may be used to endorse or promote products
 13  
       derived from this software without specific prior written permission.
 14  
 
 15  
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 16  
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 17  
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 18  
 DISCLAIMED. IN NO EVENT SHALL MATTHEW CHAMPION BE LIABLE FOR ANY
 19  
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 20  
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 21  
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 22  
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 23  
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 24  
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 25  
 
 26  
 package com.mattunderscore.http.headers.content.type;
 27  
 
 28  
 import java.util.Collection;
 29  
 import java.util.ArrayList;
 30  
 import java.util.Map;
 31  
 import java.util.HashMap;
 32  
 
 33  
 import com.mattunderscore.http.headers.HeaderFieldCollectionParser;
 34  
 import com.mattunderscore.http.headers.HeaderFieldNameVerifier;
 35  
 import com.mattunderscore.http.headers.UnParsableHeaderException;
 36  
 
 37  
 /**
 38  
  * Parse an accept field header string to a structured representation.
 39  
  * 
 40  
  * @author Matt Champion
 41  
  * @since 0.0.13
 42  
  */
 43  
 public class ContentTypeParser implements HeaderFieldCollectionParser<QContentType>,
 44  
         HeaderFieldNameVerifier
 45  
 {
 46  
     private static final String HEADER_FIELD_NAME = "Accept";
 47  
 
 48  
     public ContentTypeParser()
 49  64
     {
 50  64
     }
 51  
 
 52  
     @Override
 53  
     public boolean isHeaderFieldName(String header)
 54  
     {
 55  0
         return HEADER_FIELD_NAME.equalsIgnoreCase(header);
 56  
     }
 57  
 
 58  
     @Override
 59  
     public String getHeaderFieldName()
 60  
     {
 61  8
         return HEADER_FIELD_NAME;
 62  
     }
 63  
 
 64  
     @Override
 65  
     public Collection<QContentType> parseElements(String header) throws UnParsableHeaderException
 66  
     {
 67  64
         if (header == null)
 68  
         {
 69  0
             throw new UnParsableHeaderException("Null Pointer.");
 70  
         }
 71  64
         final Collection<QContentType> qCT = new ArrayList<QContentType>();
 72  64
         if ("".equals(header))
 73  
         {
 74  4
             return qCT;
 75  
         }
 76  60
         final String[] types = header.split(",");
 77  110
         for (int i = 0; i < types.length; i++)
 78  
         {
 79  66
             final QContentType ct = parseSingleElement(types[i].trim());
 80  50
             if (ct != null)
 81  
             {
 82  50
                 qCT.add(ct);
 83  
             }
 84  
         }
 85  44
         return qCT;
 86  
     }
 87  
 
 88  
     private QContentType parseSingleElement(String types) throws UnParsableHeaderException
 89  
     {
 90  66
         if (types == null)
 91  
         {
 92  0
             throw new UnParsableHeaderException("Null pointer");
 93  
         }
 94  66
         if ("".equals(types.trim()))
 95  
         {
 96  0
             throw new UnParsableHeaderException("Empty string");
 97  
         }
 98  66
         final String[] typeParts = types.trim().split("/");
 99  66
         if (typeParts.length == 2)
 100  
         {
 101  62
             final String[] lastParts = typeParts[1].trim().split(";", -1);
 102  62
             final Map<String, String> map = new HashMap<String, String>();
 103  62
             final double qualifier = parseParameters(lastParts, map);
 104  50
             return new QContentType(typeParts[0].trim(), lastParts[0].trim(), map, qualifier);
 105  
         }
 106  
         else
 107  
         {
 108  4
             throw new UnParsableHeaderException("Unparsable content type: " + types.trim() + ": "
 109  
                     + typeParts.length);
 110  
         }
 111  
     }
 112  
 
 113  
     /**
 114  
      * Parse the parameters of the content type.
 115  
      * 
 116  
      * @param parameters
 117  
      *            Parameter array
 118  
      * @param map
 119  
      *            Parameter map
 120  
      * @return Qualifier
 121  
      * @throws UnParsableHeaderException
 122  
      */
 123  
     private double parseParameters(String[] parameters, Map<String, String> map)
 124  
             throws UnParsableHeaderException
 125  
     {
 126  62
         double qualifier = 1.0;
 127  62
         if (parameters.length > 1)
 128  
         {
 129  72
             for (int i = 1; i < parameters.length; i++)
 130  
             {
 131  44
                 final String[] extensions = parameters[i].trim().split("=", -1);
 132  44
                 if (extensions.length != 2)
 133  
                 {
 134  6
                     throw new UnParsableHeaderException("Invalid parameter.");
 135  
                 }
 136  38
                 if ("q".equals(extensions[0].trim()))
 137  
                 {
 138  
                     try
 139  
                     {
 140  30
                         qualifier = Double.parseDouble(extensions[1].trim());
 141  
                     }
 142  2
                     catch (final NumberFormatException exception)
 143  
                     {
 144  2
                         throw new UnParsableHeaderException("Unable to parse number");
 145  28
                     }
 146  28
                     if (qualifier < 0.0 || qualifier > 1.0)
 147  
                     {
 148  4
                         throw new UnParsableHeaderException("Invalid qualifier range.");
 149  
                     }
 150  
                 }
 151  
                 else
 152  
                 {
 153  8
                     map.put(extensions[0].trim(), extensions[1].trim());
 154  
                 }
 155  
             }
 156  
         }
 157  22
         else if (parameters.length == 0)
 158  
         {
 159  0
             throw new UnParsableHeaderException("Unparsable content type: " + parameters);
 160  
         }
 161  50
         return qualifier;
 162  
     }
 163  
 }