use of org.jvnet.hk2.config.ConfigBean in project Payara by payara.
the class TemplateRestResource method doDelete.
protected ExitCode doDelete(Map<String, String> data) {
if (data == null) {
data = new HashMap<String, String>();
}
if (entity == null) {
// wrong resource
// return Response.status(404).entity(ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo)).build();
throwError(Status.NOT_FOUND, localStrings.getLocalString("rest.resource.erromessage.noentity", "Resource not found."));
}
if (getDeleteCommand() == null) {
String message = localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", uriInfo.getAbsolutePath());
throwError(Status.FORBIDDEN, message);
}
if (getDeleteCommand().equals("GENERIC-DELETE")) {
try {
ConfigBean p = (ConfigBean) parent;
if (parent == null) {
p = (ConfigBean) entity.parent();
}
ConfigSupport.deleteChild(p, (ConfigBean) entity);
return ExitCode.SUCCESS;
} catch (TransactionFailure ex) {
throw new WebApplicationException(ex, Response.Status.INTERNAL_SERVER_ERROR);
}
}
// do the delete via the command:
if (data.containsKey("error")) {
throwError(Status.BAD_REQUEST, localStrings.getLocalString("rest.request.parsing.error", "Unable to parse the input entity. Please check the syntax."));
}
ResourceUtil.addQueryString(uriInfo.getQueryParameters(), data);
ResourceUtil.purgeEmptyEntries(data);
ResourceUtil.adjustParameters(data);
if (data.get("DEFAULT") == null) {
addDefaultParameter(data);
} else {
String resourceName = getResourceName(uriInfo.getAbsolutePath().getPath(), "/");
if (!data.get("DEFAULT").equals(resourceName)) {
throwError(Status.FORBIDDEN, localStrings.getLocalString("rest.resource.not.deleted", "Resource not deleted. Value of \"name\" should be the name of this resource."));
}
}
RestActionReporter actionReport = runCommand(getDeleteCommand(), data);
if (actionReport != null) {
ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
if (exitCode != ActionReport.ExitCode.FAILURE) {
return exitCode;
}
throwError(Status.BAD_REQUEST, actionReport.getMessage());
}
throw new WebApplicationException(handleError(Status.BAD_REQUEST, localStrings.getLocalString("rest.resource.delete.forbidden", "DELETE on \"{0}\" is forbidden.", uriInfo.getAbsolutePath())));
}
use of org.jvnet.hk2.config.ConfigBean in project Payara by payara.
the class SetCommand method setLeafElement.
public static void setLeafElement(final ConfigBean node, final String elementName, final String values) throws TransactionFailure {
ConfigBeanProxy readableView = node.getProxy(node.getProxyType());
ConfigSupport.apply(new SingleConfigCode<ConfigBeanProxy>() {
/**
* Runs the following command passing the configuration object. The
* code will be run within a transaction, returning true will commit
* the transaction, false will abort it.
*
* @param param is the configuration object protected by the
* transaction
* @return any object that should be returned from within the
* transaction code
* @throws java.beans.PropertyVetoException if the changes cannot be
* applied to the configuration
*/
@Override
public Object run(ConfigBeanProxy param) throws PropertyVetoException, TransactionFailure {
WriteableView writeableParent = (WriteableView) Proxy.getInvocationHandler(param);
StringTokenizer st = new StringTokenizer(values, ",");
List<String> valList = new ArrayList<>();
while (st.hasMoreTokens()) {
valList.add(st.nextToken());
}
ConfigBean bean = writeableParent.getMasterView();
for (Method m : writeableParent.getProxyType().getMethods()) {
// Check to see if the method is a setter for the element
// An element setter has to have the right name, take a single
// collection parameter that parameterized with the right type
Class[] argClasses = m.getParameterTypes();
Type[] argTypes = m.getGenericParameterTypes();
ConfigModel.Property prop = bean.model.toProperty(m);
if (prop == null || !prop.xmlName().equals(elementName) || argClasses.length != 1 || !Collection.class.isAssignableFrom(argClasses[0]) || argTypes.length != 1 || !(argTypes[0] instanceof ParameterizedType) || !Types.erasure(Types.getTypeArgument(argTypes[0], 0)).isAssignableFrom(values.getClass())) {
continue;
}
// we have the right method. Now call it
try {
m.invoke(writeableParent.getProxy(writeableParent.<ConfigBeanProxy>getProxyType()), valList);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new TransactionFailure("Exception while setting element", e);
}
return node;
}
throw new TransactionFailure("No method found for setting element");
}
}, readableView);
}
use of org.jvnet.hk2.config.ConfigBean in project Payara by payara.
the class SetCommand method setProperty.
private boolean setProperty(AdminCommandContext context, String pattern, SetOperation op, String targetName, Map<Dom, String> dottedNames) {
Dom parentNode = null;
pattern = pattern.substring(0, trueLastIndexOf(pattern, '.'));
TreeNode[] parentNodes_ = getAliasedParent(domain, pattern);
pattern = parentNodes_[0].relativeName;
Map<Dom, String> matchingNodes_ = getMatchingNodes(dottedNames, pattern);
if (matchingNodes_.isEmpty()) {
fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
return false;
}
for (Map.Entry<Dom, String> node : matchingNodes_.entrySet()) {
if (node.getValue().equals(pattern)) {
parentNode = node.getKey();
}
}
if (parentNode == null) {
fail(context, localStrings.getLocalString("admin.set.configuration.notfound", "No configuration found for {0}", targetName));
return false;
}
if (op.value == null || op.value.length() == 0) {
// setting to the empty string means to remove the property, so don't create it
success(context, targetName, op.value);
return true;
}
// create and set the property
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("value", op.value);
attributes.put("name", op.attrName);
try {
if (!(parentNode instanceof ConfigBean)) {
final ClassCastException cce = new ClassCastException(parentNode.getClass().getName());
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", cce.getMessage(), cce));
return false;
}
ConfigSupport.createAndSet((ConfigBean) parentNode, Property.class, attributes);
return replicatePropertyChange(context, op, targetName);
} catch (TransactionFailure transactionFailure) {
fail(context, localStrings.getLocalString("admin.set.attribute.change.failure", "Could not change the attributes: {0}", transactionFailure.getMessage()), transactionFailure);
return false;
}
}
Aggregations