PageRenderTime 32ms CodeModel.GetById 22ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-sdk/src/com/nimbits/console/KeyFile.java

http://nimbits-server.googlecode.com/
Java | 70 lines | 41 code | 10 blank | 19 comment | 8 complexity | bf5cf77aa62cb3efd16178dcfd201021 MD5 | raw file
 1/*
 2 * Copyright (c) 2010 Tonic Solutions LLC.
 3 *
 4 * http://www.nimbits.com
 5 *
 6 *
 7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 8 *
 9 * http://www.gnu.org/licenses/gpl.html
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
12 */
13
14package com.nimbits.console;
15
16import com.nimbits.client.model.*;
17import com.nimbits.security.*;
18
19import java.io.*;
20import java.util.*;
21
22/**
23 * Created by Benjamin Sautner
24 * User: bsautner
25 * Date: 8/16/11
26 * Time: 2:16 PM
27 */
28public class KeyFile {
29    public static String genKey(final Map<String, String> argsMap) throws IOException {
30
31        final String filename = argsMap.get(Const.PARAM_OUT);
32        final StringBuilder sb = new StringBuilder();
33        if (argsMap.containsKey(Const.PARAM_KEY)) {
34            sb.append("-key=").append(argsMap.get(Const.PARAM_KEY)).append(" ");
35        }
36        if (argsMap.containsKey(Const.PARAM_EMAIL)) {
37            sb.append("-email=").append(argsMap.get(Const.PARAM_EMAIL)).append(" ");
38        }
39        if (argsMap.containsKey(Const.PARAM_PASSWORD)) {
40            sb.append("-password=").append(argsMap.get(Const.PARAM_PASSWORD)).append(" ");
41        }
42        if (argsMap.containsKey(Const.PARAM_HOST)) {
43            sb.append("-host=").append(argsMap.get(Const.PARAM_HOST)).append(" ");
44        }
45
46        final File file = new File(filename);
47        if (file.exists()) {
48            //noinspection ResultOfMethodCallIgnored
49            file.delete();
50        }
51
52        Encryptor.writeEncryptedFile(filename, sb.toString());
53
54        return (new File(filename).exists()) ? "Key file generated" : "Could not create key file";
55
56    }
57
58    public static String[] processKeyFile(final Map<String, String> argsMap) throws IOException {
59        final String fn =  argsMap.get(Const.PARAM_I);
60        final File file = new File(fn);
61        String[] args = null;
62        if (file.exists()) {
63            final String params = Encryptor.readEncryptedFile(fn);
64            if (params != null) {
65                args = params.split(" ");
66            }
67        }
68        return args;
69    }
70}