View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.http;
17  
18  import java.util.Collection;
19  import java.util.Properties;
20  
21  import org.apache.http.HttpHeaders;
22  import org.apache.http.HttpResponse;
23  import org.esigate.Parameters;
24  
25  public class ContentTypeHelper {
26      private Collection<String> parsableContentTypes;
27  
28      public ContentTypeHelper(Properties properties) {
29          parsableContentTypes = Parameters.PARSABLE_CONTENT_TYPES.getValue(properties);
30      }
31  
32      /**
33       * Check whether the given request's content-type corresponds to "parseable" text.
34       * 
35       * @param httpResponse
36       *            the response to analyze depending on its content-type
37       * @return true if this represents text or false if not
38       */
39      public boolean isTextContentType(HttpResponse httpResponse) {
40          String contentType = HttpResponseUtils.getFirstHeader(HttpHeaders.CONTENT_TYPE, httpResponse);
41          return isTextContentType(contentType);
42      }
43  
44      /**
45       * Check whether the given content-type corresponds to "parseable" text.
46       * 
47       * @param contentType
48       *            the input content-type
49       * @return true if this represents text or false if not
50       */
51      public boolean isTextContentType(String contentType) {
52          if (contentType != null) {
53              String lowerContentType = contentType.toLowerCase();
54              for (String textContentType : this.parsableContentTypes) {
55                  if (lowerContentType.startsWith(textContentType)) {
56                      return true;
57                  }
58              }
59          }
60          return false;
61      }
62  
63  }