158 lines
6.8 KiB
Java
158 lines
6.8 KiB
Java
package dev.alexzaw.rest4i.api;
|
|
|
|
import com.ibm.as400.access.AS400;
|
|
import com.ibm.as400.access.AS400Exception;
|
|
import com.ibm.as400.access.ExtendedIOException;
|
|
import com.ibm.as400.access.ObjectDescription;
|
|
import com.ibm.as400.access.ObjectList;
|
|
import dev.alexzaw.rest4i.exception.RestWABadRequestException;
|
|
import dev.alexzaw.rest4i.session.Session;
|
|
import dev.alexzaw.rest4i.util.AsyncLogger;
|
|
import dev.alexzaw.rest4i.util.CommonUtil;
|
|
import dev.alexzaw.rest4i.util.JSONSerializer;
|
|
import dev.alexzaw.rest4i.util.SystemObject;
|
|
import dev.alexzaw.rest4i.util.SystemObjectQSYS;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class QSYSAPIImpl extends APIImpl {
|
|
|
|
public static QSYSOperationResult qsysGetQsysObjects(Session session, String objectName, String objectLibrary, String objectType, String objectSubtype, String memberName, String memberType) {
|
|
AS400 sys = null;
|
|
ObjectList objList = null;
|
|
List<SystemObject.SystemObjectInfo> qsysObjList = new ArrayList<>();
|
|
boolean subtypeSpecified = false;
|
|
try {
|
|
if (CommonUtil.hasContent(objectName)) {
|
|
objectName = objectName.trim();
|
|
if (objectName.length() == 11 && objectName.endsWith("*"))
|
|
objectName = objectName.substring(0, objectName.length() - 1);
|
|
}
|
|
objectName = checkParam("objectName", objectName, 10, null);
|
|
objectLibrary = checkParam("objectLibrary", objectLibrary, 10, "*USRLIBL");
|
|
objectType = checkParam("objectType", objectType, 10, "*ALL");
|
|
objectSubtype = checkParam("objectSubtype", objectSubtype, 10, "*ALL");
|
|
memberName = checkParam("memberName", memberName, 10, "");
|
|
memberType = checkParam("memberType", memberType, 10, "*ALL");
|
|
if (memberName.equalsIgnoreCase("*ALL"))
|
|
memberName = "*";
|
|
subtypeSpecified = !objectSubtype.equalsIgnoreCase("*ALL");
|
|
AsyncLogger.traceDebug("search",
|
|
"in-parms: objectName='%s', objectLibrary='%s', objectType='%s', objectSubtype='%s', memberName='%s', memberType='%s', ", new Object[] { objectName, objectLibrary, objectType, objectSubtype, memberName, memberType });
|
|
if (objectLibrary.equalsIgnoreCase("*ALL") && objectType.equalsIgnoreCase("*ALL") && objectName.equalsIgnoreCase("*ALL") && !subtypeSpecified)
|
|
throw new RestWABadRequestException("The combination of values specified is not supported.", new Object[0]);
|
|
sys = session.getAS400();
|
|
objList = new ObjectList(sys, objectLibrary, objectName, objectType);
|
|
ObjectDescription[] objects = objList.getObjects(-1, 0);
|
|
if (objects != null) {
|
|
SystemObjectQSYS qsysObject = null;
|
|
byte b;
|
|
int i;
|
|
ObjectDescription[] arrayOfObjectDescription;
|
|
for (i = (arrayOfObjectDescription = objects).length, b = 0; b < i; ) {
|
|
ObjectDescription objdesc = arrayOfObjectDescription[b];
|
|
String path = objdesc.getPath();
|
|
if (path.equalsIgnoreCase("/qsys.lib/qsys.lib"))
|
|
continue;
|
|
if (subtypeSpecified) {
|
|
try {
|
|
String subtype = objdesc.getValueAsString(202);
|
|
String requestedSubtype = objectSubtype.toUpperCase();
|
|
if (subtype.equals("PF") && (
|
|
requestedSubtype.startsWith("PF-") ||
|
|
requestedSubtype.startsWith("PF38-") ||
|
|
requestedSubtype.startsWith("PF"))) {
|
|
boolean reqsrcpf = !(!objectSubtype.equalsIgnoreCase("PF-SRC") && !objectSubtype.equalsIgnoreCase("PF38-SRC"));
|
|
boolean reqpf = requestedSubtype.equalsIgnoreCase("PF");
|
|
qsysObject = new SystemObjectQSYS(sys, objdesc);
|
|
boolean isSrcPF = qsysObject.getQSYSPFSubtype().equalsIgnoreCase("PF-SRC");
|
|
if (!reqpf && ((!isSrcPF && reqsrcpf) || (isSrcPF && !reqsrcpf)))
|
|
continue;
|
|
if (CommonUtil.hasContent(memberName)) {
|
|
List<SystemObject> mbrs = qsysObject.listDir(true, memberName, memberType);
|
|
for (SystemObject f : mbrs) {
|
|
SystemObject.SystemObjectInfo systemObjectInfo = f.getInfo(false);
|
|
if (systemObjectInfo != null)
|
|
qsysObjList.add(systemObjectInfo);
|
|
}
|
|
continue;
|
|
}
|
|
} else {
|
|
if (!subtype.equalsIgnoreCase(objectSubtype))
|
|
continue;
|
|
qsysObject = new SystemObjectQSYS(sys, objdesc);
|
|
}
|
|
} catch (ExtendedIOException e) {
|
|
int rc = e.getReturnCode();
|
|
if (rc != 5 && rc != 13)
|
|
throw e;
|
|
continue;
|
|
} catch (AS400Exception e) {
|
|
String errorMsg = e.getMessage();
|
|
if (errorMsg != null)
|
|
if (errorMsg.indexOf("CPF9802") != -1 || errorMsg.indexOf("CPF9822") != -1)
|
|
continue;
|
|
throw e;
|
|
}
|
|
} else {
|
|
qsysObject = new SystemObjectQSYS(sys, objdesc);
|
|
}
|
|
SystemObject.SystemObjectInfo fi = qsysObject.getInfo(false);
|
|
if (fi != null)
|
|
qsysObjList.add(fi);
|
|
continue;
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
handleException("search", e);
|
|
} finally {
|
|
if (objList != null)
|
|
try {
|
|
objList.close();
|
|
} catch (Exception exception) {}
|
|
}
|
|
QSYSOperationResult qsysOpResult = new QSYSOperationResult();
|
|
qsysOpResult.objectList_ = new QSYSObjectList();
|
|
qsysOpResult.objectList_.qsysObjects_ = qsysObjList;
|
|
return qsysOpResult;
|
|
}
|
|
|
|
private static String checkParam(String nm, String objectValue, int maxLen, String defaultValue) {
|
|
if (!CommonUtil.hasContent(objectValue)) {
|
|
if (defaultValue == null)
|
|
throw new RestWABadRequestException("Object name not set.", new Object[0]);
|
|
objectValue = defaultValue;
|
|
}
|
|
objectValue = objectValue.trim();
|
|
if (maxLen != -1 && !objectValue.startsWith("*") && objectValue.length() > maxLen)
|
|
throw new RestWABadRequestException("The length of the value for parameter '%s' is not valid.", new Object[] { nm });
|
|
if (objectValue.equals("*"))
|
|
objectValue = "*ALL";
|
|
return objectValue;
|
|
}
|
|
|
|
public static class QSYSOperationResult {
|
|
public QSYSAPIImpl.QSYSObjectList objectList_ = null;
|
|
|
|
public boolean setEtag_ = false;
|
|
|
|
public String etagHeader_ = null;
|
|
}
|
|
|
|
public static class QSYSObjectList {
|
|
public List<SystemObject.SystemObjectInfo> qsysObjects_ = null;
|
|
|
|
public String toJSON() {
|
|
JSONSerializer json = new JSONSerializer();
|
|
json.startObject();
|
|
json.startArray("objects");
|
|
if (!this.qsysObjects_.isEmpty())
|
|
for (SystemObject.SystemObjectInfo f : this.qsysObjects_)
|
|
f.toJSON(json);
|
|
json.endArray();
|
|
json.endObject();
|
|
return json.toString();
|
|
}
|
|
}
|
|
}
|