summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/profile/ProfileClient.java
blob: 7f0b08f0e40d9a5314bcf741c2dc2fc514802ad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//--- BEGIN COPYRIGHT BLOCK ---
//This program is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; version 2 of the License.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along
//with this program; if not, write to the Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
//(C) 2012 Red Hat, Inc.
//All rights reserved.
//--- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.profile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Properties;

import javax.ws.rs.core.Response;

import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.client.Client;
import com.netscape.certsrv.client.PKIClient;

/**
 * @author Ade Lee
 */
public class ProfileClient extends Client {

    public ProfileResource profileClient;

    public ProfileClient(PKIClient client, String subsystem) throws URISyntaxException {
        super(client, subsystem, "profile");
        init();
    }

    public void init() throws URISyntaxException {
        profileClient = createProxy(ProfileResource.class);
    }

    public ProfileData retrieveProfile(String id) {
        Response response = profileClient.retrieveProfile(id);
        return client.getEntity(response, ProfileData.class);
    }

    public Properties retrieveProfileRaw(String id) {
        Response response = profileClient.retrieveProfileRaw(id);
        return byteArrayToProperties(client.getEntity(response, byte[].class));
    }

    public ProfileDataInfos listProfiles(Integer start, Integer size) {
        Response response =  profileClient.listProfiles(start, size);
        return client.getEntity(response, ProfileDataInfos.class);
    }

    public void enableProfile(String id) {
        Response response = profileClient.modifyProfileState(id, "enable");
        client.getEntity(response, Void.class);
    }

    public void disableProfile(String id) {
        Response response = profileClient.modifyProfileState(id, "disable");
        client.getEntity(response, Void.class);
    }

    public ProfileData createProfile(ProfileData data) {
        Response response = profileClient.createProfile(data);
        return client.getEntity(response, ProfileData.class);
    }

    public Properties createProfileRaw(Properties properties) {
        Response response =
            profileClient.createProfileRaw(propertiesToByteArray(properties));
        return byteArrayToProperties(client.getEntity(response, byte[].class));
    }

    public ProfileData modifyProfile(ProfileData data) {
        Response response = profileClient.modifyProfile(data.getId(), data);
        return client.getEntity(response, ProfileData.class);
    }

    public Properties modifyProfileRaw(String profileId, Properties properties) {
        Response response =
            profileClient.modifyProfileRaw(profileId, propertiesToByteArray(properties));
        return byteArrayToProperties(client.getEntity(response, byte[].class));
    }

    public void deleteProfile(String id) {
        Response response = profileClient.deleteProfile(id);
        client.getEntity(response, Void.class);
    }

    private Properties byteArrayToProperties(byte[] data) throws PKIException {
        Properties properties = new Properties();
        try {
            properties.load(new ByteArrayInputStream(data));
        } catch (IOException e) {
            throw new PKIException("Failed to decode profile Properties: " + e.toString());
        }
        return properties;
    }

    private byte[] propertiesToByteArray(Properties properties) throws PKIException {
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            properties.store(data, null);
        } catch (IOException e) {
            throw new PKIException("Failed to encode profile Properties: " + e.toString());
        }
        return data.toByteArray();
    }
}