gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.lib / org.gvsig.vcsgis.lib.impl / src / main / java / org / gvsig / vcsgis / lib / server / handlers / UsersHandler.java @ 3375
History | View | Annotate | Download (4.21 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
18 |
*
|
19 |
* For any additional information, do not hesitate to contact us
|
20 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.vcsgis.lib.server.handlers; |
24 |
|
25 |
import java.io.IOException; |
26 |
import java.io.InputStream; |
27 |
import java.io.Writer; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import org.apache.commons.io.IOUtils; |
31 |
import org.apache.commons.lang3.mutable.MutableObject; |
32 |
import org.gvsig.json.Json; |
33 |
import org.gvsig.json.JsonArrayBuilder; |
34 |
import org.gvsig.json.JsonObjectBuilder; |
35 |
import org.gvsig.vcsgis.lib.VCSGisEntity; |
36 |
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK; |
37 |
import org.gvsig.vcsgis.lib.VCSGisRuntimeException; |
38 |
import org.gvsig.vcsgis.lib.VCSGisUser; |
39 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
40 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
41 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest; |
42 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUsersRequest; |
43 |
|
44 |
/**
|
45 |
*
|
46 |
* @author gvSIG Team
|
47 |
*/
|
48 |
public class UsersHandler extends AbstractVCSGisServertHandler { |
49 |
|
50 |
public UsersHandler(VCSGisRepository repository) {
|
51 |
super(repository, "UsersHandler"); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
protected void requestProducer(MutableObject<VCSGisRequest> req,Map<String,String> params, InputStream request_contents) throws IOException { |
56 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 1"); |
57 |
final VCSGisUsersRequest request = this.getRepository().createUsersRequest(); |
58 |
request.setAuthenticationTokenAndUser(params); |
59 |
req.setValue(request); |
60 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers"); |
61 |
notifyRequestConsumers(); |
62 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return"); |
63 |
} |
64 |
|
65 |
@Override
|
66 |
protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException { |
67 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 1"); |
68 |
VCSGisUsersRequest request = (VCSGisUsersRequest) req; |
69 |
if( request.execute()!=ERR_OK ) {
|
70 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 error "+request.getLastErrorCode()); |
71 |
throw new VCSGisRuntimeException(request.getLastErrorCode(), request.getLastErrorMessage()); |
72 |
} |
73 |
|
74 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 3"); |
75 |
List<VCSGisUser> users = request.getUsers();
|
76 |
JsonArrayBuilder jsonUsers = Json.createArrayBuilder(); |
77 |
for (VCSGisUser user : users) {
|
78 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 4 entity "+user.getIdentifier()); |
79 |
jsonUsers.add(user.toJsonBuilder()); |
80 |
} |
81 |
JsonObjectBuilder jsonResponse = Json.createObjectBuilder(); |
82 |
jsonResponse.add("StatusCode", ERR_OK);
|
83 |
jsonResponse.addNull("StatusMessage");
|
84 |
jsonResponse.add("Users", jsonUsers);
|
85 |
|
86 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 5 notifyResponseConsumers"); |
87 |
notifyResponseConsumers(); |
88 |
|
89 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 6 write json"); |
90 |
IOUtils.write(jsonResponse.toString(), response_contents); |
91 |
|
92 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 7 flush"); |
93 |
response_contents.flush(); |
94 |
|
95 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 8 close"); |
96 |
IOUtils.closeQuietly(response_contents); |
97 |
|
98 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 9 return"); |
99 |
|
100 |
} |
101 |
|
102 |
} |