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 @ 2728
History | View | Annotate | Download (4.45 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 |
String entityName = jsonRequest.getString("EntityName"); |
65 |
|
66 |
VCSGisUpdateRequest request = this.getRepository().createUpdateRequest(entityName);
|
67 |
request.setLocalRevisionCode(jsonRequest.getString("LocalRevisionCode"));
|
68 |
req.setValue(request); |
69 |
|
70 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers"); |
71 |
notifyRequestConsumers(); |
72 |
|
73 |
LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return"); |
74 |
} |
75 |
|
76 |
@Override
|
77 |
protected void responseProducer(VCSGisRequest req, OutputStream response_contents) throws IOException { |
78 |
final VCSGisCheckoutRequest request = (VCSGisCheckoutRequest) req;
|
79 |
JsonGenerator gen = null;
|
80 |
try {
|
81 |
JsonGeneratorFactory genFactory = Json.createGeneratorFactory( |
82 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
83 |
); |
84 |
gen = genFactory.createGenerator(response_contents); |
85 |
gen.writeStartObject(); |
86 |
|
87 |
gen.writeStartObject("Parameters");
|
88 |
gen.write("StatusCode", request.getLastErrorCode());
|
89 |
|
90 |
notifyResponseConsumers(); |
91 |
|
92 |
if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
|
93 |
gen.writeNull("StatusMessage");
|
94 |
} else {
|
95 |
gen.write("StatusMessage", request.getLastErrorMessage());
|
96 |
} |
97 |
|
98 |
gen.write("Entity", request.getEntity().toJson());
|
99 |
gen.writeEnd(); // Paramaters
|
100 |
|
101 |
gen.writeStartArray("Data");
|
102 |
for (VCSGisRepositoryData data : request.getData()) {
|
103 |
gen.write(data.toJson()); |
104 |
gen.flush(); |
105 |
} |
106 |
gen.writeEnd(); // Data
|
107 |
gen.writeEnd(); |
108 |
|
109 |
|
110 |
gen.flush(); |
111 |
response_contents.flush(); |
112 |
|
113 |
|
114 |
} catch (Exception ex) { |
115 |
LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex); |
116 |
|
117 |
} finally {
|
118 |
IOUtils.closeQuietly(gen); |
119 |
IOUtils.closeQuietly(response_contents); |
120 |
} |
121 |
} |
122 |
|
123 |
} |