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 / repository / remoteclient / requests / EntitiesRequestClient.java @ 2697

History | View | Annotate | Download (4.23 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.repository.remoteclient.requests;
24

    
25
import java.io.IOException;
26
import java.net.URL;
27
import java.util.ArrayList;
28
import java.util.List;
29
import javax.json.Json;
30
import javax.json.JsonArray;
31
import javax.json.JsonObject;
32
import javax.json.JsonReader;
33
import javax.json.JsonValue;
34
import org.apache.commons.io.IOUtils;
35
import org.apache.http.HttpEntity;
36
import org.apache.http.client.methods.CloseableHttpResponse;
37
import org.apache.http.client.methods.HttpGet;
38
import org.apache.http.impl.client.CloseableHttpClient;
39
import org.apache.http.impl.client.HttpClients;
40
import org.apache.http.util.EntityUtils;
41
import org.gvsig.vcsgis.lib.EntityEditableImpl;
42
import org.gvsig.vcsgis.lib.VCSGisEntity;
43
import org.gvsig.vcsgis.lib.VCSGisEntityEditable;
44
import static org.gvsig.vcsgis.lib.VCSGisUtils.getErrorMessage;
45
import static org.gvsig.vcsgis.lib.client.VCSGisWorkspace.ERR_CANT_COMMIT;
46
import static org.gvsig.vcsgis.lib.client.VCSGisWorkspace.ERR_CANT_RETRIEVE_ENTITIES;
47
import static org.gvsig.vcsgis.lib.client.VCSGisWorkspace.ERR_NO_ERROR;
48
import static org.gvsig.vcsgis.lib.client.VCSGisWorkspace.ERR_OK;
49
import org.gvsig.vcsgis.lib.requests.AbstractVCSGisEntitiesRequest;
50
import org.gvsig.vcsgis.repository.remoteclient.VCSGisRepositoryClient;
51

    
52
/**
53
 *
54
 * @author gvSIG Team
55
 */
56
public class EntitiesRequestClient extends AbstractVCSGisEntitiesRequest {
57
    
58
    public EntitiesRequestClient(VCSGisRepositoryClient repository) {
59
        super(repository);
60
    }
61

    
62
    @Override
63
    protected VCSGisRepositoryClient getRepository() {
64
        return (VCSGisRepositoryClient) super.getRepository();
65
    }
66

    
67
    @Override
68
    public int execute() {
69
        
70
        URL url = this.getRepository().getUrl("entities");
71
        
72
        List<VCSGisEntity> theEntities = new ArrayList<>();
73
        
74
        CloseableHttpClient httpClient = null;
75
        CloseableHttpResponse response = null;
76
        try {
77
            httpClient = HttpClients.createDefault();
78
            HttpGet httpGet = new HttpGet(url.toString());
79
            response = httpClient.execute(httpGet);
80

    
81
            HttpEntity responseEntity = response.getEntity();
82
            JsonReader reader = Json.createReader(responseEntity.getContent());
83
            JsonObject jsonResponse = reader.readObject();
84
            EntityUtils.consumeQuietly(responseEntity);
85
            
86
            int statusCode = jsonResponse.getInt("StatusCode", ERR_CANT_COMMIT);
87
            if( statusCode!=ERR_OK ) {
88
                return error(
89
                        statusCode,
90
                        jsonResponse.getString("StatusMessage", getErrorMessage(statusCode))
91
                );
92
            }
93
            JsonArray jsonEntities = jsonResponse.getJsonArray("Entities");
94
            for (JsonValue x : jsonEntities) {
95
                VCSGisEntityEditable entity = new EntityEditableImpl();
96
                entity.fromJson((JsonObject) x);
97
                theEntities.add(entity);
98
            }
99
            this.entities = theEntities;
100
            return error(ERR_NO_ERROR);
101

    
102
        } catch (IOException ex) {
103
            LOGGER.warn("Can't retrieve entities from '"+url+"'.",ex);
104
            return error(ERR_CANT_RETRIEVE_ENTITIES, "Can't retrieve entities from '"+url+"'.");
105
        } finally {
106
            IOUtils.closeQuietly(response);
107
            IOUtils.closeQuietly(httpClient);
108
        }
109
    }
110

    
111
    @Override
112
    public void dispose() {
113
        
114
    }
115

    
116
}