Client.java

Modified Client.java file - Gian Uberto Lauri, 04/11/2011 05:16 PM

Download (6.77 KB)

 
1
/*******************************************************************************
2
 * Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)
3
 * 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * 
8
 *   http://www.apache.org/licenses/LICENSE-2.0
9
 * 
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 ******************************************************************************/
16
package org.opennebula.client;
17

    
18
import java.io.BufferedReader;
19
import java.io.File;
20
import java.io.FileNotFoundException;
21
import java.io.FileReader;
22
import java.net.MalformedURLException;
23
import java.net.URL;
24
import java.security.MessageDigest;
25
import java.security.NoSuchAlgorithmException;
26

    
27
import org.apache.xmlrpc.XmlRpcException;
28
import org.apache.xmlrpc.client.XmlRpcClient;
29
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
30
import java.io.IOException;
31

    
32

    
33
/**
34
 * This class represents the connection with the core and handles the
35
 * xml-rpc calls.
36
 *
37
 */
38
public class Client{
39

    
40
    //--------------------------------------------------------------------------
41
    //  PUBLIC INTERFACE
42
    //--------------------------------------------------------------------------
43

    
44
    /**
45
     * Creates a new xml-rpc client with default options:
46
     * the auth. file will be assumed to be at $ONE_AUTH, and
47
     * the endpoint will be set to $ONE_XMLRPC.
48
     * <br/>
49
     * It is the equivalent of Client(null, null).
50
     *
51
     * @throws Exception if one authorization file cannot be located.
52
     */
53
    public Client() // throws Exception
54
    {
55
        setOneAuth(null);
56
        setOneEndPoint(null);
57
    }
58

    
59
    /**
60
     * Creates a new xml-rpc client with specified options.
61
     *
62
     * @param secret A string containing the ONE user:password tuple.
63
     * Can be null
64
     * @param endpoint Where the rpc server is listening, must be something
65
     * like "http://localhost:2633/RPC2". Can be null
66
     * @throws Exception if the authorization options are invalid
67
     */
68
    public Client(String secret, String endpoint) //throws Exception
69
    {
70
        setOneAuth(secret);
71
        setOneEndPoint(endpoint);
72
    }
73

    
74
    /**
75
     * Performs an XML-RPC call.
76
     *
77
     * @param action ONE action
78
     * @param args ONE arguments
79
     * @return The server's xml-rpc response encapsulated
80
     */
81
    public OneResponse call(String action, Object...args)
82
    {
83
        boolean success = false;
84
        String  msg = null;
85

    
86
        try
87
        {
88
            Object[] params = new Object[args.length + 1];
89

    
90
            params[0] = oneAuth;
91

    
92
            for(int i=0; i<args.length; i++)
93
                params[i+1] = args[i];
94

    
95
            Object[] result = (Object[]) client.execute("one."+action, params);
96

    
97
            success = (Boolean) result[0];
98

    
99
            // In some cases, the xml-rpc response only has a boolean
100
            // OUT parameter
101
            if(result.length > 1)
102
            {
103
                try
104
                {
105
                    msg = (String) result[1];
106
                }
107
                catch (ClassCastException e)
108
                {
109
                    // The result may be an Integer
110
                    msg = ((Integer) result[1]).toString();
111
                }
112
            }
113

    
114

    
115
        }
116
        catch (XmlRpcException e)
117
        {
118
            msg = e.getMessage();
119
        }
120

    
121
        return new OneResponse(success, msg);
122
    }
123

    
124
    //--------------------------------------------------------------------------
125
    //  PRIVATE ATTRIBUTES AND METHODS
126
    //--------------------------------------------------------------------------
127

    
128
    private String       oneAuth;
129
    private String       oneEndPoint;
130

    
131
    private XmlRpcClient client;
132

    
133
    private void setOneAuth(String secret) // throws Exception
134
    {
135
        String oneSecret = secret;
136

    
137
        try
138
        {
139
            if(oneSecret == null)
140
            {
141
                String oneAuthEnv = System.getenv("ONE_AUTH");
142
                File   authFile;
143

    
144
                if ( oneAuthEnv != null && oneAuthEnv.length() != 0)
145
                {
146
                    authFile = new File(oneAuthEnv);
147
                }
148
                else
149
                {
150
                    authFile = new File(System.getenv("HOME")+"/.one/one_auth");
151
                }
152

    
153
                oneSecret =
154
                      (new BufferedReader(new FileReader(authFile))).readLine();
155
            }
156

    
157
            String[] token = oneSecret.split(":");
158

    
159
            if(token.length != 2 )
160
            {
161
                throw new OpenNebulaConfigurationError("Wrong format for authorization string: "
162
                    + oneSecret + "\nFormat expected is user:password");
163
            }
164

    
165
            MessageDigest md = MessageDigest.getInstance("SHA-1");
166
            byte[] digest    = md.digest(token[1].getBytes());
167

    
168
            String hash = "";
169

    
170
            for(byte aux : digest)
171
            {
172
                int b = aux & 0xff;
173

    
174
                if (Integer.toHexString(b).length() == 1)
175
                {
176
                    hash += "0";
177
                }
178

    
179
                hash += Integer.toHexString(b);
180
            }
181

    
182
            oneAuth = token[0] + ":" + hash;
183
        }
184
        catch (FileNotFoundException e)
185
        {
186
                        // This come first, since is a special case of IOException
187
            throw new OpenNebulaConfigurationError("ONE_AUTH file not present");
188
        }
189
                catch (IOException ioex) {
190
                        // You could have the file but for some reason the program can not
191
                        // read it
192
                        throw new OpenNebulaConfigurationError("ONE_AUTH file unreadable");
193
                }
194
        catch (NoSuchAlgorithmException e)
195
        {
196
            throw new OpenNebulaConfigurationError("Error initializing MessageDigest with SHA-1");
197
        }
198
    }
199

    
200
    private void setOneEndPoint(String endpoint) //throws Exception
201
    {
202
        oneEndPoint = "http://localhost:2633/RPC2";
203

    
204
        if(endpoint != null)
205
        {
206
            oneEndPoint = endpoint;
207
        }
208
        else
209
        {
210
            String oneXmlRpcEnv = System.getenv("ONE_XMLRPC");
211

    
212
            if ( oneXmlRpcEnv != null && oneXmlRpcEnv.length() != 0 )
213
            {
214
                oneEndPoint = oneXmlRpcEnv;
215
            }
216
        }
217

    
218
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
219

    
220
        try
221
        {
222
            config.setServerURL(new URL(oneEndPoint));
223
        }
224
        catch (MalformedURLException e)
225
        {
226
            throw new OpenNebulaConfigurationError("The URL "+oneEndPoint+" is malformed.");
227
        }
228

    
229
        client = new XmlRpcClient();
230
        client.setConfig(config);
231
    }
232
}