View Javadoc
1   /*
2    * Copyright (C) 2003-2009 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.wcm.connector.collaboration;
18  
19  import java.util.Collections;
20  import java.util.Iterator;
21  import java.util.LinkedHashSet;
22  import java.util.Locale;
23  import java.util.MissingResourceException;
24  import java.util.ResourceBundle;
25  import java.util.Set;
26  
27  import javax.ws.rs.GET;
28  import javax.ws.rs.Path;
29  import javax.ws.rs.QueryParam;
30  import javax.ws.rs.core.CacheControl;
31  import javax.ws.rs.core.MediaType;
32  import javax.ws.rs.core.Response;
33  import javax.xml.parsers.DocumentBuilderFactory;
34  import javax.xml.transform.dom.DOMSource;
35  
36  import org.exoplatform.services.resources.ResourceBundleService;
37  import org.exoplatform.services.rest.resource.ResourceContainer;
38  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
39  import org.w3c.dom.Document;
40  import org.w3c.dom.Element;
41  
42  /**
43   * Gets the bundle that is based on the key and the locale.
44   *
45   * @LevelAPI Provisional
46   *
47   * @anchor ResourceBundleConnector
48   */
49  @Path("/bundle/")
50  public class ResourceBundleConnector implements ResourceContainer {
51  
52    /**
53    * Gets the bundle that is based on the key and the locale.
54     *
55    * @param key The key used to get the bundle.
56    * @param locale  The locale used to get the bundle.
57    * 
58    * @anchor ResourceBundleConnector.getBundle
59    */
60    @GET
61    @Path("/getBundle/")
62    public Response getBundle (
63        @QueryParam("key") String key,
64        @QueryParam("locale") String locale) {
65      try {
66        ResourceBundleService resourceBundleService = WCMCoreUtils.getService(ResourceBundleService.class);
67        String resourceBundleNames[] = resourceBundleService.getSharedResourceBundleNames();
68        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
69        Element bundles = document.createElement("bundles");
70        bundles.setAttribute("locale", locale);
71        String keys[] = key.split(",");
72        Set<String> remainingKeys = new LinkedHashSet<String>(keys.length + 1, 1f);
73        Collections.addAll(remainingKeys, keys);
74        loop : for (String resourceBundleName : resourceBundleNames) {
75          ResourceBundle resourceBundle = null;
76          if(locale.indexOf("_") > 0) {
77              resourceBundle = resourceBundleService.getResourceBundle(resourceBundleName, new Locale(
78                  locale.substring(0, locale.lastIndexOf("_")), 
79                  locale.substring(locale.lastIndexOf("_") + 1, locale.length()))); 
80              
81          } else {
82            resourceBundle = resourceBundleService.getResourceBundle(resourceBundleName, new Locale(locale));
83          }
84          
85          for (Iterator<String> it = remainingKeys.iterator(); it.hasNext();) {
86            String oneKey = it.next();
87            try {
88              String value = resourceBundle.getString(oneKey);
89              Element element = document.createElement(oneKey);
90              element.setAttribute("value", value);
91              bundles.appendChild(element);
92              it.remove();
93              if (remainingKeys.isEmpty()) {
94                break loop;
95              }
96            } catch (MissingResourceException e) {
97              continue;
98            }
99          }
100       }
101       document.appendChild(bundles);
102 
103       CacheControl cacheControl = new CacheControl();
104       cacheControl.setNoCache(true);
105       cacheControl.setNoStore(true);
106       return Response.ok(new DOMSource(document), MediaType.TEXT_XML).cacheControl(cacheControl).build();
107     } catch (Exception e) {
108       return Response.serverError().build();
109     }
110   }
111 
112 }