##### Coverage Report - org.apache.any23.http.AcceptHeaderBuilder

Classes in this File | Line Coverage | Branch Coverage | Complexity
AcceptHeaderBuilder |
0% | 0/53
0% | 0/48
| 5.5;5.5

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      */
17 |  |
18 |  |
     package org.apache.any23.http;
19 |  |
20 |  |
     import org.apache.any23.mime.MIMEType;
21 |  |
22 |  |
     import java.util.ArrayList;
23 |  |
     import java.util.Collection;
24 |  |
     import java.util.Collections;
25 |  |
     import java.util.HashMap;
26 |  |
     import java.util.Iterator;
27 |  |
     import java.util.List;
28 |  |
     import java.util.Map;
29 |  |
30 |  |
     /**
31 |  |
      * Concatenates a collection of MIME specs in "type/subtype;q=x.x" notation
32 |  |
      * into an HTTP Accept header value, and removes duplicates and types
33 |  |
      * covered by wildcards. For example, if the type list contains "text/*;q=0.5",
34 |  |
      * then "text/plain;q=0.1" in the list will be ignored because it's already
35 |  |
      * covered by the wildcard with a higher q value.
36 |  |
      *
37 |  |
      * @author Richard Cyganiak (richard@cyganiak.de)
38 |  |
      */
39 |  |
     public class AcceptHeaderBuilder {
40 |  |
41 |  |
         private Collection<MIMEType> mimeTypes;
42 |  |
43 | 0 |
         private MIMEType highestAnyType = null;
44 |  |
45 | 0 |
         private Map<String, MIMEType> highestAnySubtype = new HashMap<String, MIMEType>();
46 |  |
47 | 0 |
         private Map<String, MIMEType> highestSpecificType = new HashMap<String, MIMEType>();
48 |  |
49 |  |
         public static AcceptHeaderBuilder fromStrings(Collection<String> typesAsStrings) {
50 | 0 |
             Collection<MIMEType> types = new ArrayList<MIMEType>(typesAsStrings.size());
51 | 0 |
             for (String type : typesAsStrings) {
52 | 0 |
                 types.add(MIMEType.parse(type));
53 |  |
             }
54 | 0 |
             return new AcceptHeaderBuilder(types);
55 |  |
         }
56 |  |
57 | 0 |
         public AcceptHeaderBuilder(Collection<MIMEType> mimeTypes) {
58 | 0 |
             this.mimeTypes = mimeTypes;
59 | 0 |
         }
60 |  |
61 |  |
         /**
62 |  |
          * Builds and returns an accept header.
63 |  |
          *
64 |  |
          * @throws IllegalArgumentException if an input MIME type cannot be parsed.
65 |  |
          */
66 |  |
         public String getAcceptHeader() {
67 | 0 |
             if (mimeTypes.isEmpty()) return null;
68 | 0 |
             for (MIMEType mimeType : mimeTypes) {
69 | 0 |
                 add(mimeType);
70 |  |
             }
71 | 0 |
             removeSpecificTypesCoveredByWildcard();
72 | 0 |
             removeTypesCoveredByWildcard();
73 | 0 |
             List<MIMEType> highest = new ArrayList<MIMEType>();
74 | 0 |
             if (highestAnyType != null) {
75 | 0 |
                 highest.add(highestAnyType);
76 |  |
             }
77 | 0 |
             highest.addAll(highestAnySubtype.values());
78 | 0 |
             highest.addAll(highestSpecificType.values());
79 | 0 |
             Collections.sort(highest);
80 | 0 |
             StringBuffer result = new StringBuffer();
81 | 0 |
             Iterator<MIMEType> it = mimeTypes.iterator();
82 | 0 |
             while (it.hasNext()) {
83 | 0 |
                 MIMEType a = it.next();
84 | 0 |
                 if (!highest.contains(a)) continue;
85 | 0 |
                 if (result.length() > 0) {
86 | 0 |
                     result.append(", ");
87 |  |
                 }
88 | 0 |
                 result.append(a);
89 | 0 |
             }
90 | 0 |
             return result.toString();
91 |  |
         }
92 |  |
93 |  |
         private void add(MIMEType newAccept) {
94 | 0 |
             if (newAccept.isAnyMajorType()) {
95 | 0 |
                 if (highestAnyType == null || newAccept.getQuality() > highestAnyType.getQuality()) {
96 | 0 |
                     highestAnyType = newAccept;
97 |  |
                 }
98 | 0 |
             } else if (newAccept.isAnySubtype()) {
99 | 0 |
                 if (!highestAnySubtype.containsKey(newAccept.getMajorType())
100 |  |
                         || newAccept.getQuality() > highestAnySubtype.get(newAccept.getMajorType()).getQuality()) {
101 | 0 |
                     highestAnySubtype.put(newAccept.getMajorType(), newAccept);
102 |  |
                 }
103 |  |
             } else {
104 | 0 |
                 if (!highestSpecificType.containsKey(newAccept.getFullType())
105 |  |
                         || newAccept.getQuality() > highestSpecificType.get(newAccept.getFullType()).getQuality()) {
106 | 0 |
                     highestSpecificType.put(newAccept.getFullType(), newAccept);
107 |  |
                 }
108 |  |
             }
109 | 0 |
         }
110 |  |
111 |  |
         private void removeSpecificTypesCoveredByWildcard() {
112 | 0 |
             for (MIMEType accept : highestSpecificType.values()) {
113 | 0 |
                 if (highestAnySubtype.containsKey(accept.getMajorType())
114 |  |
                         && accept.getQuality() <= highestAnySubtype.get(accept.getMajorType()).getQuality()) {
115 | 0 |
                     highestSpecificType.remove(accept.getFullType());
116 |  |
                 }
117 |  |
             }
118 | 0 |
             if (highestAnyType == null) return;
119 | 0 |
             for (MIMEType accept : highestSpecificType.values()) {
120 | 0 |
                 if (accept.getQuality() <= highestAnyType.getQuality()) {
121 | 0 |
                     highestSpecificType.remove(accept.getFullType());
122 |  |
                 }
123 |  |
             }
124 | 0 |
         }
125 |  |
126 |  |
         private void removeTypesCoveredByWildcard() {
127 | 0 |
             if (highestAnyType == null) return;
128 | 0 |
             for (MIMEType accept : highestAnySubtype.values()) {
129 | 0 |
                 if (accept.getQuality() <= highestAnyType.getQuality()) {
130 | 0 |
                     highestAnySubtype.remove(accept.getMajorType());
131 |  |
                 }
132 |  |
             }
133 | 0 |
         }
134 |  |
135 |  |
     }