View Javadoc
1   /**
2    * Copyright (C) 2003-2008 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  
18  package org.exoplatform.social.service.rest;
19  
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.ws.rs.GET;
25  import javax.ws.rs.Path;
26  import javax.ws.rs.PathParam;
27  import javax.ws.rs.WebApplicationException;
28  import javax.ws.rs.core.Context;
29  import javax.ws.rs.core.MediaType;
30  import javax.ws.rs.core.Response;
31  import javax.ws.rs.core.UriInfo;
32  import javax.xml.bind.annotation.XmlRootElement;
33  
34  import org.exoplatform.application.registry.Application;
35  import org.exoplatform.application.registry.ApplicationCategory;
36  import org.exoplatform.application.registry.ApplicationRegistryService;
37  import org.exoplatform.container.PortalContainer;
38  import org.exoplatform.portal.config.model.ApplicationType;
39  import org.exoplatform.services.rest.resource.ResourceContainer;
40  
41  
42  /**
43   * Provides services for the application registry gadget which shows a list of applications.
44   *
45   * @anchor AppsRestService
46   *
47   */
48  @Path("social/apps")
49  public class AppsRestService implements ResourceContainer {
50    
51    private ApplicationRegistryService _applicationRegistryService;
52    /**
53     * constructor
54     */
55    public AppsRestService() {}
56    
57    /**
58     * Gets applications from the application registry service of the portal.
59     *
60     *
61     * @param uriInfo The requested URI information.
62     * @param format The type of a returned result.
63     *
64     * @anchor AppsRestService.showApps
65     *
66     * @return The response contains returned results.
67     * 
68     * @throws Exception
69     * 
70     * @LevelAPI Platform
71     */
72    @GET
73    @Path("show.{format}")
74    public Response showApps(@Context UriInfo uriInfo, @PathParam("format") String format) throws Exception {
75      //TODO hoatle gets currentUser for filter
76      MediaType mediaType = Util.getMediaType(format);
77      AppList appList = showApps();
78      return Util.getResponse(appList, uriInfo, mediaType, Response.Status.OK);
79    }
80    
81    /**
82     * Describes an application entity from the application registry service of the portal.
83     * This class is needed as a model for converter of the rest service.
84     */
85    static public class App {
86      private String _appId;
87      private String _appName;
88      /**
89       * sets appId
90       * @param appId application id
91       */
92      public void setAppId(String appId) { _appId = appId; }
93      /**
94       * gets appId
95       * @return appId application id
96       */
97      public String getAppId() { return _appId; }
98      /**
99       * sets application name
100      * @param appName application name
101      */
102     public void setAppName(String appName) { _appName = appName; }
103     /**
104      * gets application name
105      * @return application name
106      */
107     public String getAppName() {return _appName ;}
108   }
109   
110   /**
111    * List that contains applications from application registry service of portal<br>
112    * This class is needed as a model for converter of the rest service.
113    */
114   @XmlRootElement
115   static public class AppList {
116     private List<App> _apps;
117     /**
118      * sets application list
119      * @param apps application list
120      */
121     public void setApps(List<App> apps) { _apps = apps; }
122     /**
123      * gets application list
124      * @return application list
125      */
126     public List<App> getApps() { return _apps; }
127     /**
128      * adds application to application list
129      * @param app application
130      * @see App
131      */
132     public void addApp(App app) {
133       if (_apps == null) {
134         _apps = new ArrayList<App>();
135       }
136       _apps.add(app);
137     }
138   }
139   
140   /**
141    * shows appList
142    * @return appList application list entity
143    * @see AppList
144    */
145   private AppList showApps() {
146     AppList appList = new AppList();
147     ApplicationRegistryService applicationRegistryService = getApplicationRegistryService();
148     //String[] applicationTypes = {org.exoplatform.web.application.Application.EXO_PORTLET_TYPE};
149     try {
150       List<ApplicationCategory> applicationCategoryList = applicationRegistryService.getApplicationCategories("root");
151       Iterator<ApplicationCategory> applicationCategoryItr = applicationCategoryList.iterator();
152       ApplicationCategory applicationCategory;
153       while (applicationCategoryItr.hasNext()) {
154         applicationCategory = applicationCategoryItr.next();
155         ApplicationType<org.exoplatform.portal.pom.spi.portlet.Portlet> portletType = ApplicationType.PORTLET;
156         List<Application> applications = applicationRegistryService.getApplications(applicationCategory, portletType);
157         Iterator<Application> applicationItr = applications.iterator();
158         Application application;
159         while (applicationItr.hasNext()) {
160           App app = new App();
161           application = applicationItr.next();
162           app.setAppId(application.getId());
163           app.setAppName(application.getDisplayName());
164           appList.addApp(app);
165         }
166       }
167     } catch (Exception ex) {
168       throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
169     }
170     return appList;
171   }
172   
173   /**
174    * gets applicationRegistryService
175    * @return applicationRegistryService
176    * @see ApplicationRegistryService
177    */
178   private ApplicationRegistryService getApplicationRegistryService() {
179     if (_applicationRegistryService == null) {
180       PortalContainer portalContainer = PortalContainer.getInstance();
181       _applicationRegistryService = (ApplicationRegistryService) portalContainer.
182               getComponentInstanceOfType(ApplicationRegistryService.class);
183     }
184     return _applicationRegistryService;
185   }
186 }