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 / UpdateHandler.java @ 2823
History | View | Annotate | Download (5.29 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.OutputStream; |
28 |
import java.util.Collections; |
29 |
import javax.json.JsonObject; |
30 |
import javax.json.JsonReader; |
31 |
import javax.json.stream.JsonGenerator; |
32 |
import javax.json.stream.JsonGeneratorFactory; |
33 |
import org.apache.commons.io.IOUtils; |
34 |
import org.apache.commons.lang3.StringUtils; |
35 |
import org.apache.commons.lang3.mutable.MutableObject; |
36 |
import org.gvsig.json.Json; |
37 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepository; |
38 |
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData; |
39 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisCheckoutRequest; |
40 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest; |
41 |
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest; |
42 |
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER; |
43 |
|
44 |
/**
|
45 |
*
|
46 |
* @author gvSIG Team
|
47 |
*/
|
48 |
@SuppressWarnings("UseSpecificCatch") |
49 |
public class UpdateHandler extends AbstractVCSGisServertHandler { |
50 |
|
51 |
private static final String REQUEST_NAME = "Update"; |
52 |
|
53 |
public UpdateHandler(VCSGisRepository repository) {
|
54 |
super(repository, REQUEST_NAME);
|
55 |
} |
56 |
|
57 |
|
58 |
@Override
|
59 |
protected void requestProducer(MutableObject<VCSGisRequest>req, InputStream request_contents) throws IOException { |
60 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 1"); |
61 |
JsonReader reader = Json.createReader(request_contents); |
62 |
JsonObject jsonRequest = reader.readObject(); |
63 |
|
64 |
|
65 |
JsonObject jsonParameters = jsonRequest.getJsonObject("Parameters");
|
66 |
String entityName = jsonParameters.getString("EntityName"); |
67 |
|
68 |
VCSGisUpdateRequest request = this.getRepository().createUpdateRequest(entityName);
|
69 |
request.setLocalRevisionCode(jsonParameters.getString("LocalRevisionCode", null)); |
70 |
|
71 |
req.setValue(request); |
72 |
notifyRequestConsumers(); |
73 |
|
74 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers"); |
75 |
notifyRequestConsumers(); |
76 |
|
77 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return"); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
protected void responseProducer(VCSGisRequest req, OutputStream response_contents) throws IOException { |
82 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 1"); |
83 |
final VCSGisUpdateRequest request = (VCSGisUpdateRequest) req;
|
84 |
JsonGenerator gen = null;
|
85 |
try {
|
86 |
JsonGeneratorFactory genFactory = Json.createGeneratorFactory( |
87 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
88 |
); |
89 |
gen = genFactory.createGenerator(response_contents); |
90 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 2"); |
91 |
gen.writeStartObject(); |
92 |
|
93 |
gen.writeStartObject("Parameters");
|
94 |
gen.write("StatusCode", request.getLastErrorCode());
|
95 |
|
96 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 3"); |
97 |
notifyResponseConsumers(); |
98 |
gen.flush(); |
99 |
|
100 |
if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
|
101 |
gen.writeNull("StatusMessage");
|
102 |
} else {
|
103 |
gen.write("StatusMessage", request.getLastErrorMessage());
|
104 |
} |
105 |
|
106 |
gen.write("Entity", request.getEntity().toJson());
|
107 |
gen.writeEnd(); // Paramaters
|
108 |
|
109 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 4"); |
110 |
gen.writeStartArray("Data");
|
111 |
for (VCSGisRepositoryData data : request.getData()) {
|
112 |
gen.write(data.toJson()); |
113 |
gen.flush(); |
114 |
} |
115 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 5"); |
116 |
gen.writeEnd(); // Data
|
117 |
gen.writeEnd(); |
118 |
|
119 |
|
120 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 6 gen.flush()"); |
121 |
gen.flush(); |
122 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 7 response_contents.flush()"); |
123 |
response_contents.flush(); |
124 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer 8 end"); |
125 |
|
126 |
|
127 |
} catch (Exception ex) { |
128 |
LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex); |
129 |
|
130 |
} finally {
|
131 |
LOGGER.debug("===: ["+this.getName()+"] responseProducer finally"); |
132 |
IOUtils.closeQuietly(gen); |
133 |
IOUtils.closeQuietly(response_contents); |
134 |
} |
135 |
} |
136 |
|
137 |
} |