Skip to content

Commit

Permalink
Refactor RA_Client::Execute()
Browse files Browse the repository at this point in the history
The RA_Client::Execute() has been converted into Java code
in TPSClientCLI.execute(). The original C code has been moved
into tpsclient.cpp. The TPSClientCLI.invokeOperation() has
been added to call RA_Client::InvokeOperation().
  • Loading branch information
edewata committed Feb 28, 2025
1 parent aa3c316 commit e43227c
Show file tree
Hide file tree
Showing 6 changed files with 277 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
//
package com.netscape.cmstools.tps;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.lang3.StringUtils;
import org.dogtagpki.cli.CommandCLI;

import com.netscape.cmstools.cli.MainCLI;
Expand Down Expand Up @@ -33,14 +38,78 @@ public void printHelp() {
formatter.printHelp(getFullName() + " [OPTIONS...]", options);
}

public native void execute() throws Exception;
public Map<String, String> parse(String line) {

logger.info("Parsing " + line);

Map<String, String> map = new HashMap<>();

for (String param : line.split(" ")) {

String[] parts = param.split("=", 2);
String key = parts[0];
String value = parts[1];
logger.info("- " + key + ": " + value);

map.put(key, value);
}

return map;
}

public native long createClient() throws Exception;
public native void removeClient(long client) throws Exception;

public native void invokeOperation(
long client,
String op,
Map<String, String> params)
throws Exception;

@Override
public void execute(CommandLine cmd) throws Exception {

MainCLI mainCLI = (MainCLI) getRoot();
mainCLI.init();

execute();
System.out.println("TPS Client");
System.out.println("'op=help' for Help");

long client = createClient();

try (Scanner input = new Scanner(System.in)) {
while (true) {
System.out.print("Command> ");
System.out.flush();

String line = input.nextLine();

if (line == null) {
break;
}

System.out.println(line);

if (StringUtils.isBlank(line)) {
continue;
}

if (line.startsWith("#")) {
continue;
}

Map<String, String> params = parse(line);
String op = params.get("op");

if ("exit".equals(op)) {
break;
}

invokeOperation(client, op, params);
}

} finally {
removeClient(client);
}
}
}
1 change: 1 addition & 0 deletions base/tools/src/main/native/tpsclient/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ set(tps_library_SRCS
main/RA_Token.cpp
main/Memory.cpp
main/AuthParams.cpp
main/TPSClientCLI.cpp
apdu/APDU.cpp
apdu/Unblock_Pin_APDU.cpp
apdu/Create_Object_APDU.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class RA_Client
int OpExit(NameValueSet *set);
public:
void Debug(const char *func_name, const char *fmt, ...);
void Execute();
void InvokeOperation(char *op, NameValueSet *set);
public:
RA_Token m_token;
Expand Down
101 changes: 0 additions & 101 deletions base/tools/src/main/native/tpsclient/src/main/RA_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <jni.h>

#include "prinrval.h"
#include "prmem.h"
Expand Down Expand Up @@ -85,13 +84,6 @@ RA_Client::~RA_Client ()
}
}

static void
PrintHeader ()
{
printf ("Registration Authority Client\n");
printf ("'op=help' for Help\n");
}

static void
Output (const char *fmt, ...)
{
Expand All @@ -103,12 +95,6 @@ Output (const char *fmt, ...)
va_end (ap);
}

static void
PrintPrompt ()
{
printf ("Command>");
}

static void
OutputSuccess (const char *fmt, ...)
{
Expand All @@ -131,28 +117,6 @@ OutputError (const char *fmt, ...)
va_end (ap);
}

static int
ReadLine (char *buf, int len)
{
char *cur = buf;

while (1)
{
*cur = getchar ();
if (*cur == '\r')
{
continue;
}
if (*cur == '\n')
{
*cur = '\0';
return 1;
}
cur++;
}
return 0;
}

void
RA_Client::Debug (const char *func_name, const char *fmt, ...)
{
Expand Down Expand Up @@ -1575,68 +1539,3 @@ RA_Client::InvokeOperation (char *op, NameValueSet * params)
(end - start) / 1000);
}
}

/**
* Execute RA client.
*/
void
RA_Client::Execute ()
{
char line[1024];
int rc;
char *op;
int done = 0;
char *lasts = NULL;

/* start main loop */
PrintHeader ();
while (!done)
{
PrintPrompt ();
rc = ReadLine (line, 1024);
printf ("%s\n", line);
if (rc <= 0)
{
break; /* exit if no more line */
}
if (line[0] == '#')
{
continue; /* ignore comment line */
}
/* format: 'op=cmd <parameters>' */
NameValueSet *params = NameValueSet::Parse (line, " ");
if (params == NULL)
{
continue;
}
op = params->GetValue ("op");
if (op == NULL)
{
/* user did not type op= */
op = PL_strtok_r (line, " ", &lasts);
if (op == NULL)
continue;
}
if (strcmp (op, "exit") == 0)
{
done = 1;
}
else
{
InvokeOperation (op, params);
}
if (params != NULL)
{
delete params;
params = NULL;
}
}
} /* Execute */

extern "C" JNIEXPORT void JNICALL
Java_com_netscape_cmstools_tps_TPSClientCLI_execute
(JNIEnv* env, jclass clazz)
{
RA_Client client;
client.Execute();
}
109 changes: 109 additions & 0 deletions base/tools/src/main/native/tpsclient/src/main/TPSClientCLI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// --- BEGIN COPYRIGHT BLOCK ---
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation;
// version 2.1 of the License.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
// Copyright (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---

#include <jni.h>

#include "main/NameValueSet.h"
#include "main/RA_Client.h"

extern "C" JNIEXPORT jlong JNICALL
Java_com_netscape_cmstools_tps_TPSClientCLI_createClient
(JNIEnv* env, jclass clazz) {
RA_Client* client = new RA_Client();
return (jlong) client;
}

extern "C" JNIEXPORT void JNICALL
Java_com_netscape_cmstools_tps_TPSClientCLI_removeClient
(JNIEnv* env, jclass clazz, jlong client) {
RA_Client* cclient = (RA_Client*) client;
delete cclient;
}

extern "C" JNIEXPORT void JNICALL
Java_com_netscape_cmstools_tps_TPSClientCLI_invokeOperation
(JNIEnv* env, jclass clazz, jlong client, jstring op, jobject params) {

// Map
jclass mapClass = env->FindClass("java/util/Map");
jmethodID keySetMethod = env->GetMethodID(
mapClass,
"keySet",
"()Ljava/util/Set;");
jmethodID getMethod = env->GetMethodID(
mapClass,
"get",
"(Ljava/lang/Object;)Ljava/lang/Object;");

// Set
jclass setClass = env->FindClass("java/util/Set");
jmethodID iteratorMethod = env->GetMethodID(
setClass,
"iterator",
"()Ljava/util/Iterator;");

// Iterator
jclass iteratorClass = env->FindClass("java/util/Iterator");
jmethodID hasNextMethod = env->GetMethodID(
iteratorClass,
"hasNext",
"()Z");
jmethodID nextMethod = env->GetMethodID(
iteratorClass,
"next",
"()Ljava/lang/Object;");

RA_Client* cclient = (RA_Client*) client;
char* cop = (char*) env->GetStringUTFChars(op, NULL);
NameValueSet *set = new NameValueSet();

// Set<String> keys = params.keySet();
jobject keys = env->CallObjectMethod(params, keySetMethod);

// Iterator<String> iterator = keys.iterator();
jobject iterator = env->CallObjectMethod(keys, iteratorMethod);

while (true) {
// boolean hasNext = iterator.hasNext();
jboolean hasNext = env->CallBooleanMethod(iterator, hasNextMethod);

if (!hasNext) {
break;
}

// String key = iterator.next();
jstring key = (jstring) env->CallObjectMethod(iterator, nextMethod);

// String value = params.get(key);
jstring value = (jstring) env->CallObjectMethod(params, getMethod, key);

char* ckey = (char*) env->GetStringUTFChars(key, NULL);
char* cvalue = (char*) env->GetStringUTFChars(value, NULL);

set->Add(ckey, cvalue);

env->ReleaseStringUTFChars(value, cvalue);
env->ReleaseStringUTFChars(key, ckey);
}

cclient->InvokeOperation(cop, set);

env->ReleaseStringUTFChars(op, cop);
}
Loading

0 comments on commit e43227c

Please sign in to comment.