View Javadoc
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      private MIMEType highestAnyType = null;
44  
45      private Map<String, MIMEType> highestAnySubtype = new HashMap<String, MIMEType>();
46  
47      private Map<String, MIMEType> highestSpecificType = new HashMap<String, MIMEType>();
48  
49      public static AcceptHeaderBuilder fromStrings(Collection<String> typesAsStrings) {
50          Collection<MIMEType> types = new ArrayList<MIMEType>(typesAsStrings.size());
51          for (String type : typesAsStrings) {
52              types.add(MIMEType.parse(type));
53          }
54          return new AcceptHeaderBuilder(types);
55      }
56  
57      public AcceptHeaderBuilder(Collection<MIMEType> mimeTypes) {
58          this.mimeTypes = mimeTypes;
59      }
60  
61      /**
62       * Builds and returns an accept header.
63       * @return the accept header.
64       */
65      public String getAcceptHeader() {
66          if (mimeTypes.isEmpty()) return null;
67          for (MIMEType mimeType : mimeTypes) {
68              add(mimeType);
69          }
70          removeSpecificTypesCoveredByWildcard();
71          removeTypesCoveredByWildcard();
72          List<MIMEType> highest = new ArrayList<MIMEType>();
73          if (highestAnyType != null) {
74              highest.add(highestAnyType);
75          }
76          highest.addAll(highestAnySubtype.values());
77          highest.addAll(highestSpecificType.values());
78          Collections.sort(highest);
79          StringBuffer result = new StringBuffer();
80          Iterator<MIMEType> it = mimeTypes.iterator();
81          while (it.hasNext()) {
82              MIMEType a = it.next();
83              if (!highest.contains(a)) continue;
84              if (result.length() > 0) {
85                  result.append(", ");
86              }
87              result.append(a);
88          }
89          return result.toString();
90      }
91  
92      private void add(MIMEType newAccept) {
93          if (newAccept.isAnyMajorType()) {
94              if (highestAnyType == null || newAccept.getQuality() > highestAnyType.getQuality()) {
95                  highestAnyType = newAccept;
96              }
97          } else if (newAccept.isAnySubtype()) {
98              if (!highestAnySubtype.containsKey(newAccept.getMajorType())
99                      || newAccept.getQuality() > highestAnySubtype.get(newAccept.getMajorType()).getQuality()) {
100                 highestAnySubtype.put(newAccept.getMajorType(), newAccept);
101             }
102         } else {
103             if (!highestSpecificType.containsKey(newAccept.getFullType())
104                     || newAccept.getQuality() > highestSpecificType.get(newAccept.getFullType()).getQuality()) {
105                 highestSpecificType.put(newAccept.getFullType(), newAccept);
106             }
107         }
108     }
109 
110     private void removeSpecificTypesCoveredByWildcard() {
111         for (MIMEType accept : highestSpecificType.values()) {
112             if (highestAnySubtype.containsKey(accept.getMajorType())
113                     && accept.getQuality() <= highestAnySubtype.get(accept.getMajorType()).getQuality()) {
114                 highestSpecificType.remove(accept.getFullType());
115             }
116         }
117         if (highestAnyType == null) return;
118         for (MIMEType accept : highestSpecificType.values()) {
119             if (accept.getQuality() <= highestAnyType.getQuality()) {
120                 highestSpecificType.remove(accept.getFullType());
121             }
122         }
123     }
124 
125     private void removeTypesCoveredByWildcard() {
126         if (highestAnyType == null) return;
127         for (MIMEType accept : highestAnySubtype.values()) {
128             if (accept.getQuality() <= highestAnyType.getQuality()) {
129                 highestAnySubtype.remove(accept.getMajorType());
130             }
131         }
132     }
133     
134 }