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.syncope.client.ui.commons;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.fasterxml.jackson.databind.json.JsonMapper;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  import org.apache.wicket.util.io.IOUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public class MIMETypesLoader implements Serializable {
34  
35      private static final long serialVersionUID = -1865866358622817499L;
36  
37      protected static final Logger LOG = LoggerFactory.getLogger(MIMETypesLoader.class);
38  
39      protected static final JsonMapper MAPPER = JsonMapper.builder().findAndAddModules().build();
40  
41      protected Map<String, String> mimeTypesMap;
42  
43      protected List<String> mimeTypes;
44  
45      public void load() {
46          mimeTypesMap = new HashMap<>();
47          try {
48              JsonNode jsonNode = MAPPER.readTree(IOUtils.toString(getClass().getResourceAsStream("/MIMETypes.json")));
49              for (JsonNode node : jsonNode) {
50                  JsonNode type = node.path("name");
51                  JsonNode ext = node.path("extension");
52                  if (!type.isMissingNode()) {
53                      mimeTypesMap.put(type.asText(), ext.isMissingNode() ? "" : ext.asText());
54                  }
55              }
56  
57              mimeTypesMap = Collections.unmodifiableMap(mimeTypesMap);
58              LOG.debug("MIME types loaded: {}", mimeTypesMap);
59  
60              mimeTypes = new ArrayList<>(mimeTypesMap.keySet());
61              Collections.sort(mimeTypes);
62              mimeTypes = Collections.unmodifiableList(mimeTypes);
63          } catch (Exception e) {
64              LOG.error("Error reading file MIMETypes from resources", e);
65          }
66      }
67  
68      public List<String> getMimeTypes() {
69          return mimeTypes;
70      }
71  
72      public String getFileExt(final String mimeType) {
73          return mimeTypesMap.get(mimeType);
74      }
75  }