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

History | View | Annotate | Download (5.74 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.vcsgis.lib.repository.VCSGisRepository;
40
import org.gvsig.vcsgis.lib.repository.VCSGisRepositoryData;
41
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
42
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUpdateRequest;
43
import static org.gvsig.vcsgis.lib.server.handlers.AbstractVCSGisServertHandler.LOGGER;
44

    
45
/**
46
 *
47
 * @author gvSIG Team
48
 */
49
@SuppressWarnings("UseSpecificCatch")
50
public class UpdateHandler extends AbstractVCSGisServertHandler {
51

    
52
    private static final String REQUEST_NAME = "Update";
53
    
54
    public UpdateHandler(VCSGisRepository repository) {
55
        super(repository, REQUEST_NAME);
56
    }
57
    
58

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

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

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

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

    
107
            gen.writeStartObject("Parameters");
108
            gen.write("StatusCode", request.getLastErrorCode());
109

    
110
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 3");
111
            notifyResponseConsumers();
112
            gen.flush();
113

    
114
            if( StringUtils.isBlank(request.getLastErrorMessage()) ) {
115
                gen.writeNull("StatusMessage");
116
            } else {
117
                gen.write("StatusMessage", request.getLastErrorMessage());
118
            }
119

    
120
            gen.write("Entity", request.getEntity().toJson());
121
            gen.writeEnd(); // Paramaters
122

    
123
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 4");
124
            gen.writeStartArray("Data");
125
            for (VCSGisRepositoryData data : request.getData()) {
126
                gen.write(data.toJson());
127
                gen.flush();
128
            }
129
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 5");
130
            gen.writeEnd(); // Data
131
            gen.writeEnd(); 
132
            
133

    
134
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 6 gen.flush()");
135
            gen.flush();
136
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 7 response_contents.flush()");
137
            response_contents.flush();
138
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 8 end");
139
            
140
                        
141
        } catch (Exception ex) {
142
            LOGGER.warn("Can't produce Json data for "+this.getName()+" response.",ex);
143

    
144
        } finally {
145
            LOGGER.debug("===: ["+this.getName()+"] responseProducer finally");
146
            IOUtils.closeQuietly(gen);
147
//            IOUtils.closeQuietly(response_contents);
148
        }
149
    }
150

    
151
}