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 / EntitiesHandler.java @ 3633

History | View | Annotate | Download (4.68 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.List;
29
import java.util.Map;
30
import java.util.function.Predicate;
31
import org.apache.commons.io.IOUtils;
32
import org.apache.commons.lang3.StringUtils;
33
import org.apache.commons.lang3.mutable.MutableObject;
34
import org.gvsig.json.Json;
35
import org.gvsig.json.JsonArrayBuilder;
36
import org.gvsig.json.JsonObjectBuilder;
37
import org.gvsig.vcsgis.lib.VCSGisEntity;
38
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
39
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
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.requests.VCSGisEntitiesRequest;
45
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
46

    
47
/**
48
 *
49
 * @author gvSIG Team
50
 */
51
public class EntitiesHandler extends AbstractVCSGisServertHandler {
52

    
53
    public EntitiesHandler(VCSGisRepository repository) {
54
        super(repository, "EntitiesHandler");
55
    }
56

    
57
    @Override
58
    protected void requestProducer(MutableObject<VCSGisRequest> req,Map<String,String> params, InputStream request_contents) throws IOException {
59
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
60
        final VCSGisEntitiesRequest request = this.getRepository().createEntitiesRequest();
61
        request.setAuthenticationTokenAndUser(params);       
62
        req.setValue(request);
63
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers");
64
        notifyRequestConsumers();
65
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return");
66
    }
67
    
68
    @Override
69
    protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException {
70
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  1");
71
        VCSGisEntitiesRequest request = (VCSGisEntitiesRequest) req;
72
        if( request.execute()!=ERR_OK ) {
73
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 error "+request.getLastErrorCode());
74
            throw new VCSGisRuntimeException(request.getLastErrorCode(), request.getLastErrorMessage());
75
        }
76
        
77
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  3");
78
        List<VCSGisEntity> entities = request.getRepositoryEntities();
79
        JsonArrayBuilder jsonEntities = Json.createArrayBuilder();
80
        for (VCSGisEntity entity : entities) {
81
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  4 entity "+entity.getEntityName());
82
            JsonObjectBuilder builder = VCSGisUtils.toJsonBuilder(entity, (String t) -> StringUtils.equalsIgnoreCase(t, ENTITY_AUTHORIZATIONS));
83
            jsonEntities.add(builder);
84
        }
85
        JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
86
        JsonObjectBuilder jsonParams = Json.createObjectBuilder();
87

    
88
        jsonParams.add("StatusCode", ERR_OK);
89
        jsonParams.addNull("StatusMessage");
90
        jsonParams.add("Entities", jsonEntities);
91

    
92
        jsonResponse.add("Parameters", jsonParams);
93
        
94
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  5 notifyResponseConsumers");
95
        notifyResponseConsumers();
96
        
97
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 write json");
98
        IOUtils.write(jsonResponse.toString(), response_contents);
99
        
100
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  7 flush");
101
        response_contents.flush();
102
        
103
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  8 close");
104
        IOUtils.closeQuietly(response_contents);
105
        
106
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  9 return");
107
        
108
    }
109

    
110
}