Statistics
| Revision:

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 @ 3633

History | View | Annotate | Download (6.2 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.Collections;
29
import java.util.Map;
30
import javax.json.JsonException;
31
import javax.json.JsonObject;
32
import javax.json.JsonReader;
33
import javax.json.stream.JsonGenerator;
34
import javax.json.stream.JsonGeneratorFactory;
35
import org.apache.commons.io.IOUtils;
36
import org.apache.commons.lang3.StringUtils;
37
import org.apache.commons.lang3.mutable.MutableObject;
38
import org.gvsig.json.Json;
39
import org.gvsig.json.JsonObjectBuilder;
40
import org.gvsig.vcsgis.lib.VCSGisUtils;
41
import static org.gvsig.vcsgis.lib.VCSGisUtils.ENTITY_AUTHORIZATIONS;
42
import static org.gvsig.vcsgis.lib.VCSGisUtils.ENTITY_FEATURETYPEASJSON;
43
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
44
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
45
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
46
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest;
47
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER;
48

    
49
/**
50
 *
51
 * @author gvSIG Team
52
 */
53
@SuppressWarnings("UseSpecificCatch")
54
public class UpdateHandler extends AbstractVCSGisServertHandler {
55

    
56
    private static final String REQUEST_NAME = "Update";
57
    
58
    public UpdateHandler(VCSGisRepository repository) {
59
        super(repository, REQUEST_NAME);
60
    }
61
    
62

    
63
    @Override
64
    protected void requestProducer(MutableObject<VCSGisRequest>req,Map<String,String> params, InputStream request_contents) throws IOException {
65
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
66

    
67
        JsonObject jsonParameters = null;
68
        try {
69
            JsonReader reader = Json.createReader(request_contents);
70
            JsonObject jsonRequest = reader.readObject();
71
            jsonParameters = jsonRequest.getJsonObject("Parameters");
72
        } catch (JsonException e) {
73
            //Do nothing
74
        }
75

    
76
        String entityName = params.get("EntityName");;
77
        if( jsonParameters != null ) {
78
            entityName = jsonParameters.getString("EntityName", entityName);
79
        }
80
        
81
        VCSGisUpdateRequest request = this.getRepository().createUpdateRequest(entityName);
82
        request.setAuthenticationTokenAndUser(params);
83
        if( jsonParameters == null ) {
84
            request.setLocalRevisionCode(params.get("LocalRevisionCode"));
85
        } else {
86
            request.setLocalRevisionCode(jsonParameters.getString("LocalRevisionCode", params.get("LocalRevisionCode")));
87
        }
88
        
89
        req.setValue(request);
90
//        notifyRequestConsumers();
91

    
92
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers");
93
        notifyRequestConsumers();
94
        
95
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return");
96
    }
97
    
98
    @Override
99
    protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException {
100
        LOGGER.debug("===: ["+this.getName()+"] responseProducer 1");
101
        final VCSGisUpdateRequest request = (VCSGisUpdateRequest) req;
102
        JsonGenerator gen = null;
103
        try {
104
            JsonGeneratorFactory genFactory = Json.createGeneratorFactory(
105
                Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true)
106
            );
107
            gen = genFactory.createGenerator(response_contents);
108
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2");
109
            gen.writeStartObject();
110

    
111
            gen.writeStartObject("Parameters");
112
            gen.write("StatusCode", request.getLastErrorCode());
113

    
114
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 3");
115
            notifyResponseConsumers();
116
            gen.flush();
117

    
118
            if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
119
                gen.writeNull("StatusMessage");
120
            } else {
121
                gen.write("StatusMessage", request.getLastErrorMessage());
122
            }
123

    
124
            JsonObjectBuilder builder = VCSGisUtils.toJsonBuilder(request.getEntity(),
125
                    (String t) -> StringUtils.equalsIgnoreCase(t, ENTITY_FEATURETYPEASJSON)
126
                    || StringUtils.equalsIgnoreCase(t, ENTITY_AUTHORIZATIONS));
127

    
128
            gen.write("Entity", builder.build());
129
            gen.writeEnd(); // Paramaters
130

    
131
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 4");
132
            gen.writeStartArray("Data");
133
            for (VCSGisRepositoryData data : request.getData()) {
134
                gen.write(data.toJson());
135
                gen.flush();
136
            }
137
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 5");
138
            gen.writeEnd(); // Data
139
            gen.writeEnd(); 
140
            
141

    
142
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 6 gen.flush()");
143
            gen.flush();
144
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 7 response_contents.flush()");
145
            response_contents.flush();
146
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 8 end");
147
            
148
                        
149
        } catch (Exception ex) {
150
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
151

    
152
        } finally {
153
            LOGGER.debug("===: ["+this.getName()+"] responseProducer finally");
154
            IOUtils.closeQuietly(gen);
155
//            IOUtils.closeQuietly(response_contents);
156
        }
157
    }
158

    
159
}