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 
20 package org.apache.guacamole.rest.connectiongroup;
21 
22 import com.google.inject.assistedinject.Assisted;
23 import com.google.inject.assistedinject.AssistedInject;
24 import java.util.List;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.QueryParam;
30 import javax.ws.rs.core.MediaType;
31 import org.apache.guacamole.GuacamoleException;
32 import org.apache.guacamole.net.auth.ConnectionGroup;
33 import org.apache.guacamole.net.auth.Directory;
34 import org.apache.guacamole.net.auth.UserContext;
35 import org.apache.guacamole.net.auth.permission.ObjectPermission;
36 import org.apache.guacamole.rest.directory.DirectoryObjectResource;
37 import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
38 
39 /**
40  * A REST resource which abstracts the operations available on an existing
41  * ConnectionGroup.
42  */
43 @Produces(MediaType.APPLICATION_JSON)
44 @Consumes(MediaType.APPLICATION_JSON)
45 public class ConnectionGroupResource
46         extends DirectoryObjectResource<ConnectionGroup, APIConnectionGroup> {
47 
48     /**
49      * The UserContext associated with the Directory which contains the
50      * ConnectionGroup exposed by this resource.
51      */
52     private final UserContext userContext;
53 
54     /**
55      * The ConnectionGroup object represented by this ConnectionGroupResource.
56      */
57     private final ConnectionGroup connectionGroup;
58 
59     /**
60      * Creates a new ConnectionGroupResource which exposes the operations and
61      * subresources available for the given ConnectionGroup.
62      *
63      * @param userContext
64      *     The UserContext associated with the given Directory.
65      *
66      * @param directory
67      *     The Directory which contains the given ConnectionGroup.
68      *
69      * @param connectionGroup
70      *     The ConnectionGroup that this ConnectionGroupResource should
71      *     represent.
72      *
73      * @param translator
74      *     A DirectoryObjectTranslator implementation which handles the type of
75      *     object given.
76      */
77     @AssistedInject
ConnectionGroupResource(@ssisted UserContext userContext, @Assisted Directory<ConnectionGroup> directory, @Assisted ConnectionGroup connectionGroup, DirectoryObjectTranslator<ConnectionGroup, APIConnectionGroup> translator)78     public ConnectionGroupResource(@Assisted UserContext userContext,
79             @Assisted Directory<ConnectionGroup> directory,
80             @Assisted ConnectionGroup connectionGroup,
81             DirectoryObjectTranslator<ConnectionGroup, APIConnectionGroup> translator) {
82         super(userContext, directory, connectionGroup, translator);
83         this.userContext = userContext;
84         this.connectionGroup = connectionGroup;
85     }
86 
87     /**
88      * Returns the current connection group along with all descendants.
89      *
90      * @param permissions
91      *     If specified and non-empty, limit the returned list to only those
92      *     connections for which the current user has any of the given
93      *     permissions. Otherwise, all visible connections are returned.
94      *     ConnectionGroups are unaffected by this parameter.
95      *
96      * @return
97      *     The current connection group, including all descendants.
98      *
99      * @throws GuacamoleException
100      *     If a problem is encountered while retrieving the connection group or
101      *     its descendants.
102      */
103     @GET
104     @Path("tree")
getConnectionGroupTree( @ueryParamR) List<ObjectPermission.Type> permissions)105     public APIConnectionGroup getConnectionGroupTree(
106             @QueryParam("permission") List<ObjectPermission.Type> permissions)
107             throws GuacamoleException {
108 
109         // Retrieve the requested tree, filtering by the given permissions
110         ConnectionGroupTree tree = new ConnectionGroupTree(userContext,
111                 connectionGroup, permissions);
112 
113         // Return tree as a connection group
114         return tree.getRootAPIConnectionGroup();
115 
116     }
117 
118 }
119