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 / CheckoutHandler.java @ 2823
History | View | Annotate | Download (4.42 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 static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER; |
42 |
|
43 |
/**
|
44 |
*
|
45 |
* @author gvSIG Team
|
46 |
*/
|
47 |
@SuppressWarnings("UseSpecificCatch") |
48 |
public class CheckoutHandler extends AbstractVCSGisServertHandler { |
49 |
|
50 |
public CheckoutHandler(VCSGisRepository repository) {
|
51 |
super(repository, "CheckoutHandler"); |
52 |
} |
53 |
|
54 |
|
55 |
@Override
|
56 |
protected void requestProducer(MutableObject<VCSGisRequest>req, InputStream request_contents) throws IOException { |
57 |
JsonReader reader = Json.createReader(request_contents); |
58 |
JsonObject jsonRequest = reader.readObject(); |
59 |
|
60 |
|
61 |
JsonObject jsonParameters = jsonRequest.getJsonObject("Parameters");
|
62 |
String entityName = jsonParameters.getString("EntityName"); |
63 |
|
64 |
VCSGisCheckoutRequest request = this.getRepository().createCheckoutRequest(entityName);
|
65 |
request.setEfectiveDate(jsonParameters.getString("EfectiveDate", null)); |
66 |
request.setRevisionCode(jsonParameters.getString("RevisionCode", null)); |
67 |
|
68 |
req.setValue(request); |
69 |
notifyRequestConsumers(); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
protected void responseProducer(VCSGisRequest req, OutputStream response_contents) throws IOException { |
74 |
final VCSGisCheckoutRequest request = (VCSGisCheckoutRequest) req;
|
75 |
JsonGenerator gen = null;
|
76 |
try {
|
77 |
JsonGeneratorFactory genFactory = Json.createGeneratorFactory( |
78 |
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true) |
79 |
); |
80 |
gen = genFactory.createGenerator(response_contents); |
81 |
gen.writeStartObject(); |
82 |
|
83 |
gen.writeStartObject("Parameters");
|
84 |
gen.write("StatusCode", request.getLastErrorCode());
|
85 |
|
86 |
notifyResponseConsumers(); |
87 |
gen.flush(); |
88 |
|
89 |
if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
|
90 |
gen.writeNull("StatusMessage");
|
91 |
} else {
|
92 |
gen.write("StatusMessage", request.getLastErrorMessage());
|
93 |
} |
94 |
|
95 |
gen.write("Entity", request.getEntity().toJson());
|
96 |
gen.writeEnd(); // Paramaters
|
97 |
|
98 |
gen.writeStartArray("Data");
|
99 |
for (VCSGisRepositoryData data : request.getData()) {
|
100 |
gen.writeStartObject(); |
101 |
gen.write("DataCode", data.getDataCode());
|
102 |
gen.write("Data", data.getData());
|
103 |
gen.writeEnd(); |
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 |
} |