summaryrefslogtreecommitdiffstats
path: root/base/java-tools/src/com/netscape/cmstools/cert/CertRequestReviewCLI.java
blob: 682314b2545161285220fd4fe85089e97d6d9f6f (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
package com.netscape.cmstools.cert;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.ParseException;

import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.cert.CertReviewResponse;
import com.netscape.certsrv.request.RequestId;
import com.netscape.cmstools.cli.CLI;
import com.netscape.cmstools.cli.MainCLI;

public class CertRequestReviewCLI extends CLI {

    CertCLI parent;

    public CertRequestReviewCLI(CertCLI parent) {
        super("request-review", "Review certificate request");
        this.parent = parent;
    }

    @Override
    public void execute(String[] args) {
        CommandLine cmd = null;

        Option output = new Option(null, "output", true, "Output Filename");
        options.addOption(output);

        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.err.println("Error: " + e.getMessage());
            printHelp();
            System.exit(-1);
        }

        String[] cLineArgs = cmd.getArgs();

        if (cLineArgs.length < 1) {
            System.err.println("Error: No request id specified.");
            printHelp();
            System.exit(-1);
        }
        String filename = null;
        if (cmd.hasOption("output")) {
            filename = cmd.getOptionValue("output");
        } else {
            System.err.println("No output option specified.");
            printHelp();
            System.exit(-1);
        }

        if (filename == null || filename.trim().length() == 0) {
            System.err.println("Specify the filename to write the request information");
            printHelp();
            System.exit(-1);
        }

        RequestId reqId = null;
        try {
            reqId = new RequestId(cLineArgs[0]);
        } catch (NumberFormatException e) {
            System.err.println("Error: Invalid RequestID: " + cLineArgs[0]);
            System.exit(-1);
        }

        CertReviewResponse reviewInfo = null;
        try {
            reviewInfo = parent.client.reviewRequest(reqId);
        } catch (PKIException e) {
            System.err.println(e.getMessage());
            System.exit(-1);
        }

        try {
            JAXBContext context = JAXBContext.newInstance(CertReviewResponse.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            FileOutputStream stream = new FileOutputStream(filename);

            marshaller.marshal(reviewInfo, stream);
            MainCLI.printMessage("Downloaded certificate request " + cLineArgs[0]);
        } catch (JAXBException e) {
            System.err.println("Cannot write to the file. " + e);
        } catch (FileNotFoundException e) {
            System.err.println("File not found at " + filename);
        }

    }

    @Override
    public void printHelp() {
        formatter.printHelp(parent.name + "-" + name + " <request id>", options);
    }
}