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