LegacyWebServiceHandler.java
/*
** Module : LegacyWebServiceHandler.java
** Abstract : Base class for handling legacy REST and SOAP service calls.
**
** Copyright (c) 2020-2025, Golden Code Development Corporation.
**
** -#- -I- --Date-- -----------------------------------------Description---------------------------------------
** 001 CA 20200514 First version.
** 002 CA 20220329 Added support for REST services written directly in Java.
** 003 SBI 20230324 Changed registerService to return the registered RestService.
** 004 GBB 20250403 Replace service type with enum.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** 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 Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.rest;
import java.lang.reflect.*;
import java.util.*;
import com.goldencode.p2j.main.*;
import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.util.*;
/**
* Defines common APIs for handling legacy SOAP or REST services.
*/
public abstract class LegacyWebServiceHandler
extends LegacyServiceHandler
{
/** Constant identifying the "external" mode. */
static final String EXTERNAL = "external";
/** Constant identifying the "Java" mode. */
public static final String JAVA = "java";
/** Constant identifying the "singleton" mode. */
public static final String SINGLETON = "singleton";
/** Constant identifying the "single-run" mode. */
public static final String SINGLE_RUN = "single-run";
/** Constant identifying the "persistent" mode. */
public static final String PERSISTENT = "persistent";
/** Constant identifying the "delete" mode. */
public static final String DELETE = "delete";
/** The exposed REST services. */
protected Map<ServiceKey, RestService> services = new HashMap<>();
/**
* Initialize this handler with the given base path.
*
* @param type
* The service type, also name of node to get the configuration from the directory.
* @param basePath
* The implicit basepath.
*/
public LegacyWebServiceHandler(ServiceType type, String basePath)
{
super(type, basePath);
}
/**
* Build a {@link ServiceKey} for the specified legacy service.
*
* @param ls
* The legacy service annotation.
*
* @return The service key.
*/
protected abstract ServiceKey buildServiceKey(LegacyService ls);
/**
* Register a legacy service associated with a legacy OO class method.
*
* @param ls
* The annotation describing the service, at the legacy class.
* @param cls
* The {@link com.goldencode.p2j.oo.lang.BaseObject} sub-class implementing this
* service.
* @param mls
* The annotation describing the service, at the legacy method.
* @param m
* The target operation.
*
* @return The registered service
*/
protected RestService registerService(LegacyService ls,
Class<? extends _BaseObject_> cls,
LegacyService mls,
Method m)
{
ServiceKey key = buildServiceKey(mls);
// this is from the lock of SourceNameMapper, no need to lock
if (services.containsKey(key))
{
throw new RuntimeException("More than one " + getType().toUpperCase() +
" service mapped to " + key);
}
RestService srv = new OORestService(ls, cls, mls, m, key, getTimeout());
services.put(srv.key, srv);
return srv;
}
/**
* Register a legacy service associated with a legacy external program.
*
* @param ls
* The annotation describing the service.
* @param pname
* The legacy program name.
* @param m
* The target Java method for this service.
*
* @return The registered service
*/
protected RestService registerService(LegacyService ls, String pname, Method m)
{
ServiceKey key = buildServiceKey(ls);
if (services.containsKey(key))
{
throw new RuntimeException("More than one " + getType().toUpperCase() +
" service mapped to " + key);
}
RestService srv = new ProgRestService(ls, pname, m, key, getTimeout());
services.put(srv.key, srv);
return srv;
}
/**
* Register a legacy service associated with an internal entry for an external program.
*
* @param ls
* The annotation describing the service, at the external program.
* @param pname
* The legacy program name.
* @param iels
* The annotation describing the service, at the internal entry.
* @param ie
* The target internal entry for this service.
* @param function
* Flag identifying if the internal entry is a function.
*
* @return The registered service
*/
protected RestService registerService(LegacyService ls,
String pname,
LegacyService iels,
InternalEntry ie,
boolean function)
{
ServiceKey key = buildServiceKey(iels);
if (services.containsKey(key))
{
throw new RuntimeException("More than one " + getType().toUpperCase() +
" service mapped to " + key);
}
RestService srv = new IERestService(ls, pname, iels, ie, function, key, getTimeout());
services.put(srv.key, srv);
return srv;
}
}