View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.cache.impl;
20  
21  import java.util.Map;
22  import java.util.SortedMap;
23  import java.util.TreeMap;
24  
25  /**
26   * Content type cache.
27   */
28  public class ContentTypeCacheLevelImpl extends MapCacheLevelImpl {
29  
30      private static final long serialVersionUID = 1L;
31  
32      /**
33       * Constructor.
34       */
35      public ContentTypeCacheLevelImpl() {
36          super();
37          enableKeyFallback(null);
38      }
39  
40      @Override
41      public Object get(String key) {
42          return super.get(normalize(key));
43      }
44  
45      @Override
46      public void put(Object value, String key) {
47          super.put(value, normalize(key));
48      }
49  
50      @Override
51      public void remove(String key) {
52          super.remove(normalize(key));
53      }
54  
55      /**
56       * Normalizes the key which should be a content type. It's quite simple at
57       * the moment but should cover most cases.
58       */
59      private static String normalize(String key) {
60          if (key == null) {
61              return null;
62          }
63  
64          StringBuilder sb = new StringBuilder(32);
65          int parameterStart = 0;
66  
67          // first, get the MIME type
68          for (int i = 0; i < key.length(); i++) {
69              char c = key.charAt(i);
70  
71              if (Character.isWhitespace(c)) {
72                  continue;
73              } else if (c == ';') {
74                  parameterStart = i;
75                  break;
76              }
77  
78              sb.append(Character.toLowerCase(c));
79          }
80  
81          // if parameters have been found, gather them
82          if (parameterStart > 0) {
83              SortedMap<String, String> parameter = new TreeMap<String, String>();
84              StringBuilder ksb = new StringBuilder(32);
85              StringBuilder vsb = new StringBuilder(32);
86              boolean isKey = true;
87  
88              for (int i = parameterStart + 1; i < key.length(); i++) {
89                  char c = key.charAt(i);
90                  if (Character.isWhitespace(c)) {
91                      continue;
92                  }
93  
94                  if (isKey) {
95                      if (c == '=') {
96                          // value start
97                          isKey = false;
98                          continue;
99                      }
100 
101                     ksb.append(Character.toLowerCase(c));
102                 } else {
103                     if (c == ';') {
104                         // next key
105                         isKey = true;
106 
107                         parameter.put(ksb.toString(), vsb.toString());
108 
109                         ksb.setLength(0);
110                         vsb.setLength(0);
111 
112                         continue;
113                     } else if (c == '"') {
114                         // filter quotes
115                         continue;
116                     }
117 
118                     vsb.append(Character.toLowerCase(c));
119                 }
120             }
121 
122             // add last parameter
123             if (ksb.length() > 0) {
124                 parameter.put(ksb.toString(), vsb.toString());
125             }
126 
127             // write parameters sorted by key
128             for (Map.Entry<String, String> entry : parameter.entrySet()) {
129                 sb.append(';');
130                 sb.append(entry.getKey());
131                 sb.append('=');
132                 sb.append(entry.getValue());
133             }
134         }
135 
136         return sb.toString();
137     }
138 }