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 / EntitiesHandler.java @ 3375
History | View | Annotate | Download (4.18 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.repository.VCSGisRepository; |
39 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest; |
40 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest; |
41 |
|
42 |
/**
|
43 |
*
|
44 |
* @author gvSIG Team
|
45 |
*/
|
46 |
public class EntitiesHandler extends AbstractVCSGisServertHandler { |
47 |
|
48 |
public EntitiesHandler(VCSGisRepository repository) {
|
49 |
super(repository, "EntitiesHandler"); |
50 |
} |
51 |
|
52 |
@Override
|
53 |
protected void requestProducer(MutableObject<VCSGisRequest> req,Map<String,String> params, InputStream request_contents) throws IOException { |
54 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 1"); |
55 |
final VCSGisEntitiesRequest request = this.getRepository().createEntitiesRequest(); |
56 |
request.setAuthenticationTokenAndUser(params); |
57 |
req.setValue(request); |
58 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers"); |
59 |
notifyRequestConsumers(); |
60 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return"); |
61 |
} |
62 |
|
63 |
@Override
|
64 |
protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException { |
65 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 1"); |
66 |
VCSGisEntitiesRequest request = (VCSGisEntitiesRequest) req; |
67 |
if( request.execute()!=ERR_OK ) {
|
68 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 error "+request.getLastErrorCode()); |
69 |
throw new VCSGisRuntimeException(request.getLastErrorCode(), request.getLastErrorMessage()); |
70 |
} |
71 |
|
72 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 3"); |
73 |
List<VCSGisEntity> entities = request.getRepositoryEntities();
|
74 |
JsonArrayBuilder jsonEntities = Json.createArrayBuilder(); |
75 |
for (VCSGisEntity entity : entities) {
|
76 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 4 entity "+entity.getEntityName()); |
77 |
jsonEntities.add(entity.toJsonBuilder()); |
78 |
} |
79 |
JsonObjectBuilder jsonResponse = Json.createObjectBuilder(); |
80 |
jsonResponse.add("StatusCode", ERR_OK);
|
81 |
jsonResponse.addNull("StatusMessage");
|
82 |
jsonResponse.add("Entities", jsonEntities);
|
83 |
|
84 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 5 notifyResponseConsumers"); |
85 |
notifyResponseConsumers(); |
86 |
|
87 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 6 write json"); |
88 |
IOUtils.write(jsonResponse.toString(), response_contents); |
89 |
|
90 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 7 flush"); |
91 |
response_contents.flush(); |
92 |
|
93 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 8 close"); |
94 |
IOUtils.closeQuietly(response_contents); |
95 |
|
96 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 9 return"); |
97 |
|
98 |
} |
99 |
|
100 |
} |