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 / UsersHandler.java @ 3315

History | View | Annotate | Download (4.16 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 org.apache.commons.io.IOUtils;
31
import org.apache.commons.lang3.mutable.MutableObject;
32
import org.gvsig.json.Json;
33
import org.gvsig.json.JsonArrayBuilder;
34
import org.gvsig.json.JsonObjectBuilder;
35
import org.gvsig.vcsgis.lib.VCSGisEntity;
36
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
37
import org.gvsig.vcsgis.lib.VCSGisRuntimeException;
38
import org.gvsig.vcsgis.lib.VCSGisUser;
39
import org.gvsig.vcsgis.lib.repository.VCSGisRepository;
40
import org.gvsig.vcsgis.lib.repository.requests.VCSGisEntitiesRequest;
41
import org.gvsig.vcsgis.lib.repository.requests.VCSGisRequest;
42
import org.gvsig.vcsgis.lib.repository.requests.VCSGisUsersRequest;
43

    
44
/**
45
 *
46
 * @author gvSIG Team
47
 */
48
public class UsersHandler extends AbstractVCSGisServertHandler {
49

    
50
    public UsersHandler(VCSGisRepository repository) {
51
        super(repository, "UsersHandler");
52
    }
53

    
54
    @Override
55
    protected void requestProducer(MutableObject<VCSGisRequest> req,Map<String,String> params, InputStream request_contents) throws IOException {
56
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 1");
57
        final VCSGisUsersRequest request = this.getRepository().createUsersRequest();
58
        req.setValue(request);
59
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 2 notifyRequestConsumers");
60
        notifyRequestConsumers();
61
        LOGGER.debug("===: ["+this.getName()+"] requestProducer 3 return");
62
    }
63
    
64
    @Override
65
    protected void responseProducer(VCSGisRequest req, Writer response_contents) throws IOException {
66
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  1");
67
        VCSGisUsersRequest request = (VCSGisUsersRequest) req;
68
        if( request.execute()!=ERR_OK ) {
69
            LOGGER.debug("===: ["+this.getName()+"] responseProducer 2 error "+request.getLastErrorCode());
70
            throw new VCSGisRuntimeException(request.getLastErrorCode(), request.getLastErrorMessage());
71
        }
72
        
73
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  3");
74
        List<VCSGisUser> users = request.getUsers();
75
        JsonArrayBuilder jsonUsers = Json.createArrayBuilder();
76
        for (VCSGisUser user : users) {
77
            LOGGER.debug("===: ["+this.getName()+"] responseProducer  4 entity "+user.getIdentifier());
78
            jsonUsers.add(user.toJsonBuilder());
79
        }
80
        JsonObjectBuilder jsonResponse = Json.createObjectBuilder();
81
        jsonResponse.add("StatusCode", ERR_OK);
82
        jsonResponse.addNull("StatusMessage");
83
        jsonResponse.add("Users", jsonUsers);
84
        
85
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  5 notifyResponseConsumers");
86
        notifyResponseConsumers();
87
        
88
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  6 write json");
89
        IOUtils.write(jsonResponse.toString(), response_contents);
90
        
91
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  7 flush");
92
        response_contents.flush();
93
        
94
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  8 close");
95
        IOUtils.closeQuietly(response_contents);
96
        
97
        LOGGER.debug("===: ["+this.getName()+"] responseProducer  9 return");
98
        
99
    }
100

    
101
}